Category Archives: automation

waiter2: My new Selenium wait based library

Some years ago, at a conference, I announced that I released a Selenium based library that contained useful wait-based methods for test automation. After a while I archived that project as I did not find time to work on it too much. Well, now, years later, having gathered new ideas, having a refined vision of what that library should be, and with some fresh energy, I am releasing the, let’s say, V2 version of it. In fact it is a brand new library, bigger and better, and it’s name is: waiter2.

I wanted to specify the ‘2’ in the name to highlight that this is based on the original idea (‘thewaiter’) but it is an improved concept. I kept the ‘waiter’ name as the library contains mostly methods that use Selenium’s WebDriverWait mechanism to ensure reliability in tests. It is a Maven based Java project with a Selenium dependency.

The What

I will be making several small releases that will contain the new methods I am interested in, instead of a single large release. I will track progress of that here, on the blog. Each set of new methods released will have an associated blog post where I will describe what is new, and how to use the newly released methods. I will also specify the library version in which a feature will be available. Additionally, the code will be well documented through Javadoc.

So what functionality will I cover with the new methods? Well, you will have the possibility to click on items, work with checkboxes or dropdowns, read element texts or attribute values, work with iframes or multiple windows. Basically whatever I will consider useful, I will add to the library.

Importing into your project

The library will be available for download from the Maven Repository website, at: https://mvnrepository.com/artifact/com.imalittletester/waiter2. It is recommended to always use the latest available version, since it always contains the most features. Add the dependency as it is shown in mvnrepository to your project’s pom.xml file, in the <dependencies> section. E.g.:

<dependency>
    <groupId>com.imalittletester</groupId>
    <artifactId>waiter2</artifactId>
    <version>LATEST-VERSION</version>
</dependency>

This library has as dependency the latest released version of Selenium, which means you don’t need to separately import it. However if you want to manage which Selenium version is imported into your project, you will need to exclude it from the ‘waiter2’ dependency, using an ‘exclusions’ section.

Library contents

There is only one class in the library, called Waiter, in the main -> Java -> utils package. It contains a field, named ‘driver’, and a constructor. It takes the driver instance from a test that initializes the Waiter class and sets it to the Waiter class field. This way you can call all methods from the Waiter class without having to pass the driver instance to each method. The driver instance is used inside the wait methods.

Most of the methods I implemented in the library use the WebDriverWait functionality. Initially, a WebDriverWait variable is created, to define up to how many seconds to wait for the condition specified in the method to happen. Then, the condition is defined. Every half a second the condition is retried, until it is fulfilled successfully, or the timeout has elapsed.

In the same Waiter class you can find a few constant values, whose names contain ‘TIMEOUT’. These are used either inside the methods to set for how long a method waits for a condition to be true, or they can be used by you in tests when calling methods that take a timeout value as a parameter. Each wait method comes in 2 variants: one without a timeout parameter and one with a parameter which specifies a timeout value. The TIMEOUT constants are ints as follows: TINY_TIMEOUT of 10 (corresponding to 10 seconds when passed as parameter to wait methods), TIMEOUT of 30 (the default value unless you provide a timeout parameter), MEDIUM_TIMEOUT of 60 and LONG_TIMEOUT of 120. I did not provide larger numbers, since waiting for more than 2 minutes for any UI event is simply too much.

When it comes to methods that require page elements to be specified as parameters, these also come in 2 flavours: methods that take a WebElement specified through a @FindBy annotation, and the corresponding methods that take a By item.

The methods are written so that if there is a failure due to a condition not being met within the timeout period, instead of a TimeoutException you will get a RunTimeException, with a concise message regarding the failure. This makes it easier and faster to figure out, by looking at a test failure, why the test failed.

The GitHub location of the library is: https://github.com/iamalittletester/waiter2.

Test setup

