In this blog we are going to discuss about what is data driven ,How many types of data driven we can try in cucumber and how the feature ,Page Object Model Class and step definitions will be , with examples we will discuss.
What is Data Driven Testing?
Data Driven Framework
In Data driven Framework storing Data sets and Test scripts
separately . We can use the datas in many ways. Most commercial automated software tools on the market support some sort of Data-Driven Testing. It allows us to automatically run a test case multiple times with different input and validation values.
What is Cucumber data-driven testing?
Data-Driven Testing with Cucumber is a methodology where a sequence of steps in the test script is run repeatedly against different input values fetched from the corresponding data source. It is one of the widely-used automation testing best practices for verifying the behavior and efficiency of tests when handling various types of input values.
Here are the popular external data feed or data sources in data-driven testing:
MS Excel Sheets (.xls, .xlsx)
CSV Files (.csv)
XML Files (.xml)
MS Access Tables (.mdb)
There are different ways to use the data insertion within the Cucumber and outside the Cucumber with external files.
We are going to discuss few types to do DDT in cucumber.
1. Using Parameterization.
2. Using Scenario Outline and Example.
3. Using Data Table
4. Using Excel sheet
1. Using Parameterization
If you are using Parameterization then you can give inputs as hard coded values. This is Parameterization without example type. But in this type we should give single input data.
Example :
Feature File:
In the above feature file we have hard coded the value for username and password.
Page Object Class:
Step Definition :
2. Using Scenario Outline and Example
If you are writing ‘Scenario Outline’, you have to use ‘Examples’ keyword,.
This keyword lets you run the same scenario for two or more different input data. It basically replaces the value assigned in the variable from the input values mentioned in the Examples input data set.
Cucumber inherently supports Data Driven Testing using Scenario Outline.
For better understanding, we can consider an example of the login page. Scenario Outline: Test user Login with different credentials
Example :
Feature File:
In the above feature file we used two values for username and password with examples keyword.
Now the test would run twice with two different set of values. Check the parameterization done in scenario- “When I enter “<username>” ,"<password> and "<confirm_password>" values, instead of hard coding the test data, variables are defined in Examples section and used in Scenario Outline section. Also, note that the step definition file will remain same and will require no change for scenario outline.
Page Object Class:
Step Definition Class:
3. Using Data Table
Data Tables in Cucumber are quite interesting and can be used in many ways. DataTables are also used to handle large amounts of data. They are quite powerful but not the most intuitive as you either need to deal with a list of maps or a map of lists. Most of the people get confused with Data tables & Scenario outline, but these two works completely differently.
we will pass the test data using the data table and handle it using Raw() method.
Maps in Data Tables
Maps in Data Tables can be used if different ways. Headers can also be defined for the data tables. A same step can be executed multiple times with different set of test data using Maps.
There are two types of Data Table Mappings
1. Maps in Data Tables without Header
2. Maps in Data Tables with Header
1. Maps in Data Tables single Test Data without Header
Example :
Feature File:
In the above feature file we used only one test data without any header.
Page Object Class:
Step Definition Class:
2.Maps in Data Tables single Test Data with Header
Example :
Feature File:
In the above feature file we used single test data with headers ( username, password and confirm _password).
Step Definition Class:
Difference between Scenario Outline & Data Table
Scenario Outline | Data Table |
This uses Example keyword to define the test data for the Scenario | No keyword is used to define the test data |
This works for the whole test | This works only for the single step, below which it is defined |
Cucumber automatically run the complete test the number of times equal to the number of data in the Test Set | A separate code needs to understand the test data and then it can be run single or multiple times but again just for the single step, not for the complete test |
4. Using Excel sheet
This an example for Data driven in Cucumber with external files. We can do Data Driven using excel sheet with two types
1. Using Excel SheetName.
2. Using Excel SheetNumber.
1. Using Excel SheetName
While using Excel sheet name we should give the sheet name within the quotes. Because Sheet name is a string.
Example :
Feature File:
Page Object Class:
Step Definition Class:
2. Using Excel SheetNumber.
While using Excel sheet name we should give the sheet name within the quotes. Because Sheet name is a string.
Example :
Feature File:
Page Object Class:
Step Definition Class:
Conclusion
This approach is widely used in Agile projects. Here you don’t have any business requirements to start the development or to start testing your application. The business requirement is in the form of test case. Developer and the tester will access the same document which define the test case.
Go through each of the different examples and understand how it works.
Try to identify the above examples which you think are suitable for your automation project.
Then follow the usual implementation process – add the examples to the feature files, run test runner class to get method definition, add methods to step definition classes and work out the selenium code.
Thanks For reading my blog.
Comments