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.

  • 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. 
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...

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:  
 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.  
 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.   

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:

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";  
5:      boolean doesPriceMatch = false;  
7:      System.out.println(testHeading);  
8:      System.out.println("\t* Expected Value: " + expectedValue);  
9:      System.out.println("\t* Actual Value: " + actualValue);  
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:

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();  
9:      orderActions.initializeLogin();  
10:      orderActions.navigateToHomePage();  
11:      orderActions.loginAs(username, password);  
12:      orderActions.initializeCart();  
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:
 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. 


-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


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