Embedding of Scenario Outline with Data Table in Cucumber
In one of our automation projects using Cucumber and Capybara, there was a use case where we needed to automate the process of submitting a multi-field form and checking the provided form details on the next page. We considered parameterization using Data Table and Scenario Outlineprovided by Cucumber. In this article, we'll take a look at why and how we combined Scenario Outline with Data Table to optimize the feature file.
We began with Scenario Outline because we only had four fields: First Name, Last Name, Password, Email, and we wanted to run the same scenario with different datasets. However, we were later given a new requirement that included additional fields such as Location, Age, and Phone Number, increasing the number of input parameters. As a result, the scenario and step definitions became more complex. This is how we can write a test case, which can be easily extended for new field requirements. (For reference we have included only the first four fields in the examples)
We considered using Data Table because we needed to send multiple parameters for a scenario. In contrast to Scenario Outline, Data Table passes all datasets in a single step, and to retrieve dataset details we had to loop through the data map. As an alternative to iterating through the data map, we could repeat the scenario as many times as the dataset.
To avoid the repetition of the Scenarios we passed the Reference Header of the Example Table as a parameter to the Data Table.
In the step definition, we retrieved the input parameter provided as the first row of the Data Map. We used the same hash in the subsequent steps by assigning it to an instance variable.
In the above example, the scenario will run three times as we have three rows in the Example Table.
By combining the scenario outline with the data table, we were able to DRY our code and make it more readable at the same time.