Cucumber Gherkin - simon-sai/selenium-appium-java GitHub Wiki

(All the examples below are not from the framework; they are simple versions to help understand the concept)

Introduction to Cucumber and Gherkin

  • Cucumber is a BDD framework that allows you to write tests in a human-readable format.
  • Gherkin is the language used in Cucumber to define test cases. It uses simple, structured sentences to describe features and behaviors of your system.

Basic Syntax of Gherkin

Gherkin has a specific structure that includes:

  • Feature: Describes the functionality or feature you’re testing.
  • Scenario: Represents a specific situation that tests a feature.
  • Given: Describes the initial context (preconditions).
  • When: Describes the event or action that triggers the behavior.
  • Then: Describes the expected outcome or result.
  • And/But: Additional conditions that can be used with Given/When/Then.

Example of a Gherkin Feature File

Feature: Login functionality

  Scenario: Successful login with valid credentials
    Given the user is on the login page
    When the user enters a valid username and password
    Then the user should be redirected to the homepage

  Scenario: Unsuccessful login with invalid credentials
    Given the user is on the login page
    When the user enters an invalid username or password
    Then the user should see an error message

Steps Definition

Each Gherkin step in a scenario (Given, When, Then) needs to be mapped to a step definition in the code. Example in Java:

public class StepDefinitions {
  
  @Given("the user is on the login page")
  public void userIsOnLoginPage() {
    // Code to navigate to the login page
  }
  
  @When("the user enters a valid username and password")
  public void userEntersValidCredentials() {
    // Code to enter username and password
  }
  
  @Then("the user should be redirected to the homepage")
  public void userIsRedirectedToHomePage() {
    // Code to check the redirection to the homepage
  }
}

Use Background for Repeated Steps

If multiple scenarios share the same steps at the beginning (like navigating to a login page), you can use Background to avoid repeating them in every scenario.

Feature: Login functionality

  Background:
    Given the user is on the login page

  Scenario: Successful login with valid credentials
    When the user enters a valid username and password
    Then the user should be redirected to the homepage

  Scenario: Unsuccessful login with invalid credentials
    When the user enters an invalid username or password
    Then the user should see an error message

Use Scenario Outline for Data-Driven Testing

When you need to test the same scenario with different sets of data, you can use Scenario Outline with Examples. This avoids duplicating similar scenarios.

Scenario Outline: Login with multiple credentials
  Given the user is on the login page
  When the user logs in with username "<username>" and password "<password>"
  Then the user should be redirected to the homepage

  Examples:
    | username  | password    |
    | testuser1 | password123 |
    | testuser2 | password456 |

Use Tags to Organize and Filter Tests

You can organize your scenarios using tags. Tags allow you to run only a specific subset of scenarios, which is useful when you have many test cases or different testing environments.

@smoke
Scenario: Successful login with valid credentials
  Given the user is on the login page
  When the user enters a valid username and password
  Then the user should be redirected to the homepage

@regression
Scenario: Unsuccessful login with invalid credentials
  Given the user is on the login page
  When the user enters an invalid username or password
  Then the user should see an error message

Tips for Writing Good Scenarios

  • Keep scenarios short and simple.
  • Use concrete examples to make the scenario easy to understand.
  • Avoid using technical details in the feature file; keep it business-focused.

Conclusion

Gherkin allows you to create human-readable tests that anyone on your team (including non-technical stakeholders) can understand. With the step definitions written in a programming language, Cucumber bridges the gap between BDD and automated testing.

⚠️ **GitHub.com Fallback** ⚠️