Selenium With Cucumber
Run Cucumber tests on the TestMu AI cloud grid. This guide covers setup, running a sample test, configuring capabilities, and testing locally hosted pages.
All the code used in this guide is available in the sample repository.
Prerequisites
Make sure you have the following set up before you start.
- Create a TestMu AI account if you don't have one.
- Get your Username and Access Key from the TestMu AI Dashboard.
- Install the Java development environment (Java 11 recommended).
- Install Maven. Download it from the official website or install it on Linux/MacOS using Homebrew.
Step 1: Clone the Sample Project
Pull the sample repo to your local machine and navigate into the project directory.
git clone https://github.com/LambdaTest/cucumber-testng-sample
cd cucumber-testng-sample
You may also want to run the command below to check for outdated dependencies.
mvn versions:display-dependency-updates
Step 2: Set Your Credentials
Add your TestMu AI credentials as environment variables so the test can authenticate with the grid.
Visit the TestMu AI Dashboard, navigate to the left sidebar, and select Credentials. Copy your Username and Access Key, then set them as environment variables:
- macOS / Linux
- Windows
export LT_USERNAME="undefined"
export LT_ACCESS_KEY="undefined"
set LT_USERNAME="undefined"
set LT_ACCESS_KEY="undefined"
Step 3: Configure Your Test Capabilities
Define the browser, version, and OS for your test run.
ChromeOptions browserOptions = new ChromeOptions();
browserOptions.setPlatformName(platform);
browserOptions.setBrowserVersion("latest");
HashMap<String, Object> ltOptions = new HashMap<String, Object>();
ltOptions.put("build", "Your Build Name");
ltOptions.put("w3c", true);
browserOptions.setCapability("LT:Options", ltOptions);
Use the Capabilities Generator to auto-generate capabilities for any browser, version, and OS combination.
Below is the sample feature file for Cucumber:
Feature: Add new item to ToDO list
Scenario: Lambdatest ToDO Scenario
Given user is on home Page
When select First Item
Then select second item
Then add new item
Then verify added item
Here is the TestRunner file to automate the feature file through Selenium using TestNG:
package MyRunner;
import java.net.URL;
import java.util.HashMap;
import org.openqa.selenium.chrome.ChromeOptions;
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 = {"~@Ignore"},
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");
ChromeOptions browserOptions = new ChromeOptions();
browserOptions.setPlatformName(platform);
browserOptions.setBrowserVersion("latest");
HashMap<String, Object> ltOptions = new HashMap<String, Object>();
ltOptions.put("build", "Your Build Name");
ltOptions.put("w3c", true);
browserOptions.setCapability("LT:Options", ltOptions);
String gridURL = "https://" + username + ":" + accesskey + "@hub.lambdatest.com/wd/hub";
System.out.println(gridURL);
connection = new RemoteWebDriver(new URL(gridURL), browserOptions);
System.out.println(browserOptions);
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 cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
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("https://lambdatest.github.io/sample-todo-app/");
}
@When("^select First Item$")
public void select_first_item() {
driver.findElement(By.name("li1")).click();
}
@Then("^select second item$")
public void select_second_item() {
driver.findElement(By.name("li2")).click();
}
@Then("^add new item$")
public void add_new_item() {
driver.findElement(By.id("sampletodotext")).clear();
driver.findElement(By.id("sampletodotext")).sendKeys("Yey, Let's add it to list");
driver.findElement(By.id("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();
}
}
Step 4: Run the Test
Trigger the test from your terminal.
mvn test
Step 5: View Your Results
Check the Automation Dashboard to see exactly what happened during your test.
Visit the TestMu AI Automation Dashboard to see your test results. Each session includes:
- Video recording of the full test execution
- Screenshots captured at each step
- Console logs from the browser
- Network logs for every request and response
- Selenium command logs showing each driver action
Run Cucumber Tests Using Agent Skills
Use AI coding assistants to generate and run Cucumber tests with the TestMu AI Agent Skill.
The cucumber-skill is part of TestMu AI Agent Skills - structured packages that teach AI coding assistants how to write production-grade test automation.
Install the skill:
git clone https://github.com/LambdaTest/agent-skills.git
cp -r agent-skills/cucumber-skill .claude/skills/
# For Cursor / Copilot
cp -r agent-skills/cucumber-skill .cursor/skills/
Install all available framework skills at once by cloning the repository directly into your tool's skills directory (e.g., .claude/skills/, .cursor/skills/).
