Verifying the shopping cart functionality

In order to verify the shopping cart functionality, we must be able to add a product multiple times to the shopping cart, check that the product exists in the shopping cart with the proper quantity of the product displayed, and have the ability to remove the product from the shopping cart. Therefore, there are 3 expected tests that we will need in the shopping cart test suite.

The main function in the shoppingcart_test.go source file, located in the client/tests/go directory, implements the shopping cart test suite:

func main() {

viewportParams := &caspertest.ViewportParams{Object:
js.Global.Get("Object").New()}
viewportParams.Width = 1440
viewportParams.Height = 960
casper.Get("options").Set("viewportSize", viewportParams)

casper.Get("test").Call("begin", "Shopping Cart Test Suite", 3,
func(test *js.Object) {
casper.Call("start", "http://localhost:8080/products", wait)
})

Inside the main function, we set the web browser's viewport size. We start a new test suite by calling the begin method on the casper object. Note that we have indicated that there are 3 expected tests in this test suite. Inside the then callback function that constitutes the last argument to the begin method, we call the start method of the casper object, providing the URL to the products listing page, and providing the JavaScript wait function, as the then callback function. This will cause the program to wait until the primary content div container has loaded in the DOM, before conducting any tests.

With the following code, we add three Swiss Army Knives to the shopping cart:

  for i := 0; i < 3; i++ {
casper.Call("then", func() {
casper.Call("click", ".addToCartButton:first-child")
})
}

Notice that we have passed the click method of the casper object with the CSS selector ".addToCartButton:first-child", to ensure that the Swiss Army Knife product is clicked on, since it is the first product displayed on the products listing page.

In order to verify that the Swiss Army Knives were properly placed in the shopping cart, we need to navigate to the shopping cart page:

  casper.Call("then", func() {
casper.Call("click", "a[href^='/shopping-cart']")
})

Our first test consists of verifying that the correct product type exists in the shopping cart:

  casper.Call("wait", 207, func() {
casper.Get("test").Call("assertTextExists", "Swiss Army Knife", "Display correct product in shopping cart.")
})

We do this by checking that the "Swiss Army Knife" text exists on the shopping cart page by calling the assertTextExists method on the tester module object and providing an expected text value of "Swiss Army Knife".

Our second test consists of verifying that the correct product quantity exists on the Shopping Cart page:

  casper.Call("wait", 93, func() {
casper.Get("test").Call("assertTextExists", "Quantity: 3", "Display
correct product quantity in shopping cart.")
})

Again, we call the tester module object's assertTextExists method, passing in the expected text, "Quantity: 3".

We generate a screenshot of the shopping cart, and this screenshot (shown in Figure 10.14) should display the Swiss Army Knife with a quantity value of 3:

  casper.Call("wait", 450, func() {
casper.Call("capture", "screenshots/shoppingcart_test_add_item.png")
})

Our last test consists of removing an item from the shopping cart. We remove the product from the shopping cart with the following code:

  casper.Call("then", func() {
casper.Call("click", ".removeFromCartButton:first-child")
})

In order to verify that the product was successfully removed from the shopping cart, we need to check if the message indicating that the shopping cart is empty, exists on the Shopping Cart page:

  casper.Call("wait", 5004, func() {
casper.Call("capture", "screenshots/shoppingcart_test_empty.png")
casper.Get("test").Call("assertTextExists", "Your shopping cart is
empty.", "Empty the shopping cart.")
})

Notice that in our call to the tester module object's assertTextExists method, we check to see whether the "Your shopping cart is empty." text exists on the web page. Prior to this, we also generate a screenshot image (shown in Figure 10.15), which will show us the shopping cart in the empty state.

Finally, we will signify the end of the shopping cart test suite with the following code:

  casper.Call("run", func() {
casper.Get("test").Call("done")
})

We can run the CasperJS tests for the shopping cart test suite by issuing the following command:

$ casperjs test js/shoppingcart_test.js

Figure 10.13 shows a screenshot of the result of running the shopping cart test suite:

Figure 10.13: Running the shopping cart test suite

Figure 10.14 shows the generated screenshot showing the test case, where 3 Swiss Army Knives have been successfully added to the shopping cart:

Figure 10.14: Test case to add a product multiple times to the shopping cart

Figure 10.15 shows the generated screenshot showing the test case, where the Swiss Army Knife product has been removed, thus emptying the shopping cart:

Figure 10.15: Test to verify emptying the shopping cart

Now that we've verified the functionality for the shopping cart, let's look into testing the live chat feature.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.48.204