Combining Scenarios, Backgrounds, and Scenario Outlines
Until now we have learned about Scenarios, Steps, Background, and Scenario Outline individually. But when a BA or a PO has to write the Feature file, they have to combine all these keywords to come up with a very efficient and expressive Feature file.
Consider writing a Feature file for a login functionality where the latter meets the following criteria:
- The user should get an option to log in from the application's home page
- To log in, a user should have a username and password
- After a successful login, the user should be redirected to the home page
- In case of an unsuccessful login, the user should get the appropriate message
- The user should also get an option to register new users on the home page
- The user should also be able to verify which users exist in the application (this Feature is not present on the GitHub landing page but has been added for to clarify concepts)
Note
All these requirements are specific to the behavior of the application, and none of them are concerned with how you implement these requirements.
How to do it…
Now we are going to use all the keywords we have explored until now, and we are going to create a login.feature
file that specifies all the aforementioned requirements:
Feature: login Page In order to test login page As a Registered user I want to specify the login conditions Scenario: login flow Given user is on Application landing page And Sign in button is present on screen When user clicks on Sign in button Then user is displayed login screen When user enters "ShankarGarg" in username field And user enters "123456" in password field And user clicks Sign in button Then user is on home page And title of home page is "GitHub" Scenario Outline: Login fail - possible combinations Given user is on Application landing page When user clicks on Sign in button Then user is displayed login screen When user enters "<UserName>" in username field And user enters "<Password>" in password field And user clicks Sign in button Then user gets login failed error message Examples: | UserName | Password | | wrongusername | 123456 | | ShankarGarg | wrongpassword | | wrongusername | wrongpassword | Scenario: Existing user Verification Given user is on Application landing page Then we verify following user exists | Name | Email | Phone | | Shankar | [email protected] | 999 | | Ram | [email protected] | 888 | | Sham | [email protected] | 666 | Scenario: New User Registration Given user is on Application landing page When user enters "ShankarGarg" in username field And user enters "[email protected]" in password field And user enters "123456" in password field And user clicks on Signup for GitHub button Then user is successfully registered
How it works…
Here we have combined all the keywords and concepts discussed until now in this chapter. Let's go through each requirement one by one and analyze how and with which keyword we specified these requirements:
- User should get an option to log in from the application home page—Scenario
- For login, a user should have a username and password—Scenario
- After successful login, the user should be redirected to the home page—Scenario
- In case of unsuccessful login, the user should get the appropriate message—Scenario Outline
- The user should also get an option to register new users on the home page—Scenario
- The user should also be able to verify which users exist in the application—Data Table