Skip to main content

Serenity with Selenium: Tutorial to Run Your First Test on LambdaTest


In this topic, you will learn how to configure and run your Java automation testing scripts on LambdaTest Selenium cloud platform using Java framework Serenity.

Objective


By the end of this topic, you will be able to:

  1. Set up an environment for testing your hosted web pages using Serenity framework with Selenium.
  2. Understand and configure the core capabilities required for your Selenium test suite.
  3. Run test cases in parallel using Serenity with Selenium to reduce build times.
  4. Test your locally hosted pages on LambdaTest platform.
  5. Explore advanced features of LambdaTest.

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

Prerequisites For Running Serenity with Selenium


Before you can start performing Java automation testing with Selenium, you would need to:

  • Install the latest Java development environment i.e. JDK 1.6 or higher. We recommend using the latest version.

  • Download the latest Selenium Client and its WebDriver bindings from the official website. Latest versions of Selenium Client and WebDriver are ideal for running your automation script on LambdaTest Selenium cloud grid.

  • Install Maven. It can be downloaded and installed following the steps from the official website. Maven can also be installed easily on Linux/MacOS using Homebrew package manager.

Installing Selenium Dependencies and Tutorial epo

Step 1: Clone the LambdaTest’s Serenity-Selenium-Sample repository and navigate to the code directory as shown below:

git clone https://github.com/LambdaTest/Serenity-Selenium-Sample
cd Serenity-Selenium-Sample

You may also want to run the command below to check for outdated dependencies.

mvn versions:display-dependency-updates

Setting up Your Authentication

Make sure you have your LambdaTest credentials with you to run test automation scripts on LambdaTest Selenium Grid. You can obtain these credentials from the LambdaTest Automation Dashboard or through LambdaTest Profile.

Step 2: Set LambdaTest Username and Access Key in environment variables.

  • For Linux/macOS:
    export LT_USERNAME="YOUR_USERNAME" export LT_ACCESS_KEY="YOUR ACCESS KEY"
  • For Windows:
    set LT_USERNAME="YOUR_USERNAME" set LT_ACCESS_KEY="YOUR ACCESS KEY"

Run Your First Test


Sample Test with Serenity

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 GooglePage.java file for the above Test Case Scenario.

//GooglePage.java

package com.lambdatest.cucumber.pages;

import static org.assertj.core.api.Assertions.assertThat;

import org.openqa.selenium.support.FindBy;

import net.serenitybdd.core.pages.WebElementFacade;
import net.thucydides.core.annotations.DefaultUrl;
import net.thucydides.core.pages.PageObject;

@DefaultUrl("https://www.google.com/ncr")
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 {
searchButton.click();
Thread.sleep(5000);
}

public void titleShouldMatch(String matchTitle) {
assertThat(this.getTitle()).containsIgnoringCase(matchTitle);
}
}

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

//LambdaTestSerenityDriver.java

package com.lambdatest;

import java.net.URL;
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) it.next();

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;
}
}

Note: You can generate capabilities for your test requirements with the help of our inbuilt Capabilities Generator tool.

Executing the Test

Step 3: The tests can be executed in the terminal using the following command:

mvn verify -P single

Your test results would be displayed on the test console (or command-line interface if you are using terminal/cmd) and on LambdaTest automation dashboard. LambdaTest Automation Dashboard will help you view all your text logs, screenshots and video recording for your entire automation tests.

Running Your Parallel Test Using Serenity


Setting up the Parallel Environment

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

For ParallelChromeTest.java:

//Running Parallel Test On Chrome

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

Similarly we define the class for the remaining browsers.

Executing Parallel Tests Using Serenity

To run parallel tests using Serenity, we would have to execute the below commands in the terminal:

mvn verify -P parallel

Your test results would be displayed on the test console (or command-line interface if you are using terminal/cmd) and on LambdaTest automation dashboard.

Testing Locally Hosted or Privately Hosted Projects


You can test your locally hosted or privately hosted projects with LambdaTest Selenium grid cloud using LambdaTest Tunnel app. All you would have to do is set up an SSH tunnel using LambdaTest Tunnel app and pass toggle tunnel = True via desired capabilities. LambdaTest Tunnel establishes a secure SSH protocol based tunnel that allows you in testing your locally hosted or privately hosted pages, even before they are made live.

Refer our LambdaTest Tunnel documentation for more information.

Here’s how you can establish LambdaTest Tunnel.

Download the binary file of:

Open command prompt and navigate to the binary folder.

Run the following command:

LT -user {user’s login email} -key {user’s access key}

So if your user name is [email protected] and key is 123456, the command would be:

LT -user [email protected] -key 123456

Once you are able to connect LambdaTest Tunnel successfully, you would just have to update the serenity.properties file by setting the tunnel capability lt_tunnel to true.ou would just have to pass on tunnel capabilities in the code shown below :

Tunnel Capability

 tunnel = true

Executing Local Tests Using Serenity

To run local tests using Serenity, we would have to execute the below commands in the terminal:

mvn verify -P local