January 12, 2016

Automate Amazon: Writing a Shopping Cart Test

This post is seventh in a series of nine. Need to go back to the beginning?

Finally! We have the testing framework written!

Now it is time to write a simple test to check to see how we could develop some tests to use this framework.

Let's call our test method in the PurchaseOrderTest class "test_CreatePurchaseForSingleProduct". Our test can be:

1) Go to the Product page, and check the price of a product, such as the mass market edition of the Hitchhiker's Guide to the Galaxy  
2) Add the Product to an empty Shopping Cart 
3) Verify the price in the Shopping Cart Review page is the same as the product.


To write our first ever test of the ShoppingCart Review Page, we have had a lot of setup to do. Luckily, most of it is already written for the previous six blog entries. We have:

  • Declare the test product to be a single book, the mass market edition of the first book in the Hitchhiker's Guide to the Galaxy.  ( review
  • Grab the username and password to login. 
  • Instantiate the OrdersActions class so we can process the order with methods we set up in the class. ( review )
  • We are going to instantiate the page object called the ShoppingCartReviewPage, which will be designed below. 
  • We use the methods we wrote in the OrderActions page ( review ) to initialize the Login, logging out if we have to. ( review )
  • We navigate to the Home Page and and Login as the username and password. ( review )
  • We initialize the cart to remove any previous test products from the shopping cart. ( review )
  • We grab all data shown on the product page for our test product and load up the product object. ( review ) 
  • We add this test product, a test book, to the Shopping Cart Review page, as shown below. 
Elaborating on our three part test, our test can:
  • Get the actual cart subtotal price shown in the Shopping Cart Review page. 
  • Retrieve the price stored from the book's product page. 
  • Since I like pretty output in my logs, we are going to feed the actual and expected results and see if it matches with a (PASS) or (FAIL). 
  • And finally, we are going to assert that the actual subtotal price in the cart matches what is expected, as shown below.
It's a heck of a lot of setup for one small assertion statement. The payoff will be in the next blog entry where I will be talking about testing multiple products and the tests running in parallel. 

What our Directory Structure Will Look Like


For this blog entry, we will be expanding upon the what we already had, so the directory structure will remain the same, except for a new page object called ShoppingCartReviewPage.

src/test/java
  • actions
    • OrderActions
  • base
    • LoadProperties
  • enums
    • Products
    • Url
  • pages
    • HomePage
    • SignInPage
    • ProductPage
    • ShoppingCartPage
    • ShoppingCartReviewPage
  • pojo
    • Book
  • properties
    • user.properties
  • testcases
    • PurchaseOrderTest
  • utils
    • CommonUtils
    • DriverUtils

The ShoppingCartReviewPage

Remember that our test product we set up in our test is:
 Products testBook = Products.HITCHHIKERS_GUIDE;  

Navigating to the Page


Getting to what I have dubbed the "Shopping Cart Review Page" is easy. All you need to do is:

Go to the page of the product at http://www.amazon.com/Hitchhikers-Guide-Galaxy-Douglas-Adams/dp/0345391802/

... See how the price is $6.00 USD today? It used to be $7.00 USD the day before. Whatever the price is at the time the test is run, we are going to call that $6.00 USD the expected price.

With our test, we are going to instantiate a new book object, call it bookProductPage, and call the method that we wrote that loads up all the relevant test data on the Product Page:

 Book bookProductPage = orderActions.loadProductPageDataIntoProductObject(testBook);  

So, bookProductPage now contains:

  • ProductTitle
  • Author
  • OfferPrice
  • Edition
With future tests, we can compare and contrast the values of this Book object on the product page with other values. For now, we are just going to compare the OfferPrice.

Select the [ Add to Cart ] button to add the product to the shopping cart .


... See how the Cart shows (1 item) and is $6.00 USD? That $6.00 will be the actual price.

With our test, we need to validate that the actual price equals the expected price. We need to Assert that it is Equal.

What the Page Object Looks Like


For page elements, on the ShoppingCartReview page, for this test, we will only need to grab the price of the cart subtotal. We also will need to write two methods:
  • Verify we are on the ShoppingCartReview page after clicking the [ Add to Cart ] button. 
  • Grab the price. 
