Step definition in Cucumber Framework

Step Definition

Once the feature file is ready with scenarios (test cases), the next step is to link those test steps to the actual automation code.

This is done using Step Definitions. 

A step definition in BDD is a Java method that maps each Gherkin step (Given, When, Then) to actual automation code. It connects the plain-language steps from the feature file to the underlying Selenium actions. When Cucumber runs the scripts, each step in the scenario looks for a matching step definition method to execute.

Think of it like the test class in a normal Selenium-Java Page Object Model setup, where all action methods are grouped and executed.

Create Step Definitions inside the project

In your Selenium-Java Maven project:

     1. Navigate to: src/test/java and create a new Java package named stepDefinitions.
     2. Inside that package, create a new Java class (LoginSteps.java)
     3. Write methods annotated with @Given, @When, and @Then

These annotations come from the Cucumber Java library and are used to match the steps in the feature file.

First, we need a feature file from login.feature

Feature: Login Feature
  Scenario: Login as a valid user
    Given the user is on the login page
    When the user enters a valid username and password
    Then the user should be logged in to the application


Next, Step Definition Class: LoginSteps.java

package stepDefinitions;

import io.cucumber.java.en.*;
import pages.LoginPage;

public class LoginSteps {

    LoginPage loginPage;

    @Given("the user is on the login page")
    public void the_user_is_on_the_login_page() {
        System.out.println("QA Feast site is opened");
        loginPage = new LoginPage(); 
    }

    @When("the user enters a valid username and password")
    public void the_user_enters_valid_username_and_password() {
        loginPage.enterUsername("testuser");
        loginPage.enterPassword("password123");
        loginPage.clickLogin();
    }

    @Then("the user should be logged in to the application")
    public void the_user_should_be_logged_in() {
        loginPage.verifyLoginSuccess();
        System.out.println("Test execution completed");
    }
}


Supporting Page Object Class: LoginPage.java

The above LoginSteps.java script is written based on the Page Object Model. Below is the supporting page class LoginPage.java, where all the actions are defined. These actions are then grouped and called from the step definitions based on the scenario steps.

package pages;

public class LoginPage {

    public LoginPage() {
        // Constructor without WebDriver
    }

    public void enterUsername(String username) {
        System.out.println(username + " is entered");
    }

    public void enterPassword(String password) {
        System.out.println(password + " is entered");
    }

    public void clickLogin() {
        System.out.println("Login button is clicked");
    }

    public void verifyLoginSuccess() {
        System.out.println("Dashboard page is displayed");
    }
}

This is the base structure; you can run the automation using this setup. Upcoming topics will cover other important features of Cucumber. To execute the script, follow this link: Cucumber Test Execution
 

Related Tutorials