Latest post from this blog

How to Manage Test Execution Across Different Browsers and Environments (QA, UAT, Staging)

In real-time automation projects, test execution is never limited to a single browser or a single environment . Applications must be validated across multiple browsers (Chrome, Firefox, Edge) and multiple environments such as QA, UAT, and Staging before going live. A well-designed Selenium + Java + Cucumber automation framework should allow testers to switch browsers and environments easily without changing test scripts . This blog explains how to manage test execution efficiently across different browsers and environments using best practices followed in real projects. Why Multi-Browser and Multi-Environment Testing Is Important Different users use different browsers QA, UAT, and Staging environments have different configurations Bugs may appear only in specific environments or browsers Same test cases must be validated everywhere before production release Common Challenges Testers Face Hardcoded browser names and URLs Maintaining separate test scripts for each environment Browse...

How to Configure the @CucumberOptions Annotation in Your Automation Framework

Introduction 
in Cucumber, the @CucumberOptions annotation helps configure important settings for your tests, such as where to find feature files, step definitions, and how to generate reports. It also lets you filter scenarios and control the test execution flow.

When using Cucumber with TestNG, the test runner class usually extends AbstractTestNGCucumberTests. This class allows Cucumber tests to run as TestNG tests, giving you access to TestNG features like parallel execution, flexible configuration, and detailed reports.

Together, @CucumberOptions and the TestNG runner class that extends AbstractTestNGCucumberTests create a strong testing setup. This combination makes it easier to run and manage your tests while taking advantage of both Cucumber and TestNG features.

Steps to Define Configuration for Running Cucumber Tests

Step 1: Set Up the Runner Class
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;

@RunWith(Cucumber.class)
@CucumberOptions(
features = "src/test/java/com/abc/abc/selenium/features",
glue = {"com/abc/abc/selenium/stepdefinition"},
plugin = {"pretty", "html:target/cucumber-reports.html", "json:target/cucumber.json"},
monochrome = true,
dryRun = false,
tags = "@TrialRun"
)
public class TestNGTestRunner extends AbstractTestNGCucumberTests {
@Override
@DataProvider(parallel=false)
public Object[][]scenarios(){
returnsuper.scenarios();
}
}

Step 2: Understanding Key Parameters

@RunWith(Cucumber.class) to indicate that it's a Cucumber test.

@CucumberOptions Annotation: This annotation provides configuration options to control how the Cucumber tests are executed.

The TestNGTestRunner class is a test runner class used to execute test cases created using Cucumber and Selenium. It extends AbstractTestNGCucumberTests, which is a class provided by Cucumber to integrate with TestNG. The @CucumberOptions annotation is used to configure the Cucumber test like

features: Specifies the path to the feature files that Cucumber should execute.
@CucumberOptions(feature="src/test/java/com/abc/abc/selenium/features")
glue: This is the package where the step definitions (methods implementing the feature steps) are located.
@CucumberOptions(glue="com/abc/abc/selenium/stepdefinition")
plugin: Used to specify output formats for the test results, such as pretty print, HTML, JSON, etc.

pretty: Provides a readable output in the console.

html:target/cucumber-reports.html: Generates an HTML report.

json:target/cucumber.json: Outputs test results in JSON format.
@CucumberOptions(plugin ="pretty","html:target/cucumber-reports.html","json:target/cucumber.json") 
monochrome: If set to true, makes the console output more readable by removing special characters.
@CucumberOptions(pmonochrome = true) 
dryRun:  If set to true, it checks if all the steps in the feature files have corresponding step definitions. In this case, it's set to false, meaning the tests will be executed.
dryRun = false
tags: This is used to specify which tagged features/scenarios should be executed. Here, it's set to execute features/scenarios tagged with @TrialRun.
tags = "@TrialRun"
strict: If set to true, fails the execution if there are undefined or pending steps.

strict = true (This is used in older Cucumber versions; newer versions use strict mode by default.)

The scenarios() method is overridden from AbstractTestNGCucumberTests and is used by TestNG as a @DataProvider. This method returns a two-dimensional array of Object (Object[][]), where each Object[] can be seen as the scenario to be executed and its data. 

The @DataProvider(parallel = false) annotation indicates that the scenarios should not be run in parallel.

Conclusion 
Configuring the @CucumberOptions annotation helps you control the execution flow of your Cucumber tests, making your automation framework flexible and well-organized. With a proper setup, you can run tests efficiently, manage different test scenarios, and generate comprehensive reports to monitor your testing progress.

Comments

Popular posts from this blog

How to Manage Test Execution Across Different Browsers and Environments (QA, UAT, Staging)

Purpose of the StepData Class in Selenium + Java Cucumber Automation Framework

Ensuring Thread Safety in Parallel Test Execution with Selenium, Cucumber, and Java