Enterprise Java

JUnit 5 and Selenium – Setup the project with Gradle, JUnit 5 and Jupiter Selenium

Selenium is a set of tools and libraries supporting browser automation and it is mainly used for web applications testing. One of the Selenium’s components is a Selenium WebDriver that provides client library, the JSON wire protocol (protocol to communicate with the browser drivers) and browser drivers. One of the main advantages of Selenium WebDriver is that it supported by all major programming languages and it can run on all major operating systems.

In this tutorial I will go through the setup of the test automation project for the popular TodoMVC application using Gradle with Java, JUnit 5 and Selenium Jupiter. You will learn about Selenium’s PageFactory to implement Page Object pattern. You will also learn about parallel test execution, test execution order, parameterized tests and much more.

About this Tutorial

You are reading the first part of the JUnit 5 with Selenium WebDriver – Tutorial.

All articles in this tutorial:

  • Setup the project from the ground up – Gradle with JUnit 5 and Jupiter Selenium

Coming up next:

  • Using Selenium built-in PageFactory to implement Page Object Pattern
  • Improving the project configuration – executing tests in parallel, tests execution order, parameterized tests, AssertJ and more

The source code for this tutorial can be found on Github

Prerequisites

First of all, Java JDK is required and it must be installed in your system. I will be using Java 12 and I recommend installing OpenJDK instead of the Oracle JDK due to licensing changes in Java 11. You will also need Gradle to init a new project and your favourite Java IDE – I recommend IntelliJ IDEA Community or Professional. Optionally, you can also install Git version control system.

For managing (installing, updating, uninstalling) the tools I recommended using the package manager. If you are working on Windows, you can use Chocolately, if you are on macOS you should be using Homebrew.

To sum up, make sure you have the following tools installed and available to you while working with the project in this article:

  • Java JDK – the newest Java JDK version is recommended
  • Gradle – required only for setting up the project, Gradle 5.6+ is recommended
  • Your favourite Java IDE – IntelliJ IDEA Community or Professional is recommended
  • Chrome browser – for running Selenium tests
  • Terminal Emulator – for executing shell commands with at least basic support for Unix commands. In Windows this can be Cmder (with Git), in macOS I recommend iTerm2
  • Git – if you want to track your source code history

Setup the project from the ground up

To create an empty Gradle based project, open you favourite terminal and type:

1
2
3
mkdir junit5-selenium-todomvc-demo
cd junit5-selenium-todomvc-demo
gradle init --type basic --dsl groovy

The generated project is an empty, DIY project – with no plugins and no dependencies. It comes with the redundant settings.gradle that can be removed:

1
rm settings.gradle

Java and JUnit 5

For the basic Java project configuration with JUnit 5 add the following content to the build.gradle:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
plugins {
 id 'java'
}
 
repositories {
  mavenCentral()
}
 
dependencies {
 testImplementation('org.junit.jupiter:junit-jupiter:5.5.1')
}
 
test {
 useJUnitPlatform()
 testLogging {
  events "passed", "skipped", "failed"
 }
}

The above DSL configures Gradle’s Java plugin (plugins), that provides us capabilities for building Java based projects with Gradle. The project uses Maven repository (repositories) to download project dependencies (dependencies) that are declared in the project. Test implementation dependency for the project is set to JUnit 5 (testImplementation) and the task is adjusted (test) to make sure that JUnit 5 is used while executing the tests with Gradle.

Configuration can be verified by executing the Gradle build in the terminal:

1
./gradlew build

The build is successful:

1
2
BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed

./gradlew command run the Gradle Wrapper instead a global Gradle distribution. The project was generated with the Gradle Wrapper and therefore global Gradle distribution is not needed at all for executing the tasks and working with the project.

Note: If you are looking for JUnit 5 project templates for Gradle or Maven checkout the official JUnit 5 Samples Github repository: https://github.com/junit-team/junit5-samples

JUnit Jupiter

