John Ferguson Smart | Mentor | Author | Speaker - Author of 'BDD in Action'.
Helping teams deliver more valuable software sooner20th March 2023

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

Objectives

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

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

Pre-requisites

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

  • Install the latest Java development environment. We recommend to use Java 11 version.
  • 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 LambdaTest Selenium cloud platform package manager.

Cloning Repo and Installing Dependencies

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

git clone https://github.com/serenity-bdd/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.

On Linux/Mac:

export LT_USERNAME="my.username" \
export LT_ACCESS_KEY="XXXXXXX"

And on Windows:

set LT_USERNAME="my.username" `
set LT_ACCESS_KEY="XXXXXXXXX"

Run Your First Test

Step 3 The first scenario you will run uses a simple Todo App that you can find here.

The Cucumber scenarios look like this:

Feature: A Simple Todo App

  Rule: The app should initally contain 5 items
    Scenario: Can see the default items
      When I open the app
      Then I should see 5 items

  Rule: Users can add more items to the list
    Scenario: Can add new items
      Given I have opened the app
      When I add new item "Complete LambdaTest Tutorial"
      Then I should see 6 items

We implement the step definition methods for these steps as follows:

public class TodoAppSteps {
    TodoApp todo;

    @Given("I have opened the app")
    @When("I open the app")
    public void iOpenTheApp() {
        todo.open();
    }

    @When("I add new item {string}")
    public void iAddNewItem(String item) {
        todo.addNewElement(item);
    }

    @Then("I should see {int} items")
    public void iShouldSeeItems(int expectedItems) {
        assertThat(todo.itemCount()).isEqualTo(expectedItems);
    }
}

The TodoApp class is the Page Object representing our Todo List app, and looks like this:

@DefaultUrl("https://lambdatest.github.io/sample-todo-app/")
public class TodoApp extends PageObject {

    private final static By LIST_ITEMS = By.cssSelector("div[ng-app='sampleApp'] li");

    public void addNewElement(String newItem) {
        $("#sampletodotext").sendKeys(newItem);
        $("#addbutton").click();
    }

    public List<String> listItems() {
        return findAll(LIST_ITEMS).texts();
    }

    public int itemCount() {
        return listItems().size();
    }
}

The final thing we need to configure LambdaTest is the serenity.conf file, which is where we configure the LambdaTest endpoint using the environment variables we defined earlier:

serenity {
  take.screenshots = FOR_FAILURES
}

headless.mode = true
webdriver {
  driver = remote
  remote.url = "https://"${LT_USERNAME}":"${LT_ACCESS_KEY}"@hub.lambdatest.com/wd/hub"
  capabilities {
    browserName = "chrome"
    "goog:chromeOptions" {
      args = ["remote-allow-origins=*","test-type", "no-sandbox", "ignore-certificate-errors", "--window-size=1000,800",
        "incognito", "disable-infobars", "disable-gpu", "disable-default-apps", "disable-popup-blocking"]
    }
    "LT:options" {
      platformName = "Windows 10"
    }
  }
}

We use the capabilities section to define the W3C capabilities to pass to LambdaTest, including the "LT:options" options where we can pass LambdaTest-specific properties.

Executing the Test

You can now run the test from the command line:

mvn clean verify

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 in the "LT:options" section of the webdriver 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.

You can read more about LambdaTest tunnel in the LambdaTest Tunnel documentation.

© 2019 John Ferguson Smart