Q4 of 21 · BDD / Cucumber
What are step definitions, and how does Cucumber match a Gherkin step to code?
BDD / CucumberJuniorbddcucumberstep-definitionsglue-code
Short answer
Short answer: Step definitions are the glue code that maps each Gherkin step to an executable function. Cucumber matches steps using regular expressions or Cucumber Expressions — if a step text matches the pattern, that function runs.
Detail
Java example:
// Feature step:
// When the user submits the form with email "alice@example.com"
@When("the user submits the form with email {string}")
public void userSubmitsFormWithEmail(String email) {
loginPage.enterEmail(email);
loginPage.clickSubmit();
}
JavaScript / Playwright example:
const { When } = require('@cucumber/cucumber');
When('the user submits the form with email {string}', async function(email) {
await this.page.fill('[data-testid="email"]', email);
await this.page.click('[data-testid="submit"]');
});
How matching works:
- Cucumber Expressions (preferred) use typed placeholders:
{string},{int},{float},{word}. These auto-convert the captured value to the right type. - Regular expressions offer more flexibility but are harder to read.
- If no step definition matches, Cucumber reports "Undefined step" and generates a snippet you can use as a starting point.
- If two step definitions match the same step, Cucumber reports "Ambiguous" — you must make them more specific.
Step definition files should be in a dedicated step_definitions (JS) or stepdefs (Java) directory. They contain only glue logic and delegate real actions to Page Objects or API clients.
// EXAMPLE
@Given("a user account already exists with email {string}")
public void userAccountExistsWithEmail(String email) {
dbHelper.insertUser(email, "Test", "User");
}
@Then("an error message {string} is displayed")
public void errorMessageIsDisplayed(String expectedMessage) {
String actual = registrationPage.getErrorMessage();
assertThat(actual).isEqualTo(expectedMessage);
}// WHAT INTERVIEWERS LOOK FOR
That step definitions are the bridge between Gherkin and code. How Cucumber Expressions work ({string}, {int}). Delegation to Page Objects rather than putting UI logic in step definitions.
// COMMON PITFALL
Putting all test logic (locators, assertions, data setup) directly in step definitions. They should be thin — just call a helper.