-
Book Overview & Buying
-
Table Of Contents
Cucumber Cookbook
By :
In the previous recipe, we learnt how we can send test data in Steps itself, which can be used by the application for processing. Until now, the data was associated with one particular Step (implemented by Data Tables); but what if I want to send data which is related to the whole Scenario, and what if I want to repeat all the Steps of a Scenario again and again for different sets of data? This is a classic case of data-driven testing. This will be implemented by using a Scenario Outline.
Let's create a Scenario for a login functionality where we want to test all the possible Scenarios where the login will fail. Based on what we have learned so far, this is how our Scenario will look:
Scenario: login fail - wrong username
Given user is on Application landing page
When user clicks on Sign in button
Then user is displayed login screen
When user enters "wrongusername" in username field
And user enters "123456" in password field
And user clicks Sign in button
Then user gets login failed error message
Scenario: login fail - wrong password
Given user is on Application landing page
When user clicks on Sign in button
Then user is displayed login screen
When user enters "ShankarGarg" in username field
And user enters "wrongpassword" in password field
And user clicks Sign in button
Then user gets login failed error messageIn terms of syntax, there is no problem in this code. Cucumber will treat it as well as any other, but the problem is for the person writing the Feature file. If you look closely, only the dataset is changing and all the other Steps are the same. These are the following problems with this approach to creating Feature files:
To avoid these problems, let's look at the next section and understand how we can solve them.
Here, we are going to use the Scenario Outline keyword and add one Scenario Outline to test possible login Scenarios:
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 |
Here we have used the Scenario Outline keyword and we have merged all three Scenarios in to one Scenario Outline. One advantage of the Scenario Outline is that our Feature file is now compact and expressive. Let's understand Scenario Outline in more detail:
< > in the Scenario Outline's Steps.Examples table row, where the text between the placeholder's angle brackets matches that of the table column header. When user enters <UserName> in username field in our case, Cucumber looks for a column with the header UserName in the Examples table.UserName in the Examples table, then Cucumber does not give an error but instead considers <UserName> as a String and passes it to Step Definition as it is.UserName, then it picks the first row data from this column and replaces UserName with that value, which is wrongusername in our case, and sends this value to Step Definition.< > for one round of execution.Given user is on Application landing page When user clicks on Sign in button Then user is displayed login screen When user enters "wrongusername" in username field And user enters "123456" in password field And user clicks Sign in button Then user gets login failed error message
Examples table is reached.Examples table, which Lists the rows of values to be substituted for each placeholder.Now that you have leaned the concept of Scenario Outline, try implementing the following:
Change the font size
Change margin width
Change background colour