Skip to main content

Running Cucumber Scripts With TestNG And Selenium

LambdaTest Selenium Automation Grid is a cloud-based scalable Selenium testing platform which enables you to run your automation scripts on 2000+ different browsers and operating systems. You can now run your Cucumber scripts with TestNG and Selenium for automating your web application over a scalable Selenium infrastructure that is running real browsers and real operating systems.

This post will help you in getting started with Selenium automation testing with Cucumber on LambdaTest cloud platform. In this post we would be exploring:

  • Prerequisites and Environment Setup.
  • Running your first Cucumber Java test.
  • Running your Cucumber scripts on locally hosted web application.


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

  1. Global Dependencies

    • Make sure you have the latest Java installed in your system. For Windows, you can install it from their official website by clicking here.

    • For Linux, execute the following command in terminal.

    sudo apt-get install openjdk-8-jre
    • For Mac, Java is installed by default on Mac OS X.
    • Install Maven from their official website. You could also use the below command for Homebrew.
  2. LambdaTest Authentication Credentials

Be aware of your LambdaTest authentication credentials i.e. your LambdaTest username, access key and HubURL. You need to set them up as your environment variables. You can retrieve them from your LambdaTest automation dashboard by clicking on the key icon near the help button.

  • For Windows:
set LT_USERNAME="undefined"set LT_ACCESS_KEY="undefined"
  • For Mac/Linux:
export LT_USERNAME="undefined"export LT_ACCESS_KEY="undefined"
  1. Environment Setup

    • Ensure that the necessary packages are available by running the below command.
    $ cd Cucumber-TestNG-Sample
    • You may also want to run the command below to check for outdated dependencies. Please be sure to verify and review updates before editing your pom.xml file as they may not be compatible with your code.
    $ mvn versions:display-dependency-updates

Running Your First Cucumber Script For TestNG and Selenium#

Here is the sample feature file for Cucumber.

Feature: Add new item to ToDO list
Scenario: Lambdatest ToDO Scenario
Given user is on home PageWhen select First ItemThen select second itemThen add new itemThen verify added item

Here is the TestRunner file to automate our feature file through Selenium using TestNG.

package MyRunner;
import org.openqa.selenium.remote.CapabilityType;import org.openqa.selenium.remote.DesiredCapabilities;import org.openqa.selenium.remote.RemoteWebDriver;import org.testng.annotations.AfterClass;import org.testng.annotations.AfterMethod;import org.testng.annotations.BeforeClass;import org.testng.annotations.BeforeMethod;import org.testng.annotations.DataProvider;import org.testng.annotations.Parameters;import org.testng.annotations.Test;
import cucumber.api.CucumberOptions;import cucumber.api.testng.CucumberFeatureWrapper;import cucumber.api.testng.TestNGCucumberRunner;
@CucumberOptions(        features = "src/main/java/Features",        glue = {"stepDefinitions"},        tags = {"[email protected]"},        format = {                "pretty",                "html:target/cucumber-reports/cucumber-pretty",                "json:target/cucumber-reports/CucumberTestReport.json",                "rerun:target/cucumber-reports/rerun.txt"        },plugin = "json:target/cucumber-reports/CucumberTestReport.json")
public class TestRunner {        private TestNGCucumberRunner testNGCucumberRunner;      public static RemoteWebDriver connection;        @BeforeClass(alwaysRun = true)    public void setUpCucumber() {         testNGCucumberRunner = new TestNGCucumberRunner(this.getClass());    }        @BeforeMethod(alwaysRun = true)    @Parameters({ "browser", "version", "platform" })    public void setUpClass(String browser, String version, String platform) throws Exception {
            String username = System.getenv("LT_USERNAME") == null ? "YOUR LT_USERNAME" : System.getenv("LT_USERNAME");             String accesskey = System.getenv("LT_ACCESS_KEY") == null ? "YOUR LT_ACCESS_KEY" : System.getenv("LT_ACCESS_KEY"); 
            DesiredCapabilities capability = new DesiredCapabilities();                     capability.setCapability(CapabilityType.BROWSER_NAME, browser);            capability.setCapability(CapabilityType.VERSION,version);            capability.setCapability(CapabilityType.PLATFORM, platform);                                capability.setCapability("build", "Your Build Name");            String gridURL = "https://" + username + ":" + accesskey + "";            System.out.println(gridURL);            connection = new RemoteWebDriver(new URL(gridURL), capability);            System.out.println(capability);            System.out.println(connection);}     @Test(groups = "cucumber", description = "Runs Cucumber Feature", dataProvider = "features")    public void feature(CucumberFeatureWrapper cucumberFeature) {        testNGCucumberRunner.runCucumber(cucumberFeature.getCucumberFeature());    }     @DataProvider    public Object[][] features() {        return testNGCucumberRunner.provideFeatures();    }     @AfterClass(alwaysRun = true)    public void tearDownClass() throws Exception {        testNGCucumberRunner.finish();    }}

Below are the step definitions.

package stepDefinitions;
import org.openqa.selenium.By;import org.openqa.selenium.remote.RemoteWebDriver;import org.testng.Assert;
import cucumber.api.Scenario;import;import;import;import;import;import MyRunner.*;
public class ToDoStepDefinition extends TestRunner {
    public RemoteWebDriver driver = this.connection;
    @Before    public void updateName(Scenario scenario) {        driver.executeScript("lambda-name="+scenario.getName());    }
    @Given("^user is on home Page$")    public void user_already_on_home_page() {        System.out.println(driver.getCapabilities());        driver.get("");
    @When("^select First Item$")    public void select_first_item() {        driver.findElement("li1")).click();    }
    @Then("^select second item$")    public void select_second_item() {        driver.findElement("li2")).click();    }
    @Then("^add new item$")    public void add_new_item() {        driver.findElement("sampletodotext")).clear();        driver.findElement("sampletodotext")).sendKeys("Yey, Let's add it to list");        driver.findElement("addbutton")).click();    }
    @Then("^verify added item$")    public void verify_added_item() {        String item = driver.findElement(By.xpath("/html/body/div/div/div/ul/li[6]/span")).getText();        Assert.assertTrue(item.contains("Yey, Let's add it to list"));    }
    @After    public void close_the_browser(Scenario scenario) {        driver.executeScript("lambda-status=" + (scenario.isFailed() ? "failed" : "passed"));        driver.quit();    }

Open your cmd/terminal and route the pointer to Cucumber-TestNG-Sample.

$ mvn test

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 latest LambdaTest Tunnel binary file and extract it.


To run your Cucumber tests for locally hosted web applications you would need to set the Tunnel Capability flag to true.

 DesiredCapabilities capabilities = new DesiredCapabilities();         capability.setCapability("tunnel", true);

Important Note: Some Safari & IE browsers, doesn’t support automatic resolution of the URL string “localhost”. Therefore if you test on URLs like https://localhost/ or https://localhost:8080 etc, you would get an error in these browsers. A possible solution is to use or replace the string “localhost” with machine IP address. For example if you wanted to test https://localhost/dashboard or, and your machine IP is you can instead test on or

Last updated on