TL;DR
In chapter five, we implement a simple scenario with Given/When/Then steps for adding a specific shirt to the shopping cart.
In this pull request, you can see all the files involved in chapter 05.
features/shop.feature
This is a cucumber feature file with one scenario that uses three steps:
- Given have already been implemented in home steps.
- When is a new step, implemented in shirt steps.
- Then is a new step, implemented in cart steps.
features/step_definitions/cart.rb
Here we implement the cart steps. Then step, where we expect that item_name text value is equal to shirt name.
features/step_definitions/shirt.rb
In this step, we click on the item element that has a shirt name Dark Thug Blue-Navy T-Shirt. Note that we use an unpopular sleep method, with hardcoded one second.
features/support/cart.rb
This is class for a cart page objects. It has one paragraph p attribute with long XPath. got Xpath value automatically, using Chrome developers tools copy Xpath option. We p attribute is the method implemented in page object accessors. p attribute generates three methods: item_name, item_name_element, and item_name? We use item_name, that returns HTML p element text value.
features/support/shirt.rb
Class for shirt page object with one div element, located using long XPath. div page object accessors implements three methods: add_to_cart, add_to_cart_element and add_to_cart? We use the add_to_cart_element method that returns the Watir div object. That object has a click method.
Now you can run:
cucumber features/shop.feature
The test should pass.
To do
In the next chapter, we will replace sleep with the wait method. Using sleep is not a good option because the sleep method is run only once. wait for method implements under the hood conditional loop, where condition waits for page element to become present/visible. Long Xpath locator will be replaced with the adjacent nodes method. Long Xpath is breakable when elements are inserted along the path. The longer the path, the greater the chance that XPath would become obsolete. Adjacent nodes are more robust to page element changes because we locate an element using adjacent HTML elements that are located using more stable locators (for example name, text or id).