Writing a Feature file with multiple Scenarios
Feature files contain possible Scenarios for a particular functionality. This is like writing all possible requirements that a Feature should meet when it is implemented. So let's write these specifications in Cucumber in the following section.
How to do it…
We will create a new Feature file called home_page.feature
, which will cover the functionality of the default content of https://github.com/, the Bootcamp section, and the top banner content. We will create a different Scenario for each functionality. Take a look at the following screenshot for more clarity:
Feature: Home Page In order to test Home Page of application As a Registered user I want to specify the features of home page Scenario: Home Page Default content Given user is on Github home page Then user gets a GitHub bootcamp section And username is also displayed on right corner Scenario: GitHub Bootcamp Section Given user is on GitHub home page When user focuses on GitHub Bootcamp Section Then user gets an option to setup git And user gets an option to create repository And user gets an option to Fork Repository And user gets an option to work together Scenario: Top Banner content Given user is on GitHub home page When user focuses on Top Banner Then user gets an option of home page And user gets an option to search And user gets settings options And user gets an option to logout
How it works…
A Cucumber Feature file can have any number of Scenarios as required. Some points to keep in mind are as follows:
- One Feature file normally focuses on one functionality of the application, such as login page, home page, and so on.
- One Scenario refers to one sub-Feature of that functionality, such as the new customer page, delete customer page, and so on.
When we have multiple Scenarios in a Feature file, we should always follow the Stateless Scenarios Guideline. Let's understand this guideline better—each Scenario must make sense and should be executed independently of any other Scenario. The result of one Scenario/Feature should not affect the other Scenario.
These are the benefits of independent Scenarios:
- Feature files are easier and fun to understand
- You can only run a subset of Scenarios, as all the required Steps of a Scenario are mentioned in the Scenario itself
- In comparison to dependent Scenarios, independent Scenarios will be more eligible candidates for parallel execution
Tip
Downloading the example code
You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.