Skip to main content

Running Serenity Test On Selenium Grid

Serenity BDD is an open-source framework that enables you to write cleaner and maintainable tests faster. It provides support for automated web testing. It lets you generate comprehensive test reports, tracks your automated tests, keeps project status tabs, and creates more versatile and easy-to-maintain tests.

This document will help you run Serenity automation tests on LambdaTest cloud Selenium Grid. LambdaTest enables you to run your Serenity automated tests across 2000+ real browsers for desktop & mobile combination.


All the code samples in this documentation can be found in the Serenity LambdaTest Repository on Image GitHub. You can either download or clone the repository to quickly run your tests.

Before starting running your Selenium tests with Serenity, ensure you have the following things installed.

  1. Download Java development environment i.e. JDK 8 or higher. The latest version is recommended.
  2. An IDE(Integrated Development Environment) such as Eclipse or you can use CMD (Command Prompt).
  3. Make sure you have the following Maven dependencies installed.

You can install it by running the following command in the terminal.

mvn install

  1. To run your Serenity Tests on online Selenium Grid, you would need to set your LambdaTest username and access key in environment variables. You can get it from Automation Dashboard by clicking on the Key button from the top-right. You can set environment variables or update file with your LambdaTest Username and Access Key.
  • For Windows:
set LT_USERNAME="undefined"set LT_ACCESS_KEY="undefined"
  • For Mac/Linux:
export LT_USERNAME="undefined"export LT_ACCESS_KEY="undefined"

Running Your First Serenity Test#

To run your first Serenity Test on LambdaTest Selenium Grid, let’s understand our test case scenario, the test case below checks for the word "LambdaTest" on Google and tests if the title of the resultant page is "LambdaTest-Google Search".

Feature: Google's Search Functionality
    Scenario: Can find search results        When I type query as "LambdaTest"        And I submit        Then I should see title "LambdaTest - Google Search"

Following below is the file for the above Test Case Scenario.

package com.lambdatest.cucumber.pages;
import static org.assertj.core.api.Assertions.assertThat;
import net.serenitybdd.core.pages.WebElementFacade;import net.thucydides.core.annotations.DefaultUrl;import net.thucydides.core.pages.PageObject;
@DefaultUrl("")public class GooglePage extends PageObject {
    @FindBy(name = "q")    WebElementFacade search;
    @FindBy(name = "btnK")    WebElementFacade searchButton;
    public void searchForString(String searchString) {        search.sendKeys(searchString);    } public void submitForm() throws Exception {;        Thread.sleep(5000);    }
    public void titleShouldMatch(String matchTitle) {        assertThat(this.getTitle()).containsIgnoringCase(matchTitle);    }}

Below is the file that shows the integration of Serenity with LambdaTest.

package com.lambdatest;
import;import java.util.Iterator;
import org.openqa.selenium.WebDriver;import org.openqa.selenium.remote.DesiredCapabilities;import org.openqa.selenium.remote.RemoteWebDriver;
import net.thucydides.core.util.EnvironmentVariables;import net.thucydides.core.util.SystemEnvironmentVariables;import net.thucydides.core.webdriver.DriverSource;
public class LambdaTestSerenityDriver implements DriverSource {
    public WebDriver newDriver() {        EnvironmentVariables environmentVariables = SystemEnvironmentVariables.createEnvironmentVariables();
        String username = System.getenv("LT_USERNAME");        if (username == null) {            username = (String) environmentVariables.getProperty("lt.user");        }
        String accessKey = System.getenv("LT_ACCESS_KEY");        if (accessKey == null) {            accessKey = (String) environmentVariables.getProperty("lt.key");        }
        String environment = System.getProperty("environment");                DesiredCapabilities capabilities = new DesiredCapabilities();        capabilities.setCapability("plugin","Serenity LambdaTest Plugin");
        Iterator it = environmentVariables.getKeys().iterator();        while (it.hasNext()) {            String key = (String);
            if (key.equals("lt.user") || key.equals("lt.key") || key.equals("lt.grid")) {                continue;            } else if (key.startsWith("lt_")) {                capabilities.setCapability(key.replace("lt_", ""), environmentVariables.getProperty(key));
            } else if (environment != null && key.startsWith("environment." + environment)) {
                capabilities.setCapability(key.replace("environment." + environment + ".", ""),                        environmentVariables.getProperty(key));            }        }
        try {            String url = "https://" + username + ":" + accessKey + "@" + environmentVariables.getProperty("lt.grid")                    + "/wd/hub";            return new RemoteWebDriver(new URL(url), capabilities);        } catch (Exception e) {            System.out.println(e);            return null;        }    }
    public boolean takesScreenshots() {        return false;    }}

