Published in


End-to-end testing with Selenium, Gradle, JUnit

I wanted to test a web page using JVM, but there are scattered tutorials everywhere. I decided to create a barebones project to illustrate how.

Our goal will be to test the end-to-end flow of adding a Kindle to the cart on

1. New project

Follow the steps at:

2. Selenium

Add Selenium to the dependencies:


To be able to control a browser, Selenium needs a proper driver. There are multiple ways to achieve that but the quickest is to let WebDriverManager manage that for you:


Now, edit TestAmazonCart and paste the following basic test that visits Amazon:

This assumes you have Chrome, but you can replace chromedriver() with other.

3. Page object model

The test will quickly get filled with details like CSS selectors and waits, so we’ll resort to the Page Object Model pattern, which abstracts the page details. It’s a way to follow the SLA principle.

Let’s create the following files:

Notice that the test method is taking advantage of the Page Object Model by running multiple instructions on different pages

Some people like to put all the selectors in the beginning, but I avoid that due to simplicity and because they're used only once.

4. Reports

How about a beautiful report? Just add the Allure report dependency to build.gradle.kts’s plugins section:

id("io.qameta.allure") version "2.9.6"
id("io.qameta.allure-report") version "2.9.6"

Run the tests. Then, to view its report, run:

./gradlew allureServe

How about adding screenshots to the report when a test fails? Create the following utility:

Now, you just need to inject it into the test:

private val screenshotOnError = ScreenshotOnError(driver as RemoteWebDriver)

This is enough to add the screenshot to the Allure report.

5. Headless

If you need to integrate this into your CI/CD system, you need to use a headless browser (browser runs without UI). For that, you can change the setup to:

init {

private val driver = ChromeDriver(ChromeOptions().addArguments("--headless"))

private val screenshotOnError = ScreenshotOnError(driver)

Be aware that the addArguments part needs to depend on an environment variable so that you can run on headless only on CI/CD and not on local testing.




Everything connected with Tech & Code. Follow to join our 900K+ monthly readers

Recommended from Medium

Meet our G{Code} Mentors!

The 22 Most-Used Python Packages in The World in 2021

Integration of GitHub,Jenkins and Docker with different branches{Master and Developer’s}]

An Agile Approach to Zomato

ASNU Harness Adapter Bosch Injector To Toyota Loom With Clip | JDM Garage UK — Nissan Specialists

Basic Concepts and Tools to Get Started with Web Development

How to setup your new apple silicon M1 mac for development

We need a better alternative to Web(& Electron) apps

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Luís Soares

Luís Soares

I write about Lean, TDD, CI/CD, trunk-based dev., user-centric dev, DDD, coding good practices, testing

More from Medium

JDK 17 — Unsupported Class File Major Version

How to insert a ScrollView in Android Layout

Observability in Kotlin — with KTor and New Relic

Build Simple REST API Using Vert.x in Kotlin