Skip to content

Cucumber

Functional testing brings elements of BDD with Gherkin back into play. Using Cucumber, teams can leverage their alignment and work that produced Gherkin scenarios and translate that into executable code. When combined with UI test, Gherkin becomes the source of a team's functional test automation suite.

Mapping Gherkin to step definitions

Cucumber uses regular expressions to match Gherkin steps to glue code, or step definition code. The Gherkin steps are contained within scenarios that are defined inside the project's .feature files. Upon execution, Cucumber will map those steps to functions found inside the project's glue code directory.

  When we click on new account button
@When("we click on new account button")
public void clickNewAccountButton() {
  driver.findElement(By.id("newAcctBtn")).click();
}

Building a reusable framework

As teams begin adopting test automation with Cucumber, they quickly see the benefits of its flexibility. Cucumber allows you to develop templated step definition code, which enables teams to build out a framework of shared steps to be leveraged across the team's project. This is also a great way to innersource and expand a testing framework in the organization.

Scenario: the user can login to the application
  Given we are on the home page
  Given we click on the signIn button
  When we enter testuser123 in the username field
  And we enter password321 in the password field
  And we click the login button
  Then we will see the welcome page
@When("we enter {} in the {} field")
public void enterText(String value, String element) {
  driver.findElement(By.id(element).sendText(value));
}

@When("we click on the {} button") {
  driver.findElement(By.id(element).click();
}

Tags

Users can specify which scenarios or features to run using tags. This is a powerful feature which can be implemented at runtime or in the continuous delivery pipeline. Teams can implement a variety of tagging strategies, including environments, tickets, sprints, etc. These are defined by default in Cucumber's runner class, but is often is overridden at runtime.

@regression
Scenario: ...

@smoke
Scenario: ...

@integration-qa @LAH-323
Scenario: ...

Tags can be applied at the Feature and Scenario level, and follow logical execution (AND, OR, NOT)