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...

Controlling Execution Order by Naming Feature Files Alphabetically in Cucumber Framework

Introduction

In the Cucumber framework, you can control the order of execution of your feature files by naming them in the sequence you want them to be executed. Cucumber executes feature files in alphabetical order by default. So, you can prefix your feature files with numbers or letters to control their execution order.

Consider the following three features in a basic application: Login, HomePage, and Registration.
Here’s how your feature files may look initially:

  • Login.feature
  • HomePage.feature
  • Registration.feature

By default, Cucumber might execute these in an unpredictable order. To control the execution sequence, you need to rename them alphabetically or numerically.

Rename Feature Files Alphabetically

For example, you can rename your feature files like this:

  • 1_Login.feature
  • 2_HomePage.feature
  • 3_Registration.feature

And, add the same tag (@Sanity) in each feature file:


In the TestNG runner class, you specify the tag @Sanity to run the tests.

Execution Flow

When you run the tests, Cucumber will:
  • Identify all the feature files that contain the @Sanity tag.
  • Execute them in alphabetical order based on their file names, even though all of them have the same tag.
Why It Works
When you specify a tag in the TestNG runner (@Sanity in this case), Cucumber looks for all the feature files that match this tag. It then executes those files in alphabetical order based on their file names, unless a custom order is specified (for example, in testng.xml or through a custom runner). By numbering or alphabetically naming the feature files, you ensure that Cucumber follows a specific execution sequence.

Conclusion
Controlling the execution order of feature files in Cucumber is often necessary to ensure a logical flow, especially in projects where one feature depends on the outcome of another. By naming the feature files alphabetically or numerically, you can achieve a defined execution sequence effortlessly. This approach is ideal for teams using JUnit or TestNG with Cucumber, as it requires no extra setup and works out of the box.

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