Cucumber Cheat Sheet
Gherkin Keywords
- Feature
- Scenario
- Background
- Given
- When
- Then
- And
- But
- Scenario Outline
- Examples
Feature File
Gherkin
Copy
Feature: Login functionality Scenario: Successful login Given the user is on the login page When the user enters valid credentials Then the user should be logged in successfully
Step Definition File
Java
Copy
@Given("the user is on the login page") public void navigateToLoginPage() { // Code to navigate to the login page System.out.println("User is on the login page"); } @When("the user enters valid credentials") public void enterValidCredentials() { // Code to enter valid login credentials System.out.println("User enters valid credentials"); } @Then("the user should be logged in successfully") public void verifySuccessfulLogin() { // Code to verify successful login System.out.println("User is logged in successfully"); }
Parameterization
Gherkin
Copy
Feature: Login functionality Scenario: Successful login Given the user is on the login page When the user enters username "root" and password "admin" Then the user should be logged in successfully
Java
Copy
@When("the user enters username {string} and password {string}") public void theUserEntersUsernameRootAndPasswordAdmin(String username, String password) { System.out.println("The user enters username " + username + " and password " + password + "."); }
Scenario Outline
Gherkin
Copy
Feature: Login functionality Scenario Outline: Successful login Given the user is on the login page When the user enters username <username> and password <password> Then the user should be logged in successfully Examples: | username | password | | "root" | "admin" | | "admin" | "admin" |
Java
Copy
@When("the user enters username {string} and password {string}") public void theUserEntersUsernameRootAndPasswordAdmin(String username, String password) { System.out.println("The user enters username " + username + " and password " + password + "."); }
Background
Gherkin
Copy
Feature: Login functionality Background: Given the user is on the login page Scenario: Successful login When the user enters username "root" and password "admin" Then the user should be logged in successfully
Java
Copy
@Given("the user is on the login page") public void navigateToLoginPage() { // Code to navigate to the login page System.out.println("User is on the login page"); }
Data Table
Gherkin
Copy
Feature: User registration Scenario: Successful User registration Given the following user details | Name | Age | City | | John | 25 | New York | When the user submits the registration form Then the user should be registered successfully
Java
Copy
@Given("the following user details") public void enterUserDetails(DataTable userDetails) { List<Map<String, String>> data = userDetails.asMaps(String.class, String.class); for (Map<String, String> row : data) { String name = row.get("Name"); String age = row.get("Age"); String city = row.get("City"); System.out.println("Name : " + name); System.out.println("Age : " + age); System.out.println("City : " + city); } }
Hooks
Java
Copy
import io.cucumber.java.Before; import io.cucumber.java.After; public class Hooks { @Before public void beforeScenario() { System.out.println("Setting up the test environment..."); // Perform setup tasks here, like launching browser or connecting to database } @After public void afterScenario() { System.out.println("Cleaning up the test environment..."); // Perform cleanup tasks here, like closing browser or disconnecting from database } }
Tags
Gherkin
Copy
@smoke Feature: Login functionality @positive @regression Scenario: Successful login Given the user is on the login page When the user enters valid credentials Then the user should be logged in successfully
Test Runner
Java
Copy
import org.junit.runner.RunWith; import io.cucumber.junit.Cucumber; import io.cucumber.junit.CucumberOptions; @RunWith(Cucumber.class) @CucumberOptions( features = "src/test/resources/features", // Location of your feature files glue = "com.example.stepdefinitions", // Package where your step definitions are located plugin = {"pretty", "html:target/cucumber-reports"} // Reporting options ) public class TestRunner { // This class is empty because it serves as an entry point for JUnit to run Cucumber tests }