Latest post from this blog

Test Scenarios vs. Test Cases: Understanding the Basics

What Are Test Scenarios Test scenarios represent high-level ideas or conditions that need to be validated to ensure the application works as expected. They provide a broader perspective and are typically used during the test planning phase. Purpose: To capture the "what to test" without going into granular details. Example of a Test Scenario: Verify that a user can successfully log in to the application using valid credentials. Verify the behavior of the login page when invalid credentials are entered. Verify the application behavior when the login button is clicked without entering any credentials. What Are Test Cases Test cases are detailed documents that define the specific steps to execute a test. They cover inputs, execution steps, expected results, and actual outcomes. Purpose: To guide the tester step-by-step on "how to test." Example of a Test Case (for the first scenario): Test Case ID TC_01_Login_Valid_Credentials Test Scenario Verify user login with val...

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

Common Exception in Selenium

Test Scenarios vs. Test Cases: Understanding the Basics

Handling Shadow DOM in Selenium WebDriver

Handling HTTPS Websites and SSL Certificate Errors in Selenium

Normalize-space Function In Xpath