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 configuring and running your Cucumber Java automation test scripts on LambdaTest Selenium cloud platform. In this post we would be exploring:
1 |
sudo apt-get install openjdk-8-jre |
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.
set LT_USERNAME=”YOUR_USERNAME”
set LT_ACCESS_KEY=”YOUR ACCESS KEY”
export LT_USERNAME=”YOUR_USERNAME”
export LT_ACCESS_KEY=”YOUR ACCESS KEY”
1 |
$ cd Cucumber-TestNG-Sample |
1 |
$ mvn versions:display-dependency-updates |
Here is the sample feature file for Cucumber.
1 2 3 4 5 6 7 8 9 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
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 = {"~@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"); 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"); capability.setCapability("network", true); capability.setCapability("video", true); capability.setCapability("console", true); capability.setCapability("visual", true); 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
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(); } } |
Open your cmd/terminal and route the pointer to Cucumber-TestNG-Sample.
1 |
$ mvn test |
You can test your locally hosted or privately hosted projects with LambdaTest Selenium grid cloud using Lambda Tunnel app. All you would have to do is set up an SSH tunnel using Lambda Tunnel app, and pass toggle tunnel = True via desired capabilities. Lambda 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.
Here’s how you can establish Lambda 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
1 2 |
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 “http://localhost/” or “http://localhost:8080” etc, you would get an error in these browsers. A possible solution is to use “localhost.lambdatest.com” or replace the string “localhost” with machine IP address. For example if you wanted to test “http://localhost/dashboard” or, and your machine IP is 192.168.2.6 you can instead test on “http://192.168.2.6/dashboard” or “http://localhost.lambdatest.com/dashboard”.