Cucumber — Step Definition | Code Factory

Code Factory
1 min readNov 27, 2020

Donate : Link

WordPress Blog : Link

Applications… : Link

Cucumber Tutorial Index Page: Link

  • Cucumber doesn’t know how to execute scenarios in feature file alone.
  • It needs a step definition to translate plain text Gherkin steps into actions that will interact with system.
  • When Cucumber executes a step in scenario it will look for matching step definition to execute.
  • A step definition is a small piece of code with a pattern attached to it.
  • The pattern is used to link the step definition to all the matching steps and the code is what cucumber will executes when it sees a Gherkin step.

Feature file:

Feature: Login functionality feature
Scenario: Login functionality with valid data
Given Navigate to xyz.com login page
When User logged in using username as "user1" and password as "user1
Then Home page should be display

Class file:

public class stepDefinition {
WebDriver driver;

/* https://github.com/mozilla/geckodriver/releases */
static {
/* System.setProperty("webdriver.gecko.driver", "<path to your gecko driver executable>"); */
System.setProperty("webdriver.gecko.driver", "C:\\Users\\CodeFactory\\Downloads\\geckodriver-v0.28.0-win64\\geckodriver.exe");
}

@Given("^Navigate to xyz.com login page$")
public void navigate() {
driver = new FirefoxDriver();
driver.get("https://xyz.com");
driver.manage().window().maximize();
}

@When("^User logged in using username as "user1" and password as "user1"$")
public void login() {
driver.findElement(By.name("username")).sendKeys("user1");
driver.findElement(By.name("password")).sendKeys("user1");
driver.findElement(By.name("submit")).click();
Thread.sleep(3000);
}

@Then("^Home page should be display$")
public void verifySuccessful() {
String expectedText = "Logout";
String actualText = driver.Findelement(By.linkText("Logout")).getText();
Assert.assertTrue("Login was successful", expectedText.equals(actualText));
}
}

--

--