Please post your Web Driver questions in official Web Driver forum

Monday, November 28, 2016

How to remove duplicate page object methods

You may come across situation when you find page object methods on various classes having similar operations. For ex in following example there are two different different methods doing almost same thing. Both methods get the count of ticket in opening and outgoing queue in a system. The only difference is the element locator. One method uses element locator openingTicketCount while other uses outgoingTicketCount -

public int getOpeningTicketCount() {
   final String count = fluent(openingTicketCount).getText();
   return count.length() == 0 ? 0 : Integer.valueOf(count);
}

public int getOutgoingTicketCount() {
   final String count = fluent(outgoingTicketCount).getText();
   return count.length() == 0 ? 0 : Integer.valueOf(count);
}

We can extract the common steps and create a new private method to encapsulate them - 

private int getTicketCount(By locator) {
   final String count = fluent(locator).getText();
   return count.length() == 0 ? 0 : Integer.valueOf(count);
}

And previous page object methods can call this method with required element locator - 

public int getOpeningTicketCount() {
   return getTicketCount(openingTicketCount);
}

public int getOutgoingTicketCount() {
   return getTicketCount(outgoingTicketCount);
}

What do you think of this approach? How would you handle duplicate element locators?

Monday, November 21, 2016

What else should page object test?

As we know that objective of page object is to provide services offered by page (or part of it). In its true sense page object does not advocate asserting page condition in page object class. Except the page object constructor which can check if control is on right page on it, like following -


Screenshot from 2016-06-06 15:12:12.png


But this is not the only use case of a check on page object. When returning data set from page object one could also check if data set is empty, for ex following method checks if collection of webelement is empty using Preconditions.checkState() method before returning it’s content -


Screenshot from 2016-06-06 15:16:36.png


In the above code snippet, if element list is empty then page object method would throw IllegalStateException exception. This check could also be carried out on test method which receives the member name list. But I prefer to keep such checks on page object method and focus on assertion on test method. What do you think of this approach? Do you also have such checks in your page object methods?

Saturday, November 12, 2016

Using chrome console to test xPath and css selectors

Since the advent of selenium there have been many plugin to test xPath / css selectors but you don’t need any of them if you have chrome browser. Using Chrome console you can test both xPath and css selectors. Launch website to be tested in chrome browser and hit F-12 and you would see chrome console opened in lower pane of application -


Screenshot from 2016-07-27 13:35:38.png

Hit escape key and console would open another pane to write element locators -


Screenshot from 2016-07-27 13:37:30.png

And now you can start writing xPath or css selectors in chrome console and test them -
The syntax for writing css id - $$(“”)


Screenshot from 2016-07-27 13:43:27.png


And hit the enter key. If your expression is right then html snippet of the application element corresponding to the css selector would be displayed -


Screenshot from 2016-07-27 13:45:11.png


If you mouse over the html snippet in chrome console then it would highlight the corresponding element in application -

Screenshot from 2016-07-27 13:47:51.png

If you want to clean console of previously written element selectors then just hit ctrl+L keys and chrome console would be empty again.


Process of testing xPath selector is same as css selector except that syntax to test xPath is -


$x(“your_xpath_selector”)


Screenshot from 2016-07-27 13:55:04.png

So no more using any plugin to test element locators :-)

Is there any other innovative way you use to test your element locator?

Monday, November 7, 2016

Structure of test automation project

Have you come across situation when you did not know where you should keep the test class, page object or any supporting class. I am going to share some of the practices I follow when working on test automation project. Given that I work with java and maven, it solves the problem of the directory structure. Maven project may have many directory but these are the ones I use most -

src/main/java - any non test class, i.e - page object etc
src/main/resources - resources like - properties file, text file etc used by main classes
src/test/java - the test classes
src/test/resources - resources like - properties file, text file etc used by test classes.
target - this directory is created automatically building a maven project and

Let’s focus on src/main/java - this is a directory which contains all of non test code. I usually end up having following structure on src/main/java -

src/main/java
com.google
core
dataobjects
groups
model
navigation
pageobject
util

Hence it looks as -

Screenshot from 2016-07-27 10:16:07.png
Package core contains SelTestCase class which takes care of setup and teardown. Given that I use STF, I don’t have to set up browser myself and setup method is mere access to application URL, test environment etc

Screenshot from 2016-07-27 10:21:45.png

Package dataobjects contains the test data objects used by the test classes -

Screenshot from 2016-07-27 10:32:41.png

Package groups contains TestNG test group. It is set of classes with String constants so that I can easily change the group name by modifying the String constants.

Package model contains the API definition which are used by either test classes or page objects -

Screenshot from 2016-07-27 10:34:56.png

Package navigation contains the Navigation APIs for navigating through application pages. Hence navigation steps don’t have to be repeated in test classes or page objects -

Screenshot from 2016-07-27 10:40:02.png

Package pageobject does not need any introduction :-). I further divide pageobject package into application areas for which page objects are to be created -

Screenshot from 2016-07-27 10:45:12.png

I further divide /resources package into application environment related entities. I set up environment parameter in testng.xml file and depending on the environment corresponding data set is picked up for test run -

Screenshot from 2016-07-27 10:49:45.png

The test package contains the test classes. Like the page object, I created various packages to contain test classes of application area -

Screenshot from 2016-07-27 10:54:30.png

At times I also create util package containing utility classes. Though I would not recommend this approach as util package tends to become dumping ground of unrelated classes. A better approach would be to create dedicated packages for those classes.

This brings us end to this post. What do you think of this package structure? How does your test structure look like?
Fork me on GitHub