Cucumber Step Definitions

What are Step Definitions?

Step Definitions are the pieces of code that map Gherkin steps (e.g., Given, When, Then) from your feature files to executable actions. They tell Cucumber what to do when a specific step is encountered during test execution. Written in a programming language like Java, Ruby, or JavaScript, step definitions are the bridge between human-readable Gherkin scenarios and automated tests.

Why Are Step Definitions Important?


How Step Definitions Work

Each step in a Gherkin scenario (e.g., Given the user is on the login page) is matched to a method in a step definition file using annotations (e.g., @Given). The method contains the code to perform the action, such as navigating to a webpage or interacting with a UI element.

Key Components


Writing Your First Step Definition

Let’s create step definitions for a login feature to demonstrate how they work. Assume you have the following feature file (login.feature) in src/test/resources/features:

Feature: User Login
  As a user, I want to log in to the application so that I can access my account.

  Scenario: Successful login with valid credentials
    Given the user is on the login page
    When the user enters "user1" and "pass123" in the login form
    And the user clicks the login button
    Then the user should be redirected to the homepage

Step 1: Set Up the Project

Ensure you have a Cucumber project with Java and Maven, as described in the Installation and Setup post. Your project should have:

Step 2: Create Step Definitions

  1. In src/test/java/steps, create a file named LoginSteps.java.
  2. Add the following code:
package steps;

import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
import io.cucumber.java.en.Then;

public class LoginSteps {
    @Given("the user is on the login page")
    public void userIsOnLoginPage() {
        System.out.println("Navigating to the login page");
        // Placeholder: Add Selenium code to navigate to the login page
    }

    @When("the user enters {string} and {string} in the login form")
    public void userEntersCredentials(String username, String password) {
        System.out.println("Entering username: " + username + ", password: " + password);
        // Placeholder: Add Selenium code to enter credentials
    }

    @When("the user clicks the login button")
    public void userClicksLoginButton() {
        System.out.println("Clicking the login button");
        // Placeholder: Add Selenium code to click the button
    }

    @Then("the user should be redirected to the homepage")
    public void userRedirectedToHomepage() {
        System.out.println("Verifying redirection to the homepage");
        // Placeholder: Add Selenium code to verify the URL
    }
}

Explanation:

Step 3: Run the Test

Run the test using the TestRunner class or the Cucumber CLI:

mvn test

Output:

Navigating to the login page
Entering username: user1, password: pass123
Clicking the login button
Verifying redirection to the homepage

1 Scenarios (1 passed)
4 Steps (4 passed)
0m0.123s

Advanced Step Definitions

Let’s explore more advanced techniques for writing step definitions, including handling multiple scenarios and reusing steps.

Example 1: Reusing Steps Across Scenarios

Add a second scenario to login.feature to test a failed login:

Feature: User Login
  As a user, I want to log in to the application so that I can access my account.

  Scenario: Successful login with valid credentials
    Given the user is on the login page
    When the user enters "user1" and "pass123" in the login form
    And the user clicks the login button
    Then the user should be redirected to the homepage

  Scenario: Failed login with invalid credentials
    Given the user is on the login page
    When the user enters "user1" and "wrongpass" in the login form
    And the user clicks the login button
    Then the user should see an error message

Update Step Definitions:
Add the new step to LoginSteps.java:

@Then("the user should see an error message")
public void userSeesErrorMessage() {
    System.out.println("Verifying error message is displayed");
    // Placeholder: Add Selenium code to verify the error message
}

Explanation:

Output (when run):

Navigating to the login page
Entering username: user1, password: pass123
Clicking the login button
Verifying redirection to the homepage

Navigating to the login page
Entering username: user1, password: wrongpass
Clicking the login button
Verifying error message is displayed

2 Scenarios (2 passed)
8 Steps (8 passed)
0m0.245s

Example 2: Using Scenario Outline

For a Scenario Outline with multiple data sets, step definitions can handle dynamic inputs. Update login.feature:

Feature: User Login
  As a user, I want to log in to the application so that I can access my account.

  Scenario Outline: Login with different credentials
    Given the user is on the login page
    When the user enters "<username>" and "<password>" in the login form
    And the user clicks the login button
    Then the user should see "<result>"

    Examples:
      | username | password   | result                    |
      | user1    | pass123    | Login successful          |
      | user1    | wrongpass  | Invalid credentials       |
      | user2    | pass456    | Login successful          |

Update Step Definitions:
Modify LoginSteps.java to handle the dynamic result:

@Then("the user should see {string}")
public void userSeesResult(String result) {
    System.out.println("Result: " + result);
    // Placeholder: Add Selenium code to verify the result
}

Output (when run):

Navigating to the login page
Entering username: user1, password: pass123
Clicking the login button
Result: Login successful

Navigating to the login page
Entering username: user1, password: wrongpass
Clicking the login button
Result: Invalid credentials

Navigating to the login page
Entering username: user2, password: pass456
Clicking the login button
Result: Login successful

3 Scenarios (3 passed)
12 Steps (12 passed)
0m0.367s

Explanation:


Best Practices for Step Definitions

  1. Keep Them Simple: Each step definition should perform one clear action or verification.
  2. Reuse Steps: Write generic steps with parameters (e.g., {string}) to reuse across scenarios.
  3. Avoid Hardcoding: Use parameters or configuration files instead of hardcoding values like URLs or credentials.
  4. Organize by Feature: Group step definitions by feature (e.g., LoginSteps.java, SearchSteps.java) for clarity.
  5. Use Descriptive Method Names: Name methods clearly to reflect their purpose (e.g., userEntersCredentials).
  6. Handle Edge Cases: Account for variations in step text using regular expressions or Cucumber expressions.

Troubleshooting Step Definitions


Tips for Beginners

🤖
PrepCampusPlus AI Tutor
Scroll to Top