To simplify the configuration of Selenium WebDriver in the project I am going to use Selenium Jupiter which is the JUnit 5 extension aimed to ease the use of Selenium (WebDriver and Grid) in JUnit 5 tests. It comes as a single dependency that needs to be added to dependencies list in build.gradle:

1
2
3
dependencies {
  testCompile('io.github.bonigarcia:selenium-jupiter:3.3.0')
}

Selenium Jupiter library provides integration with Selenium and Appium. Selenium Jupiter supports local and remote browsers, browsers in Docker containers (Docker engine is required) but also Selenide based browser configuration. It uses WebDriverManager internally to manage browser drivers.

Note: Don’t be suprised to see many libraries in your project. Selnium Jupiter has many dependencies. To see all the project dependencies (including transitive dependencies) execute the following command: ./gradlew dependencies.

Directories and project files

The project was created with no Java source files. To create the initial directory and the first test the following commands can be executed:

1
2
mkdir -p src/test/java/pl/codeleak/demos/selenium/todomvc
touch src/test/java/pl/codeleak/demos/selenium/todomvc/SeleniumTest.java

The SeleniumTest.java file contains very basic test confirming the project is configured properly. The test uses JUnit 5 extension provided by Selenium Jupiter and it has a single test with no assertions:

01
02
03
04
05
06
07
08
09
10
11
12
13
package pl.codeleak.demos.selenium.todomvc;
 
import io.github.bonigarcia.seljup.SeleniumExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.openqa.selenium.chrome.ChromeDriver;
 
@ExtendWith(SeleniumExtension.class)
class SeleniumTest {
 
    @Test
    void projectIsConfigured(ChromeDriver driver) {}
}

Running the test

Executing the Gradle build should confirm the test is passing:

1
2
3
4
5
6
./gradlew build
 
pl.codeleak.demos.selenium.todomvc.SeleniumTest > projectIsConfigured() PASSED
 
BUILD SUCCESSFUL in 1s
3 actionable tasks: 2 executed, 1 up-to-date

You probably noted that during the execution of the task the Chrome browser was opened and than closed. This only confirms that all the driver configuration was done under the hood by Selenium Jupiter (with the use of WebDriverManager library). You also noticed that there is no setup and cleanup code for this test. Instead we are injecting the instance of the ChromeDriver directly to the test where it is needed. This is how the Selenium Jupiter uses JUnit 5 extension mechanism to inject parameters to test.

Create Git repository

The initial setup of the project is done. Before the real work starts, the project setup can be now be stored in the Git repository. If you have Git installed, run the following command to create a new repository:

1
git init

Edit .gitignore file to exclude files and directories you want to skip from the repository:

1
2
3
4
5
.gradle
.idea
*.iml
build
out

Execute the following command to add and commit files to the repository:

1
2
git add .
git commit -m 'Initial project setup'

Importing project to IDE

Please note that all the job so far was done with no IDE whatsoever (not fully true – the test I created with the help of IDE). In general, this is very important aspect of the project configuration: always make your project IDE independent. Prove you can execute the build with single shell commands. This will pay off – especially when you are going to execute the build using continuous integration tool.

Anyways, with IntelliJ the project will work with no problem. Just lunch it and open a directory with the project and import it as Gradle project.

And now you are all set to start developing the tests and improving the project. But remember, if you are making any configuration changes it is advised that from time to time you test them with the terminal, outside the IDE.

Next steps

In the next part of this tutorial you will learn some basics about Page Object Pattern and implementing it using Selenium’s built-in PageFactory.

Published on Java Code Geeks with permission by Rafal Borowiec, partner at our JCG program. See the original article here: JUnit 5 and Selenium – Setup the project with Gradle, JUnit 5 and Jupiter Selenium

Opinions expressed by Java Code Geeks contributors are their own.

Rafal Borowiec

Software developer, Team Leader, Agile practitioner, occasional blogger, lecturer. Open Source enthusiast, quality oriented and open-minded.
Subscribe
Notify of
guest

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

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button