Now run the following command in your terminal/cmd to run your Single Serenity Test on LambdaTest Grid.

mvn verify -P single

Visit your Automation dashboard. You will find your single Serenity test is executed successfully.


Running Parallel Tests With Serenity#

Parallel Testing is among the most stimulating features of the LambdaTest Selenium Grid. You can run more than one test case concurrently through parallel testing. This means that parallel testing will allow you to run a whole range of automation test cases. So you're running a single test scenario across multiple browsers, or you might run different test scenarios across the same browser, but with different browser versions.

To run parallel tests with Serenity,we will run single.feature test case in four different environments Chrome, Firefox, IE, and Safari.


//Running Parallel Test On Chrome
@RunWith(CucumberWithSerenity.class)@CucumberOptions(features = "src/test/resources/features/single.feature")public class ParallelChromeTest extends LambdaTestSerenityTest {}


//Running Parallel Test On Firefox
@RunWith(CucumberWithSerenity.class)@CucumberOptions(features = "src/test/resources/features/single.feature")public class ParallelFirefoxTest extends LambdaTestSerenityTest {}


//Running Parallel Test On Internet Explorer
@RunWith(CucumberWithSerenity.class)@CucumberOptions(features = "src/test/resources/features/single.feature")public class ParallelIETest extends LambdaTestSerenityTest {}


//Running Parallel Test On Safari
@RunWith(CucumberWithSerenity.class)@CucumberOptions(features = "src/test/resources/features/single.feature")public class ParallelSafariTest extends LambdaTestSerenityTest {}

Now run the following command in your terminal/cmd to run your Parallel Serenity Test on LambdaTest Grid.

mvn verify -P parallel

Visit your Automation dashboard. You will find your Parallel Serenity tests are executed successfully.


Note: Get your test coverage by using our Concurrency Test Calculator to know how many concurrent sessions you need.

Running Serenity Tests For Locally Hosted Websites#

With LambdaTest Tunnel, you can perform Serenity tests of your locally hosted websites across 2000+ browsers/OS combinations running on real devices and operating systems. LambdaTest Tunnel allows you to test your website or web-pages for cross-browser compatibility before making them live.

LambdaTest Tunnel has several protocols including Web Socket, HTTPS, SSH(Encrypted Shell), etc to help you develop a secure and specialized tunnel connection between your device and LambdaTest cloud servers over corporate firewalls.

For more information, visit our LambdaTest Tunnel documentation.

To perform the Serenity test you would have to download the tunnel binary. You can find OS-specific instructions to download and set up tunnel binary at the following links.

You would need to update the file by setting the tunnel capibility lt_tunnel to true.

webdriver.timeouts.implicitlywait = 5000serenity.use.unique.browser =
#You can add more capability with a prefix 'lt_' as below#For example to use lt_network as true use below capabilitylt_build=Serenity-Selenium-Samplelt_debug=truelt_console=falselt_visual=falselt_tunnel=true //tunnel capibility
#You can add more capability with a prefix 'environment.{environment}.' as below#Check valid capabilities here -
environment.single.browser=Chromeenvironment.single.browserVersion=78environment.single.platform=Windows 10
environment.parallel_1.browser=Chromeenvironment.parallel_1.browserVersion=78environment.parallel_1.platform=Windows 10
environment.parallel_2.browser=firefoxenvironment.parallel_2.browserVersion=68environment.parallel_2.platform=Windows 8.1
environment.parallel_3.browser=Safarienvironment.parallel_3.browserVersion=12environment.parallel_3.platform=MacOS Mojave
environment.parallel_4.browser=Internet Explorerenvironment.parallel_4.browserVersion=11environment.parallel_4.platform=Windows 7
#environment.parallel_5.deviceName=Galaxy S10

Now run the following command in your terminal/cmd to run your local Serenity Test.

mvn test -P local

Visit your Automation dashboard. You will find your Local Serenity tests are executed successfully. You can find the blue tunnel icon on the right corner that ensures the tunnel is active.


That's all! You have successfully executed your Serenity Automation Tests. In case you come across any doubts, feel free to reach out through our 24*7 Chat Portal or you can also drop a mail to [email protected].
Happy testing!

Last updated on