Introducing thewaiter. A WebDriverWait based library for writing reliable Selenium tests

During my talk at the recent SauceCon conference, i described how you can write reliable Selenium tests by using WebDriverWait based methods to wait for page events to take place. I am now happy to announce that my little project, thewaiter library that i mentioned in the talk, is available to use from the Maven Repository! Read on for details.

Introducing thewaiter

The library is made up of just one class that holds a ton of wait based methods, to help you design reliable and shorter Selenium tests. I will write several blog posts describing the purpose of those methods, but just to give you an idea, the methods included in the library will help you wait for:

  • page load complete
  • URLs (to equal or contain a String, taking the case into account or not)
  • a WebElement to be clickable and to actually click on it
  • a WebElement text to equal/contain an expected String, ignoring or taking into account the case or whitespaces
  • a WebElement attribute’s value to equal/contain an expected String, ignoring or taking into account the case or whitespaces
  • some click and wait methods are also present, like clicking on a WebElement and waiting for a URL (to equal or contain an expected String)
  • and so on

And this is just in the first version of the library! Further releases will bring even more goodness.

All the methods that are available are well documented, so don’t hesitate to read the Javadoc once you imported the library into your project.

Where to find the library

Version 1.0 of the library was just released to the Maven Repository. You can find it here:

https://mvnrepository.com/artifact/com.imalittletester/thewaiter/1.0

The GitHub repo of the project (and the place where you will find some documentation) is:

https://github.com/iamalittletester/thewaiter

Importing thewaiter into your project

In order to import the library into your Maven project, just add the following entry in your pom.xml file, in the dependencies section:

<dependency>
     <groupId>com.imalittletester</groupId>
     <artifactId>thewaiter</artifactId>
     <version>1.0</version>
     <exclusions>
       <exclusion>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
       </exclusion>
     </exclusions>
 </dependency>

The waiter library has Selenium as a dependency, so in order to avoid dependency management issues, the “exclusion” section needs to be added. This way, you can control from your own dependency the Selenium version you want to import into your project.

Using Waiter in your test

After you imported the library as a dependency, you can start creating the tests that will use it. In the class where you will use it, you first need to import the Waiter class, as follows:

import waiter.Waiter;

You also need to instantiate the Waiter class:

private Waiter waiter = new Waiter();

In the test, you just need to call the method that you want from the Waiter class, with the corresponding parameters. Some examples:

waiter.get(theUrl, theDriverInstance);

waiter.waitForElementTextEqualsString(webElement, expectedString, theDriverInstance);

waiter.waitForElementAttributeContainsString(webElement, theAttribute, expectedString, theDriverInstance, 10);

waiter.click(webElement, theDriverInstance);
 
waiter.clickElementAndWaitForUrlContains(webElement, expectedString, theDriverInstance);

Note that for methods that take a WebElement as parameter, you need to pass it as a PageObject variable, not as “driver.findElement(…)” because the latter will not work.

Further reading

I will write quite a few posts on the blog to exemplify the usage of the methods, so stay tuned. Also, the examples will be uploaded to another of my GitHub projects, here:

https://github.com/iamalittletester/learning-project/tree/master/src/test/java/waitertests.

You can also take a look at the talk in which i describe how to use the wait methods to write reliable tests:

https://www.youtube.com/watch?v=Ap4lmIiCCnk.

Enjoy!

2 thoughts on “Introducing thewaiter. A WebDriverWait based library for writing reliable Selenium tests”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.