ShoppingCartReviewPage.java
1:  package pages;  
2:  import org.openqa.selenium.By;  
3:  import org.testng.TestException;  
4:  import utils.CommonUtils;  
5:  /**  
6:   * Created by tmaher on 12/21/2015.  
7:   */  
8:  public class ShoppingCartReviewPage extends CommonUtils {  
9:     private final By PRICE = By.cssSelector("[class='a-color-price hlb-price a-inline-block a-text-bold']");  
10:    public void verifyOnShoppingCartReviewPage(){  
11:      String url = getCurrentURL();  
12:      System.out.println("SHOPPING_CART_REVIEW_PAGE: Verifying that we are on SHOPPING_CART_REVIEW_PAGE.");  
13:      if (!url.contains("view")){  
14:        throw new TestException("ERROR: Not on SHOPPING_CART_REVIEW_PAGE! URL: " + url);  
15:      }  
16:    }  
17:    public String getCartSubtotal(){  
18:      return getElementText(PRICE);  
19:    }  
20:  }  

Retrieve the Actual Price

We already have the test object set up, login and initialization, price retrieval from the product page, we just need to go to the ShoppingCartReview page and retrieve that price, too...

PurchaseOrderTest.java
1:  @Test()  
2:    public void test_createPurchaseOrderForSingleProduct(){  
3:      Products testBook = Products.HITCHHIKERS_GUIDE;  
4:      String username = LoadProperties.user.getProperty("tester23.username");  
5:      String password = LoadProperties.user.getProperty("tester23.password");  
6:      OrderActions orderActions = new OrderActions();  
7:      ShoppingCartReviewPage shoppingCartReviewPage = new ShoppingCartReviewPage();  
8:      orderActions.initializeLogin();  
9:      orderActions.navigateToHomePage();  
10:      orderActions.loginAs(username, password);  
11:      orderActions.initializeCart();  
12:      Book bookProductPage = orderActions.loadProductPageDataIntoProductObject(testBook);  
13:      orderActions.addProductToShoppingCartReview(testBook);  
14:      String actualCartSubtotalPrice = shoppingCartReviewPage.getCartSubtotal();  
15:      String expectedBookPrice = bookProductPage.getOfferPrice();  
16:  }  

Running what we have for the test looks like...
 [TestNG] Running:  
  C:\Users\tmaher\.IdeaIC15\system\temp-testng-customsuite.xml  
 INITIALIZING: Signing out, if needed.  
   
 Navigating to Amazon.com: http://www.amazon.com  
 HOME_PAGE: Selecting [YOUR_ACCOUNT] in navigation bar.  
 HOME_PAGE: Navigating to the SIGNIN_PAGE.  
   
 SIGNIN_PAGE: Entering username: amzn.tester23@gmail.com  
 SIGNIN_PAGE: Entering password.  
 SIGNIN_PAGE: Clicking the [SIGN_IN] button.  
   
 INITIALIZING: Deleting all Items in Cart.  
   
 Starting process to load info for HITCHHIKERS_GUIDE:  
 PRODUCT_PAGE: Navigated to http://www.amazon.com/gp/product/0345391802  
 PRODUCT_PAGE: Verifying that the product title is 'The Hitchhiker's Guide to the Galaxy'  
 LOAD_INFO: Loading data...  
   
 Product Title: The Hitchhiker's Guide to the Galaxy  
 Author: Douglas Adams  
 Edition: Mass Market Paperback  
 Offer Price: $6.00  
   
 Adding HITCHHIKERS_GUIDE to cart:  
 PRODUCT_PAGE: Navigated to http://www.amazon.com/gp/product/0345391802  
 PRODUCT_PAGE: Verifying that the product title is 'The Hitchhiker's Guide to the Galaxy'  
 PRODUCT_PAGE: Clicking on [ADD_TO_CART] button.   
   
 SHOPPING_CART_REVIEW_PAGE: Verifying that we are on SHOPPING_CART_REVIEW_PAGE.  

Compare the actual price with the expected price is simple with TestNG... We can use an AssertEquals statement. If the actual price doesn't match the expected price, the test will fail.


Make Log Output More Readable


Having the test fail is all well and good... but it doesn't match what I would write if I was designing an impromptu test plan. I would prefer to see (PASS) and (FAIL), such as:

TestHeading: Describe what the test is supposed to compare:

  • List the actual value
  • List the expected value
  • Mark if the test (PASS) or (FAIL)
To do this, we can create two methods in OrderActions.java: One checkMatchingValues, and one which has outputPassOrFailOnFieldComparison:

