LambdaTest Community

Find answers, support, and inspiration from other users

Questions
LambdaTest Community
Answered

How can I execute Selenium test scripts using Cucumber?

I want a tutorial on how can I execute Selenium test scripts using Cucumber. Can anyone pleasse provide it to me.

Up Vote Down Vote 0 Votes
Posted 3 months ago

Answers


Let’s consider a real-world case study of querying a search term in google search by creating a feature file which consists of feature, tags and scenarios and scenario outline as applicable.

For automation testing with Selenium & Cucumber framework for Java, we need to create a GoogleSearch.feature file under Features package with below content under “src/test/java” path.

Feature: Searching Google should return the name of query
 Scenario: Google search with scenario
 Given user launches Google webapp
 When user searches for a "LambdaTest"
 And click on search button
 Then results retrieved should contain the "LambdaTest" used
 Scenario Outline: Searching google using scenario outline and examples
 Given user launches Google webapp
 When user searches for a "<query>"
 And click on search button
 Then results retrieved should contain the "<query>" used

Once you have completed creating a feature file, created steps would be highlighted with warnings indicating that you haven’t defined the step contained in step definition file yet. In order to resolve the errors, lets navigate to the step definition file to complete coding the gist which defines our steps. Below code can be replaced in step definition file which is Stepdefs.

TIP:

While creating your step definitions if you want a template for step definitions file creation just run the feature file along with warnings as discussed previously. It would provide you with the template upon which you can start coding as shown in the image below.

Make sure your step definitions are specified in package “com.googlesearch.testing.steps” under the path of “src/main/java”.

Copy the code and define the path to your chrome driver. Below is the code used for automation testing with Selenium & Cucumber in Java.

package com.googlesearch.testing.steps;
import http://org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import cucumber.api.java.en.And;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
public class stepDefinitions {
 public static WebDriver driver;
 String baseURL = "Google";
 @Given("^user launches Google webapp$")
 public void user_launches_Google_webapp() throws Throwable {
 // Write code here that turns the phrase above into concrete actions
 System.setProperty("webdriver.chrome.driver", "C:\\Cucumber_Workspace\\GoogleSearch\\Libs\\chromedriver.exe");
 driver = new ChromeDriver();
 driver.manage().window().maximize();
 driver.get(baseURL);
 }
 @When("^user searches for a \"([^\"]*)\"$")
 public void user_searches_for_a(String arg1) throws Throwable {
 WebElement searchBox = driver.findElement(http://By.name("q"));
 searchBox.sendKeys(arg1);
 }
 @And("^click on search button$")
 public void click_on_search_button() throws Throwable {
 WebElement searchBox = driver.findElement(http://By.name("q"));
 searchBox.submit();
 }
 @Then("^results retreived should contain the \"([^\"]*)\" used$")
 public void results_retreived_should_contain_the_used(String resultString) throws Throwable {
 WebElement result = driver.findElement(By.className("LC20lb"));
 if (resultString.equalsIgnoreCase(result.getText())) {
 System.out.println("Text is matching");
 } else {
 System.out.println("Text is not matching");
 }
 driver.quit();
 }
}

Make sure your Test Runner java file which is named as RunCucumberTest is under “src/test/java” path with code specified below.

import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
@RunWith(Cucumber.class)
@CucumberOptions(features = { "classpath:features" }, plugin = { "pretty", "json:target/cucumber/cucumber.json" })
public class RunCucumberTest {
}

Now you have all the 3 main aspects ready for test execution. To proceed further for running automation testing with Selenium and Cucumber you would need to right-click on RunCucumberTest.java file which is created. Click on Run as JUnit Test. Tests get triggered for both the scenarios specified. If you observe the scenario outline it gets executed thrice with all the sets of data specified in a data driven format.

Up Vote Down Vote 0 Votes
Posted 3 months ago