Automating UI tests: Selenide & Java with Optimizely demo

March 26, 2024

Setting up an automated test in Selenide and Java is just a few steps away, thanks to this in-depth guide from our Senior QA Automation Engineer Konstantin Loginov. Keep reading to find out how to implement the test, and see his examples using Optimizely.




Software test automation is essential nowadays because it helps in reducing manual effort, increases test coverage, and accelerates the software development lifecycle. It also improves accuracy and consistency in testing, leading to higher-quality software releases.

Main advantages of test automation implementation

  • Saves time and effort by handling repetitive tasks, boosting productivity and accelerating product delivery
  • Automated tests ensure consistent and accurate results, minimizing human errors and maintaining software reliability
  • Automation aids in detecting regressions early, facilitating rapid feedback and preventing issues from reaching production
  • Automated testing scales effectively to handle complex projects and diverse testing environments without requiring additional resources
  • While initial setup costs are involved, automation reduces long-term testing expenses, leading to improved cost-effectiveness and ROI

Selenium WebDriver

Despite the fact that nowadays there are many tools for WEB UI test automation, Selenium is still at the top, and de-facto as the standard.

Why Selenium is the top choice:

  1. It supports all of the most popular programming languages (Java, Python, JavaScript, C#, Ruby)
  2. It gives the opportunity to launch autotests for all well-known browsers (Google Chrome, Safari, Edge, Firefox, Opera)

What is Selenide and what are its benefits?

Selenide is an open-source automation testing framework for web applications based on Selenium WebDriver. It has the following benefits:

  • Simpler syntax and improved readability
  • Powerful selectors
  • Smart waiting mechanisms
  • Detailed exception descriptions
  • Automated screenshots for failed test cases
  • Easy integration setup with other testing frameworks

Demo with Optimizely

Let's create a new project with Java and Selenide to start test automation for Optimizely authorization feature.

We will be using Maven as an automation build tool. 

To add Selenide library to our project, we need include the following dependency to pom.xml file:

We also need to add one of the Java test frameworks. I prefer JUnit, so we need include dependency for JUnit 5:

After that, we are ready to go!

Start browser instance with Selenide

If you already have experience working with clear Selenium you already know how many things you need to do just to open instance of browser with the necessary URL:

  • Find and download browser driver corresponding browser type and version
  • Add browser driver path to environment variable
  • Create new instance of driver 
  • Use driver.get(“https://url” ) method

Selenide allows us to replace all of those steps in one command!

We just need to use method open() from the Selenide library:

When using Selenide, it automatically downloads the required version of the driver based on your browser's version, sets the driver's path, and creates an instance of the driver to navigate to the specified link.

Locators in Selenide

Selenide provides different types of locators based on ID, CSS, XPath, searching by text and more specifically. To define the locators you need to use method $() to search the web element and $$() to search the elements collection.

For example, in our case we need to use locators searching by id and by text in this way:

$(byId("UserName"))

$(byId("Password"))

$(byId("Submit"))

$(byText("Check the Catalog"))

Basic methods in Selenide

Basic Selenide methods we will use are as follows:

setValue() - allows us to set the web element with the value from the argument. Before starting the setting this method will be waiting for up to 4 seconds by default for element finding on the page. 

click() - waiting for the element to appear on the page for up to 4 seconds and click on it.

shouldBe(), shouldHave() - assert statements with smart waiting (implicit and explicit) which checks actual result correspondence to the expected result.

A simple autotest with Selenide and Java to check success auth for Optimizely user looks like this:

Here we set username and password input fields with valid data and clicked on the “Submit” button. After this, check that "Check the Catalog" is displayed after successful authentication.

Also, we can add the autotest, which checks that we can see the error message in the case of invalid credentials:

Page Object Model pattern in test automation design

The Page Object Model (POM) pattern in test automation design involves creating reusable and maintainable objects that represent web pages, encapsulating their elements and functionality to streamline test development and improve code maintainability.

Then POM class for Login page can be represented in this way:

And for Landing page like this:

Then, the finally rewritten autotest with a POM pattern for success auth looks the following way:

Allure report building

When we have a greater amount of autotests, we truly need a reporting system which provides us with clear information about autotest statuses, and additional info to investigate causes of failed tests.

The Allure report is a powerful tool for generating interactive and visually appealing test reports with detailed information, helping teams to analyze test results effectively.

To include Allure reports we need to add the following dependencies:

Selenide provides an easy way to configure Allure report generating. We just need to add the following listener:

To launch autotest and then generate an Allure report we can use the following command with Maven:

After that all autotests will be launched, all files for report will be generated, Jetty web server will be launched, and an html report will be built.

Example of successful authorization test report: 

When we have a failed test, the Allure report displays the error message description:

And it also provides a screenshot by Selenide of the failed step:

Leveraging test automation with Java, Selenide, and Allure empowers software testing teams to achieve greater efficiency and reliability in their testing processes. Java's robust programming capabilities, combined with Selenide's intuitive syntax and Allure's comprehensive reporting features, create a powerful toolkit for automated testing. By adopting these technologies, organizations can streamline their testing workflows, improve test coverage, and deliver high-quality software products to their users.

About the author

Konstantin Loginov is a Senior QA Automation Engineer at First Line Software, and has been with the team since 2019. Konstantin has shared his expertise in QA automation with his colleagues with workshops and mentorship through our Influencer program.

Let’s talk!

Have any questions? Fill out the form and our team will be in touch!