checkMatchingValues
1:   public boolean checkMatchingValues(String testHeading, Object actualValue, Object expectedValue) {  
2:      String successMessage = "\t* The Expected and Actual Values match. (PASS)\n";  
3:      String failureMessage = "\t* The Expected and Actual Values do not match! (FAIL)\n";  
4:    
5:      boolean doesPriceMatch = false;  
6:    
7:      System.out.println(testHeading);  
8:      System.out.println("\t* Expected Value: " + expectedValue);  
9:      System.out.println("\t* Actual Value: " + actualValue);  
10:    
11:      if (actualValue.equals(expectedValue)) {  
12:        System.out.println(successMessage);  
13:        doesPriceMatch = true;  
14:      } else {  
15:        System.out.println(failureMessage);  
16:        doesPriceMatch = false;  
17:      }  
18:      return doesPriceMatch;  
19:    }  
20:  }  

... This method is public. We want to be able to call this in our test every time we compare values, so that it can be outputted to the logs. 

The Final Version of Our Test


With the Assert statement and the output methods, our test now looks like:

test_createPurchaseOrderForSingleProduct()
1:  @Test()  
2:    public void test_createPurchaseOrderForSingleProduct(){  
3:      Products testBook = Products.HITCHHIKERS_GUIDE;  
4:      String username = LoadProperties.user.getProperty("tester23.username");  
5:      String password = LoadProperties.user.getProperty("tester23.password");  
6:      OrderActions orderActions = new OrderActions();  
7:      ShoppingCartReviewPage shoppingCartReviewPage = new ShoppingCartReviewPage();  
8:    
9:      orderActions.initializeLogin();  
10:      orderActions.navigateToHomePage();  
11:      orderActions.loginAs(username, password);  
12:      orderActions.initializeCart();  
13:    
14:      Book bookProductPage = orderActions.loadProductPageDataIntoProductObject(testBook);  
15:      orderActions.addProductToShoppingCartReview(testBook);  
16:      String actualCartSubtotalPrice = shoppingCartReviewPage.getCartSubtotal();  
17:      String expectedBookPrice = bookProductPage.getOfferPrice();  
18:      orderActions.checkMatchingValues("Verify the Price Listed for the book:", actualCartSubtotalPrice, expectedBookPrice);  
19:      assertEquals(actualCartSubtotalPrice, expectedBookPrice, "SHOPPING_CART_REVIEW: Cart Subtotal not what is expected!");  

We are passing in the following values from the test into checkMatchingValues:

  • TestHeading: "Verify the Price Listed for the book:"
  • Actual Value: actualCartTotalPrice
  • Expected Value: expectedBookPrice

If the test passes, the following output will be sent to the logs...

Successful Test Execution:
 SHOPPING_CART_REVIEW_PAGE: Verifying that we are on SHOPPING_CART_REVIEW_PAGE.  
 Verify the Price Listed for the book:  
      * Expected Value: $6.00  
      * Actual Value: $6.00  
      * The Expected and Actual Values match. (PASS)  
   
   
 ===============================================  
 Default Suite  
 Total tests run: 1, Failures: 0, Skips: 0  
 ===============================================  

... If there were failures, not only would the assertion fail, halting the test,

Why Worry About Log Output?

Why worry about the log output? Isn't it enough for the test to pass or fail?

For me, it is not enough. Coming from a manual testing background, I want to see exactly how the test traverses a site. Without seeing the output explicitly spelled out as I have it, it is difficult for me to see the test composition.

Without knowing the test composition, or seeing exactly what values are being compared and contrasted, having a test pass or fail is meaningless to me.

Just because the test claims to have passed or failed, there still could be false positives or false negatives, as it compares and contrasts incorrect data. 

NEXT UP





-T.J. Maher
 Sr. QA Engineer, Fitbit
 Boston, MA

// Automated tester for [ 9 ] month and counting!

Please note: 'Adventures in Automation' is a personal blog about automated testing. It is not an official blog of Fitbit.com

38 comments:

Kailey Hinz said...

Nowadays eCommerce Business is trendy in the emerging world of the latest technology. So starting a business in e-Commerce is a good idea.

markson said...

Very useful article, this explains a lot about Component Integration Test. If you also want to learn about software tests then don’t hesitate to follow the link provided

King Hornbill said...

Very nice article, thank you very much. review finds

sylvia said...

I must thank you for the efforts you have put in penning this site. I am hoping to check out the same high-grade content by you later on as well. Keep up the good work
software testing companies
software testing tester
software testing and quality assurance services
Automation testing services

Vidogarment said...
This comment has been removed by the author.
Vidogarment said...

Perhaps you should also a put a forum site on your blog to increase reader interaction.*::-` Amazon Saving Tricks

arnav sharma said...

Very informative article thanks for share Amazon

Nicholas Olson said...

Thanks for the entire information you have given here to impart knowledge amongst us?
try safe personal alarm reviews

Harsh Patel said...

Very informative.
Regards Tech98

akhilapriya404 said...

Thanks, this is generally helpful.
Still, I followed step-by-step your method in this selenium training

Alia parker said...

I was searching for some quality information on 4 wheel dollies so that i can use it in my kitchen. I am so unhappy with my kitchen because it is so small in size and i can not work properly here. I saw by this dolly i cane easily arrange many things at a single time and can also transfer them easily without wasting any energy.

Bernard Harris said...

Thank you for sharing superb information. Your web site is so cool. I’m impressed by the details that you’ve on this website. It reveals how nicely you perceive this subject. Bookmarked this website page, will come back for more articles. Speakers Black Friday Deals

tike mik said...

But if you are fitter than your opponent who have just out-dribbled you, you will be able to track back fast enough to cover your position again. dart board with cabinet

John smith said...

I am hoping the same best effort from you in the future as well. In fact your creative writing skills has inspired me. cheap essay writer service

John smith said...

I am hoping the same best effort from you in the future as well. In fact your creative writing skills has inspired me. cheap essay writer service

123456789 said...

When there are bargains in a neighborhood store, you can be certain that there will be a group to fight with. Discovering bargains online spares you from managing the groups and cerebral pains brought about by the groups. best amazon uk deals

Watson said...
This comment has been removed by the author.
Watson said...


This is really a nice and informative, containing all information and also has a great impact on the new technology. Check it out here:CBD oil from Amazon

Wild Gearz said...

I don’t know if it’s just me or if perhaps everyone else encountering problems with your blog. It seems like some of the written text within your content are running off the screen. Can someone else please provide feedback and let me know if this is happening to them as well? This could be a problem with my internet browser because I’ve had this happen previously. Many thanks kevin david amazon

Online Shopping At Smile Bazar said...

SmileBazar is an online shopping site offering made in India Products. Buy Duffel Bag online at lowest prices. Hope you enjoy discovering new products.

Kamal Singh said...

Thank you for sharing superb information. I am hoping for the same best effort from you in the future as well. This is really nice and informative, containing all information and also has a great impact on the new technology.

India Best Automation Testing Company
India Best Software Testing Services

mikethomson said...

I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article. Mua Tài khoản Grammarly Premium

Phan Thương said...

Hello everyone, We are Children's Furniture – Bao An Kids. Surely when you come to this page, you are looking for a professional children's interior design and construction unit to help you advise and choose for your family and baby to have a perfect and comfortable living space. the safest. Thank you very much for trusting and wanting to accompany Bao An Kids. nội thất trẻ em

quatang24k said...


Bài viết rất hay! Quà tặng 24k chuyên cung cấp:

Đặt cúp đồng giá rẻ tại Hà Nội

Quà tặng vinh danh ở đâu chất lượng và uy tín

Hotline: 0866 866 711

Phan Thương said...

Tan Binh valve has many years of experience in the field of industrial valves. Production meets DIN, BS, JIS standards… Tan Binh After more than 10 years of supplying materials for the water industry Our company is now a reliable partner for most companies providing water treatment systems in the local area. Hanoi as well as nationwide. van công nghiệp, Van cửa, van điện từ

congdu1990 said...

Xe nâng Đông Đô là địa chỉ chuyên: Thuê xe nâng người tại KCN Bình Xuyên; Cho thuê xe nâng hàng tại KCN Biên Hòa; Cho thuê xe nâng hàng tại KCN Biên Hòa; Xe nâng hàng tại KCN Biên Hòa uy tín, giá rẻ nhất trên thị trường.

thicongnoithattrongoi said...


Bài viết bạn rất hay! Công ty cổ phần nội thất KFA hoạt động trong lĩnh vực thiết kế kiến trúc, nội thất. gợi ý cho bạn:

Bí quyết để có một không gian nội thất đẹp

Khám phá chi phí thi công nội thất trọn gói bao nhiêu?

Liên hệ để được tư vấn: ĐT: 0987.316.777

Phan Thương said...

Our company specializes in manufacturing and executing works of aluminum and glass, tempered glass, rolling doors ... with a commitment to the construction team of over 5 years of experience, genuine products, long-term warranty.More: Cửa nhôm xingfa, cầu thang kính cường lực

Phan Thương said...

Today, the demand for modern sanitary products and equipment with preeminent features is increasing day by day. That's why many sanitary ware companies were born. To meet the essential needs of our customers. So what are the advantages of today's sanitary ware products? Let's find out detailed information with the Great World of Lights Showroom through the article below. More: https://bitly.com.vn/hfn52q

maynguyen.hn92@gmail.com said...

Nội thất Kfa đã có trên 10 năm kinh nghiệm trong thiết kế và thi công nội thất, được hàng nghìn khách hàng tin tưởng - đem đến sự hài lòng cho cả những khách hàng khó tính nhất
Hé lộ cách treo đèn phòng khách theo phong thủy đón tài lộc và may mắn
Top 5 mẫu cửa gỗ tự nhiên đẹp nhất
Bí quyết thiết kế quán café nhỏ vạn người mê
Màu sắc có vai trò gì trong thiết kế nội thất phòng ngủ
Phong cách tropical trong thiết kế nội thất

beobeoyeu068@gmail.com said...

Nhân Sâm Trường Thọ chuyên cung cấp các sản phẩm chính hãng chất lượng:

Trà hồng sâm Hàn Quốc

Hồng sâm Hàn Quốc 6 năm tuổi

Sản phẩm nhân sâm nhập khẩu trực tiếp từ Hàn Quốc có chất lượng đảm bảo, bao bì đóng gói đẹp mắt, sang trọng, vô cùng thích hợp để làm quà biếu, quà tặng.

Địa chỉ: 853 - 855 Nguyễn Kiệm, P.3, Q.Gò Vấp, TP.HCM.

Tel: +84 (28) 350.66666

Hotline: 090.77.999.88

phannhathoang144@gmail.com said...


Công ty Cổ Phần Công Nghệ và Giải Pháp EMS cung cấp :

Điều hòa chính xác

hệ thống nguồn một chiều

Điều hoà chính xác Liebert PEX

Liên Lạc theo SĐT: +84 24 7306 6226; +84 24 6687 4507

maynguyen.hn92@gmail.com said...

Nội thất Kfa đã có trên 10 năm kinh nghiệm trong thiết kế và thi công nội thất, được hàng nghìn khách hàng tin tưởng - đem đến sự hài lòng cho cả những khách hàng khó tính nhất
Phong cách tropical trong thiết kế nội thất
Phong cách tropical là gì? Xem chi tiết ngay
Vách ngăn phòng khách là gì
Có Nên Sử Dụng Thảm Phòng Khách Hay Không

phannhathoang144@gmail.com said...

Máy tính Hải long là địa chỉ bán máy tính cũ uy tín chất lượng, có đội ngũ tư vấn chuyên sâu, am hiểu tường tận về từng chi tiết của máy tính. Đặc biệt, thời gian bảo hành cũng như chính sách đổi trả, không kém việc bạn mua máy mới 100% tại các siêu thị lớn.
Địa chỉ: Số 9 ngách 109 ngõ 69a Hoàng Văn Thái, Thanh Xuân, Hà Nội
Điện thoại: 0972 105 943

phannhathoang144@gmail.com said...

Bài viết rất hay: Chúng tôi chuyên cung cấp các sản phẩm chất lượng


Giảo cổ lam giá rẻ tại Hà Nội

Bao nhiêu tiền 1 kg giảo cổ lam

nên mua điện thoại nào said...

I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article. 4 trieu nen mua dien thoai nao

dogotamlinh said...


Công ty Tất Phú là nhà phân phối và trực tiếp nhập khẩu tấm xi măng CEMBOARD từ các hãng sản xuất uy tín, chất lượng từ nhiều nước khác nhau. Nắm trong tay gần 10 năm kinh nghiệm trong lĩnh vực cung cấp các dòng xi măng CEMBOARD chất lượng.

Báo giá tấm cemboard

Cung cấp tấm cemboard thái lan

Địa chỉ: Tầng 3, tòa nhà Shodex, số 245 Tam Trinh, Hoàng Mai, Hà Nội

Điện thoại: 024 66604646 – 0977 688 695

Phan Thương said...

Hung was formerly an official distributor of industrial lubricants of Shell in the North. Currently, in addition to oil trading, we also trade in transportation and equipment trading. After nearly 12 years of establishment and development, Yen Hung is now a prestigious partner of nearly 10,000 large and small domestic and international factories. Main products:
giá dầu truyền nhiệt
dầu bánh răng
dầu tuần hoàn
dầu dẫn nhiệt
dầu thủy lực shell
mỡ bò bôi trơn chịu nhiệt