In the latest installment of our Cucumber Automation Framework series, learn how to make tests maintainable with PageObjects.
Blog 10 of a series.
As the number of Scenarios in your project increases, you begin to notice that numerous steps are manipulating the same web object. At first, this doesn’t bother you – until the development team adopts a new naming standard and all of your step definitions need to be reworked.
Wouldn’t it be nice if there was a single place to put each of your objects unique identifiers so you have a single place to maintain it? There is! And it can be created using the PageObject gem.
To demonstrate PageObject, I am going to convert the BDD Cucumber framework created in previous blogs from a WATiR WebDriver-based framework to a PageObjects-based framework.
One thing that makes Cucumber so powerful is that in order to do this conversion, none of our scripts need to be changed. All the changes will occur under the hood.
Our BDD Gherkin script was as follows:
Using WATiR-WebDriver and effective Cucumber step parameters, we were able to quickly get this script to green. Now we need to make it scalable and maintainable using PageObjects.
Before we jump into the code we need to ensure the env.rb file is configured properly. When we created the project using TestGen, it created env.rb with four lines in it:
I’m not going to get into the details of what each line is all about. Except to say that if your env.rb file has these lines, the PageObject gem is now available for us to use globally.
In our first step, we navigate to Google’s homepage. PageObject accomplishes this with the visit_page(class object) command. With this command, the browser will visit the URL specified in the class object specified. Our visit_page will look like this:
If you run the .feature file now, you will get an error message telling us what to do next:
We need to create the GoogleHomePage class object. To do this, right-click on the features/support/pages directory -> New -> Ruby Class. At the prompt, type “GoogleHomePage” and hit Enter. The class object GoogleHomePage is created. All you need to do is add “include PageObject”:
When visit_page is called, it will go to the class object specified and look for the page_url method. Since there is no page_url defined in GoogleHomePage, the browser has nowhere to go. We need to add it:
Now you can run the .feature file. You should be taken to GoogleHomePage.
Next we need to search for “Centric Software Quality Assurance and Testing”. This is accomplished using the on(class object).method command.
Again it is helpful to run the .feature file and let Cucumber tell us what to do next:
We need to define ‘search_text’ and ‘search_button’ in the GoogleHomePage class like this:
Running the .feature file at this point shows our agile testing team is making progress:
For the third step, “Then I will see “Software Quality Assurance and Testing | Centric” listed as the first search result” and the fifth step “Then I will be on “Software Quality Assurance and Testing” page” we are going to use the WATiR-WebDriver include? command we used in the WATiR-WebDriver blog:
The last step we have to define is “When I Click the link”. Searching for Centric Software Quality Assurance and Testing yielded a lot of results. The first result is the one we are looking for and want to click on. Google shows this has an h3 element:
Run the .feature file and you should see all green:
What makes PageObject so powerful is that if the definition of an object changes, we now only have to go to one place to update it. Our Cucumber framework is much more maintainable.
For more information on PageObject, I would encourage you to go to the PageObject GitHub site.