Skip to main content

Cucumber 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 Cucumber with TestNG.

Objective


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

  1. Set up an environment for testing your hosted web pages using Cucumber framework with TestNG and Selenium.
  2. Understand and configure the core capabilities required for your Selenium test suite.
  3. Test your locally hosted pages on LambdaTest platform.
  4. 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 Cucumber-TestNG 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 cucumber-testng-sample repository and navigate to the code directory as shown below:

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

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 Cucumber-TestNG

Here 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 our feature file through Selenium using TestNG.

package MyRunner;

import java.net.URL;

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 + "@hub.lambdatest.com/wd/hub";
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 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();
}

}

Configuration of Your Test Capabilities

Step 3: In the test script, you need to update your test capabilities. In this code, we are passing browser, browser version, and operating system information, along with LambdaTest Selenium grid capabilities via capabilities object. The capabilities object in the above code are defined as:

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");

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

Executing the Test

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

mvn test

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.

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 pass on tunnel capabilities in the code shown below :

Tunnel Capability

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