Cucumber Options in Cucumber

  

In Cucumber@CucumberOptions is an important annotation used in the Test Runner class to define and customize the behavior of the Cucumber framework while executing tests. This annotation comes from the io.cucumber.junit or cucumber.api package (depending on the version you're using).


What is @CucumberOptions?

@CucumberOptions is used to configure various execution parameters for running Cucumber tests. It helps define where feature files and step definitions are located, how the results should be reported, and what tags or filters to apply during test execution.


Syntax Example

@RunWith(Cucumber.class)
@CucumberOptions(
    features = "src/test/resources/features",
    glue = "stepdefinitions",
    plugin = {"pretty", "html:target/cucumber-report.html"},
    monochrome = true,
    tags = "@SmokeTest"
)
public class TestRunner {
}




Parameters of @CucumberOptions


ParameterDescription
featuresPath to the .feature files. It can be a directory or specific file path.
gluePackage name(s) where step definitions and hooks are defined.
pluginUsed for report generation. Common plugins include "pretty", "html:target/report.html", "json:target/report.json" etc.
monochromeMakes the console output more readable by removing unnecessary characters. Set to true or false.
tagsFilters which scenarios or features to run based on assigned tags. Useful for running subsets like @SmokeTest, @Regression.
dryRunIf true, it checks if every Step in the feature file has a corresponding Step Definition without executing the tests.
strictIf true, fails execution if there are undefined or pending steps.
nameRuns scenarios whose names match the given regular expression.
snippetsControls the style of generated snippets (CAMELCASE or UNDERSCORE).



Example with Explanation

@CucumberOptions(
    features = "src/test/resources/features/Login.feature", // specific feature file
    glue = "stepdefinitions.login",                          // package with step defs
    plugin = {
        "pretty",
        "json:target/jsonReports/report.json",
        "html:target/htmlReports"
    },
    tags = "@Regression and not @WIP",                       // include/exclude based on tags
    monochrome = true,                                       // clean console output
    dryRun = false,                                          // actually runs the tests
    strict = true                                            // fails on undefined steps
)


When and Where Is It Used?

  • Placed above the Test Runner class that runs with @RunWith(Cucumber.class).

  • Helps control the execution and reporting of Cucumber tests.

  • Allows filtering which scenarios to run in large test suites.

  • Provides integration with tools like Jenkins and Allure via plugins.



Important Points:

@CucumberOptions gives you flexibility and control over how your Cucumber test suite runs by specifying:

  • where your tests are,

  • how they should be reported,

  • which tests to run,

  • and how results should be formatted.

What are Gherkins Keywords in Cucumber

  

Gherkin Keywords in Cucumber

Gherkin is the language used to write Cucumber feature files. It follows a structured syntax using keywords that make it easy for both technical and non-technical stakeholders to understand test scenarios in Behavior-Driven Development (BDD).


What is Gherkin?

  • Gherkin is a domain-specific language (DSL) for writing human-readable tests.

  • Each line in a Gherkin file begins with a keyword, followed by natural language describing the behavior.

  • File extension: .feature




Gherkin Keywords List

KeywordDescription
FeatureDefines the feature or functionality under test.
ScenarioDefines a single concrete example or test case.
Scenario OutlineAllows running the same scenario with different data sets.
ExamplesProvides the data table for Scenario Outline.
GivenDescribes the initial context or preconditions.
WhenDescribes the action or event (trigger).
ThenDescribes the expected outcome or result.
AndUsed to add more conditions to Given, When, or Then.
ButAdds a negative condition to a step.
BackgroundDefines common steps shared by all scenarios in a feature file.
*Wildcard, can replace Given, When, Then, etc., to improve readability.




Example of Gherkin Feature File

Feature: Login functionality

  Background:
    Given the user is on the login page

  Scenario: Successful login
    When the user enters valid credentials
    Then the user should be redirected to the dashboard

  Scenario: Unsuccessful login
    When the user enters invalid credentials
    Then an error message should be displayed




Scenario Outline Example with Examples Table

Scenario Outline: Login with multiple credentials
  Given the user is on the login page
  When the user enters username "<username>" and password "<password>"
  Then the login result should be "<result>"

  Examples:
    | username | password | result         |
    | user1    | pass1    | success        |
    | user2    | wrong    | error message  |




Important Points:

  • Gherkin supports multiple languages (like French, Hindi, etc.).

  • Keywords must be followed by plain language sentences.

  • Each scenario is independent (except for Background steps).

Parameterization in Cucumber

  

What is Parameterization in Cucumber?

Parameterization in Cucumber allows you to pass different input data into your Gherkin steps (feature files), enabling reusability of steps and data-driven testing. Instead of writing separate scenarios for each data input, you can parameterize the steps using:

  1. Regular Expressions (Step Definition Parameters)

  2. Scenario Outline and Examples Table



1. Parameterization using Step Definition Parameters

You can define steps with placeholders using double quotes (") and capture them in the step definitions with regex or Cucumber expressions.


Feature File (Login.feature)

Feature: Login Feature

  Scenario: Login with valid credentials
    Given User logs in with username "john" and password "password123"




Step Definition in Java

import io.cucumber.java.en.Given;

public class LoginSteps {

    @Given("User logs in with username {string} and password {string}")
    public void user_logs_in_with_credentials(String username, String password) {
        System.out.println("Username: " + username);
        System.out.println("Password: " + password);
        // Add login logic here
    }
}



2. Parameterization using Scenario Outline and Examples Table

This is useful when you want to run the same scenario multiple times with different inputs.


Feature File with Scenario Outline (Login.feature)

Feature: Login Feature

  Scenario Outline: Login with multiple credentials
    Given User logs in with username "<username>" and password "<password>"

    Examples:
      | username | password     |
      | john     | password123  |
      | alice    | qwerty456    |
      | bob      | 12345        |





Step Definition in Java

import io.cucumber.java.en.Given;

public class LoginSteps {

    @Given("User logs in with username {string} and password {string}")
    public void user_logs_in_with_credentials(String username, String password) {
        System.out.println("Username: " + username);
        System.out.println("Password: " + password);
        // Add logic to perform login here
    }
}




Important Points

FeatureBenefit
Step ParameterizationReusable steps with dynamic data
Scenario Outline + ExamplesData-driven testing made easy
Reduces code duplicationLess maintenance, more clarity

Data Driven Testing using Cucumber

  

What is Data-Driven Testing in Cucumber Using Examples Keyword?

Data-Driven Testing in Cucumber is a method that allows you to run the same scenario multiple times with different sets of data. This is done using the Scenario Outline and Examples keywords in the Gherkin language.


Why Use Data-Driven Testing?

  • Eliminates code duplication

  • Increases test coverage

  • Allows testing edge cases easily

  • Makes test scenarios readable and maintainable


 Gherkin Keywords Used

KeywordDescription
Scenario OutlineUsed when you want to run the same scenario with multiple data sets
ExamplesContains the table of input values that will be injected into the scenario


Syntax Example

Feature File: login.feature

Feature: Login Feature

  Scenario Outline: Valid login with multiple credentials
    Given User is on the login page
    When User enters username "<username>" and password "<password>"
    Then User should see the dashboard

    Examples:
      | username | password  |
      | user1    | pass123   |
      | user2    | password1 |
      | admin    | admin123  |

 

Step Definition in Java

File: LoginSteps.java

package stepDefinitions;

import io.cucumber.java.en.*;

public class LoginSteps {

    @Given("User is on the login page")
    public void user_is_on_the_login_page() {
        System.out.println("User navigates to login page");
    }

    @When("User enters username {string} and password {string}")
    public void user_enters_username_and_password(String username, String password) {
        System.out.println("Entered Username: " + username);
        System.out.println("Entered Password: " + password);
    }

    @Then("User should see the dashboard")
    public void user_should_see_the_dashboard() {
        System.out.println("User lands on the dashboard");
    }
}




Test Runner Class

package testRunner;

import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;

@RunWith(Cucumber.class)
@CucumberOptions(
    features = "src/test/resources/features",
    glue = {"stepDefinitions"},
    plugin = {"pretty", "html:target/cucumber-reports.html"},
    monochrome = true
)
public class TestRunner {
}



Project Structure

src/
 └── test/
     └── java/
         └── stepDefinitions/
             └── LoginSteps.java
         └── testRunner/
             └── TestRunner.java
     └── resources/
         └── features/
             └── login.feature



Output

User navigates to login page
Entered Username: user1
Entered Password: pass123
User lands on the dashboard

User navigates to login page
Entered Username: user2
Entered Password: password1
User lands on the dashboard

User navigates to login page
Entered Username: admin
Entered Password: admin123
User lands on the dashboard




Important Points:


FeatureDescription
Data-Driven TestingRun one scenario with multiple data sets
Scenario OutlineDefines a scenario template
ExamplesData table used for substitution in the scenario
BenefitAvoids duplication, increases maintainability and test coverage

What is Scenario Outline in Cucumber

  

What is Scenario Outline in Cucumber?

Scenario Outline in Cucumber is used when you want to run the same scenario multiple times with different sets of data. This is extremely useful for data-driven testing.

It avoids repeating the same scenario logic and makes the feature file clean, readable, and maintainable.


Why Use Scenario Outline?

  • To avoid writing multiple similar scenarios.

  • To test the same logic with different data inputs.

  • To improve test coverage.


How It Works

  • Scenario Outline is written like a normal scenario.
  • Examples keyword is used to provide the input data in a tabular format.
  • Each row in the Examples table is treated as a new execution of the scenario.
  • The placeholders (example: <username><password>) are replaced with values from the Examples table during execution.



Syntax of Scenario Outline

Scenario Outline: Scenario title
  Given some step with <parameter1>
  When action with <parameter2>
  Then expected result

Examples:
  | parameter1 | parameter2 |
  | value1     | value2     |
  | value3     | value4     |



Example: Login Functionality

Feature File: login.feature


Feature: Login Feature

  Scenario Outline: Valid login with different users
    Given User is on Login page
    When User enters username "<username>" and password "<password>"
    Then Login should be successful

    Examples:
      | username | password  |
      | user1    | pass123   |
      | user2    | pass456   |
      | admin    | admin@123 |




Step Definition File (Java)

package stepdefinitions;

import io.cucumber.java.en.*;

public class LoginSteps {

    @Given("User is on Login page")
    public void user_is_on_login_page() {
        System.out.println("User is on Login page");
    }

    @When("User enters username {string} and password {string}")
    public void user_enters_credentials(String username, String password) {
        System.out.println("Username: " + username);
        System.out.println("Password: " + password);
    }

    @Then("Login should be successful")
    public void login_should_be_successful() {
        System.out.println("Login successful!");
    }
}


How It Runs

Cucumber will run the scenario 3 times, once for each row in the Examples table:

  • user1 / pass123
  • user2 / pass456
  • admin / admin@123


Benefits of Scenario Outline

  • Reusability of test steps.

  • Better test coverage for multiple inputs.

  • Easy to maintain and extend.

What is Scenario in Cucumber

 

What is a Scenario in Cucumber?

Scenario in Cucumber is a concrete example that illustrates a business rule or requirement. It is written in Gherkin language, which is designed to be easily understandable by non-technical stakeholders such as business analysts and testers.

Each scenario represents a single test case that describes a specific functionality or behavior of the system under test, using the Given-When-Then structure.


Gherkin Keywords used in Scenario

  • Given: Describes the initial context or state of the system.

  • When: Specifies the action or event performed by the user.

  • Then: Describes the expected outcome or result.

  • AndBut: Used to add more steps for clarity.



Example: Scenario in Cucumber

Feature File - Login.feature



Feature: Login functionality for a web application

  Scenario: Successful login with valid credentials
    Given User is on the login page
    When User enters valid username and password
    And clicks on the login button
    Then User should be redirected to the home page





Step Definition File - LoginStepDefinitions.java

package stepDefinitions;

import io.cucumber.java.en.*;

public class LoginStepDefinitions {

    @Given("User is on the login page")
    public void user_is_on_login_page() {
        System.out.println("Navigated to login page");
        // You can use Selenium code like: driver.get("http://example.com/login");
    }

    @When("User enters valid username and password")
    public void user_enters_credentials() {
        System.out.println("Entered username and password");
        // Selenium code to enter username and password
    }

    @When("clicks on the login button")
    public void user_clicks_login() {
        System.out.println("Clicked login button");
        // Selenium code to click login button
    }

    @Then("User should be redirected to the home page")
    public void user_redirected_home() {
        System.out.println("User is on the home page");
        // Selenium code to verify redirection
    }
}





Test Runner File - TestRunner.java

package runners;

import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;

@RunWith(Cucumber.class)
@CucumberOptions(
    features = "src/test/resources/features",
    glue = "stepDefinitions"
)
public class TestRunner {
}





Important Points:

TermDescription
ScenarioA single test case describing a user story or feature behavior
Feature FileContains one or more scenarios
Step DefinitionsJava methods mapped to each Gherkin step
Runner ClassExecutes the scenarios with JUnit or TestNG