Data Driven Automation in Specflow.

What is data driven automation in Specflow and how to pass dynamic data in Specflow and parameterization in Specflow.

Data-driven automation is a process to execute the same test case with multiple inputs. Specflow provides the following ways to perform data-driven testing in Specflow.

  • Using Parameterization.
  • Using Example or ScenarioOutlines.
  • Using table.
  • Using External files like Excel.

Parameterization in Specflow

Parameter is used to pass data to step definition as a parameter in Specflow. The parameter should be defined in the feature file within “ ”. Let’s have some examples to set the parameter values.

Example:

When: I log in with “UserName”.

We can use multiple parameters in a step just need to the user “ ”.

Example:

When: I login with “UserName” and “Password”.

Get the parameter value in step definition.

When you create a step definition for this step. By default, it will create two variable of string type p0 and p1 in step definition that holds the value.

[When(@"User enters ""(.*)"" and ""(.*)""")]
public void WhenUserEntersAnd(string UserName, string Password)
{
Console.WriteLine("UserName : " + UserName + "Password : "+ Password);
}

ScenarioOutline:

  • ScenarioOutline is another way to perform data driven automation in Specflow.
  • ScnearioOutline is used to execute the same scenario multiple times with different data in each execution.

Example:

Scenario Outline: Login as Admin
Given I Navigate to the login page
And I Enter <username> and <password>
When I click to login button
Then I can see admin is able to login
Examples:
| Username | Password |
| Asd@gmail.com | Pass@123 |
| Admin@gmail.com | Pass@123 |
| User@gmail.com | Pass@123 |
| Div@gmail.com | Pass@123 |

Read in detail about the scenario outline.

Table in Specflow

The table is used to execute the same step multiple times with different data in each execution.

Scenario: Login
Given User is navigate to login page
When User enter username and password
| uname | password |
| Bhupi123 | Pass@word1|
Then User is able to login in the application


Read in detail about Table.

Data Driven automation Using External file in Specflow

In order to read data from excel in Specflow, we have to use C#. below is the example of C# program to read data from excel file.

publicstatic UserData GetTestData(string keyName)
{
using (var connection = new OleDbConnection(TestDataFileConnection()))
{
connection.Open();
var query = string.Format("select * from [DataSet$] where key='{0}'", keyName);
var value = connection.Query<UserData>(query).FirstOrDefault();
connection.Close();
return value;
}
}

How to read data from excel in selenium

In order to read data from excel in Specflow+ we can read data in feature file only under the scenario outlines.
Yes, Specflow+ will provide @source tag to read an excel file. Let’s have an example.

Scenario Outline: Login
Given User is navigated to login page
When User enters <UserName> login
Then User is able to login in the application
@source: D:\Login.xlsx
Examples:
| UserName |