In order to call the methods from the ‘waiter2’ library, I recommend having a dedicated variable in the test class. I would call it ‘waiter’. In order to initialize the Waiter class (from the ‘waiter2’ library), a driver instance needs to exist. It needs to be passed as parameter to the constructor of the Waiter class. My preferred setup would include initializing the driver instance in the @BeforeAll method (or @BeforeClass, depending on whether you use JUnit5 or TestNG), then initializing the ‘waiter’ variable:

waiter = new Waiter(driver); 

In a nutshell, this is what the ‘waiter2’ library is about. In the next posts I will discuss the methods I implemented in it, and how to use them, so stay tuned!

My articles published in September

This September was quite productive, as i had 4 test automation related articles published, which i recommend reading:

Enjoy.

My blog posts on Tech buzzwords testers should at least know about have been published!

Checkout my series of two blog posts on Tech buzzwords testers should at least know about: https://blog.testproject.io/2021/07/08/tech-buzzwords-testers-should-at-least-know-about-part-1/ and https://blog.testproject.io/2021/07/29/tech-buzzwords-testers-should-at-least-know-about-2/. With lots of useful links for in-depth learning. Enjoy.

My series of Maven related articles is out now

If you haven’t seen, this week i released my series of 2 articles to help get you started with Maven. In the first one, i discuss how to setup Maven on your machine and how to create a brand new Maven project. I also explain key concepts that define what Maven and a Maven project are (like dependencies). Check out this article here: https://blog.testproject.io/2021/06/28/getting-started-with-maven-part-1/.

In the second installment in this series, i further discuss some specifics about dependencies. Then, i go over project goals, how you can run your tests, and how you can create profiles and use system properties in your test run. Check it out here: https://blog.testproject.io/2021/06/28/getting-started-with-maven-part-2/. Enjoy.

Logging Test Automation Information with Log4j

This week my newest article was released, this time on how to use Log4j to log relevant test automation information: https://blog.testproject.io/2021/05/05/logging-test-automation-information-with-log4j/. Check it out!

Using Retries in tests can hide the bugs

We are quite familiar with the concept of randomly failing automated tests. Those are the tests that even though there is no change in the feature they are testing, they either fail randomly at the same step, or they fail at random steps. Handling the results of such tests can be tricky, and some teams choose to simply retry a test if it failed. But is that the best option? Here are my thoughts. Continue reading Using Retries in tests can hide the bugs

The weird true story of Selenium, the StaleElementReferenceException, the iframe, the List and the WebElement

I write a lot of automated tests. Most of the times, it all goes nice and smooth, like a good song. But once in a while i run into an automation situation that leaves me completely baffled. Debugging does not reveal how to fix it, and it is not very obvious to me what is going on. When do i realize what the issue was, i’m like ‘WOW i did not expect that’. These kind of stories are good to share, so that you know what to expect, should you encounter the same behavior. So, here is the peculiar, true story of Selenium, the StaleElementReferenceException, the iframe, the List and the WebElement. Continue reading The weird true story of Selenium, the StaleElementReferenceException, the iframe, the List and the WebElement

About my JUnit 5 course

As you have probably seen, my new course on JUnit 5 is now available at Test Automation University. It’s an introduction to all the amazing features of this framework that you can use in your automated tests, with, of course, plenty of examples.

Continue reading About my JUnit 5 course

Have you seen my article on using Spring for managing test environments in automated tests?

Read all about how to configure your test environment specific data in property files with Spring, to help run your automated TestNG and JUnit tests on any test environment you need: https://blog.testproject.io/2021/02/09/using-spring-to-switch-environments-in-automated-tests/. Enjoy.

Read my new article on Selenium Actions

My latest article is now published, and it’s all about using Selenium Actions for more interesting and complex interactions, like mouse double clicking, mouse right clicking or mouse hovering. Read all of it and follow the examples i present right here:  https://blog.testproject.io/2021/01/29/performing-page-interactions-with-selenium-actions/ . Enjoy!