How to use table in Specflow

Table in Sepcflow is really helpful when it comes to data-driven automation. Yes, We can pass the data or parameter from the steps of the feature file in table form. The table comes under the SpecFlow Assist Helpers package. 

Use of table in Specflow

The table is used when you need to handle a large amount of the data in a step of test case. It will be more clear in the below examples.

The table is also used to pass data multiple times as a parameter in a test step but it’s not the same as the Scenario outline. There is a difference between the table and the scenario outline.

Table VS Scenario Outline in Specflow

  • The major difference between the table and the ScenarioOutline is in the execution process. Like ScenarioOutline is repeat the whole test case and table repeat the step of a test case.
  • Table can execute single or multiple times for a single step and the execution of the ScenarioOutline is depend on the number of data sets.
  • The table does not need any keyword and Scenario Outline required keyword “Example”.
  • Table will return the complete data(Instance of table) but Scenario Outline will return the single row in each execution.

Example of a table in Specflow

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

Example of a ScenarioOutline in Specflow

Scenario Outline: Login as Admin
Given I Navigate to the login page
And I Enter and 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 |

How to read Table data in Specflow

There are many ways to use table data in the Sepecflow but we are going to discuss two ways that will give a clear idea to get data from the table objects.

Read Table data using TableRow class

By using table TableRow class we can get the data in the form of “list object”. each row/list contains a collection of the values.

Let’s see an example to login in the application using Table

Scenario: Login
When User enter username and password
| uname | password |
| placem | Pass@word1|

Step definition

[When(@"User enter username and password")]
public void WhenUserEnterUsernameAndPassword(Table table)
{
//TO print the value on console
foreach (TableRow row in table.Rows)
{
foreach (String value in row.Values)
Console.WriteLine(value);
}
//Pass the value in parameters
foreach (TableRow row in table.Rows)
{
Page.LoginPage.Login(row[0], row[1]);
}

}

In the above example table will return the collection of rows and each row will contain the collection of values.

Transform table into Data Table

We can convert the table into the Data table that contains data in the form of Key and Value.
In the below example “LoginID” and “Pass” are keys that we are using in the step definition.

Example

When User enter username and password
| Key| Value |
| LoginID | placem |
| Pass | Pass@word1|

Step Definition

[When(@"User enter username and password")]
public void WhenUserEnterUsernameAndPassword(Table table)
{
var dictionary = new Dictionary<string, string>();
foreach (var row in table.Rows)
{
dictionary.Add(row[0], row[1]);
}
Page.LoginPage.Login(dictionary["LoginID"], dictionary["Pass"]);
}