4. Step Definitions

Estimated reading: 3 minutes 24 views

In Cucumber, step definitions link the steps in a Gherkin scenario to the actual code that performs the actions described in the scenario. Step definitions allow Cucumber to execute the tests by matching the natural language steps (Given, When, Then) with specific code implementations.

Mapping Gherkin Steps to Step Definition Methods

Each step in a Gherkin scenario corresponds to a method in the programming language you’re using (e.g., Java, Ruby, etc.). The step definition method implements the behavior specified in the scenario step. When Cucumber runs the tests, it matches each Gherkin step to the corresponding method and executes it.

For example, the step Given the user is on the login page from a feature file would be mapped to a method in the step definition file that contains the logic to open the login page.

Writing Step Definitions Using Cucumber Annotations (@Given, @When, @Then)

Cucumber provides annotations such as @Given, @When, and @Then to define methods that correspond to specific steps in the feature files. These annotations are used to mark methods as step definitions.

  • @Given: Used to define preconditions or initial context.
  • @When: Defines actions or events that trigger behavior.
  • @Then: Defines the expected outcome or result after an action.
  • @And / @But: Used for additional steps or conditions.

Example of Step Definitions in Java (Using Cucumber and Selenium WebDriver)

				
					import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
import io.cucumber.java.en.Then;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class LoginSteps {
    WebDriver driver;

    @Given("^the user is on the login page$")
    public void theUserIsOnLoginPage() {
        driver = new ChromeDriver();
        driver.get("https://example.com/login");
    }

    @When("^the user enters a valid username and password$")
    public void theUserEntersValidCredentials() {
        driver.findElement(By.id("username")).sendKeys("validUser");
        driver.findElement(By.id("password")).sendKeys("validPassword");
        driver.findElement(By.id("loginButton")).click();
    }

    @Then("^the user should be redirected to the dashboard page$")
    public void theUserIsRedirectedToDashboard() {
        assert driver.getCurrentUrl().contains("dashboard");
        driver.quit();
    }
}

				
			

Sharing Step Definitions Across Multiple Feature Files

In Cucumber, step definitions are reusable across multiple feature files. This is especially useful when the same steps appear in different scenarios or feature files. To share step definitions, you can place them in a separate class or file and make sure it’s accessible to all feature files in your project.

  • Create Reusable Step Definitions: You can create a class with common step definitions that can be shared across multiple feature files.
  • Step Definition Class: Each step definition file should correspond to a feature file (or a group of related features), and the steps should be organized to promote reuse.

Example of Sharing Step Definitions Across Feature Files

				
					public class CommonSteps {
    @Given("^the user is logged in$")
    public void theUserIsLoggedIn() {
        // Logic to log the user in
    }

    @When("^the user navigates to the profile page$")
    public void theUserNavigatesToProfilePage() {
        // Logic to navigate to the profile page
    }
}

				
			

This class can be used across different feature files that need the same steps (e.g., logging in and navigating to the profile page).

Conclusion

Step definitions are crucial in Cucumber for executing tests based on Gherkin scenarios. By mapping Gherkin steps to step definition methods using annotations like @Given, @When, and @Then, you can define the behavior and actions of your application. Sharing step definitions across multiple feature files promotes reusability and keeps your code clean and organized.

Leave a Comment

Share this Doc

4. Step Definitions

Or copy link

CONTENTS