Automating management of Selenium webdriver binaries with Java
Manual setup of testing environment for Selenium can be tiresome. Installed browser version and Selenium webdriver binary version need to be in sync. When you need to do it on multiple machines, each with different OS, it takes a lot of time and effort.
There is a nice javascript tool webdriver-manager that is useful if you can install node.js on your testing environment, but it’s useless if you only have Java available.
At first I wrote custom Gradle build scripts. It took a lot of effort to maintain them and they were useless on projects that used Maven. Then I found one awesome Java project that solves the problem. It’s Boni Garcia’s Webdriver manager.
The project is actively maintained and has excellent documentation. WebdriverManager can be used as a standalone server that fetches Selenium webdriver binaries, or you can include it as a dependency in your project and use it directly in your test code.
Selenium test example
We will create a simple Junit test with Selenium. We will use Gradle as our build tool.
Prerequisites
- Java JDK 1.8.X
- Gradle 5.X
- Firefox browser
Test overview
- fetch the right Firefox driver binary if needed
- create instance of Firefox webdriver
- visit Google search page with Firefox browser and do a search for AG04
- Verify that first result is “Agency 04”
- close browser after test
Project setup
We need to create a new directory for our project. We will create a simple gradle project in it that uses Groovy as build script DSL in it by invoking gradle init command.
mkdir webdriver-manager
cd webdriver-manager
gradle init
Than we will add following code to gradle.build script
First test implementation
We’ll create a directory for com.ag04 test package.
mkdir -p src/test/java/com/ag04
There we will create out first test class.
touch src/test/java/com/ag04/GoogleTestExample.java
We’ll add the following Java code to GoogleTestExample class.
We are using standard Junit annotations. WebdriverManager does its magic in a method annotated with @BeforeClass.
Annotated with @Before and @After are setup and teardown of webdriver instance.
We start test by typing:
./gradlew clean test
We can see what the WebdriverManager does in the Gradle log output if we add the info parameter when we invoke the gradle test task.
./gradlew clean test --info
Page objects pattern
A common pattern used in browser automation is Page objects pattern.
We will create a new directory for com.ag04.pages package.
mkdir src/test/java/com/ag04/pages
Next, we need two new classes for the search page and the results page.
touch src/test/java/com/ag04/pages/HomePage.javatouch src/test/java/com/ag04/pages/ResultsPage.java
We will add the following code to these classes.
LoadableComponent
We extended LoadableComponent with our page objects so we get a standardized way of loading pages. See Selenium documentation for more info about LoadableComponent.
PageFactory
Selenium provides a nice @FindBy annotation for WebElements which provides improved readability of page objects. For annotations to work we need to use PageFactory to initialize WebElements. Check PageFactory in Selenium docs for detailed examples.
We will make changes to GoogleTestExample class to use page objects.
Reports
When we invoke the Gradle test task with:
./gradlew clean test
There is a default Gradle HTML report generated in directory
build/reports/tests/test/
Source
Source for this example project can be found on GitHub.