SpecFlow Assist Helpers

Tables in Specflow are quite interesting and can be used in many ways. Tables are also used to handle large amount of data. They are quite powerful but not the most intuitive as you either needs to deal with dictionary objects, plain class objects and collections.

Tables comes under the package of SpecFlow Assist Helpers.To use these helpers, we need to add the TechTalk.SpecFlow.Assistnamespace to the top of your file.

using TechTalk.SpecFlow.Assist;

Most of the people get confused with Tables & Scenario outline, but these two works completely differently.

Difference between Scenario Outline & Table

Scenario Outline:

This uses Example keyword to define the test data for the Scenario

This works for the whole test

Cucumber automatically run the complete test the number of times equal to the number of data in the Test Set

Tables:

No keyword is used to define the test data

This works only for the single step, below which it is defined

A separate code is need 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

As I said above, that the Tables can be used in many ways because it has provided many different methods to use. Let’s just go through few most popular methods. I will choose a simple scenario to illustrate the working of the Table but we will make effective use of this when we will do Specflow Frameworkin the next series of this SpecFlow Tutorial.

In this series we will cover the following:

Transform Table in to Dictionary using Table.Rows

Transform Table in to DataTable using Table.Header

Tables – CreateInstance

Tables – CreateSet

Tables – ToProjection

Tables in Specflow – Transform Table in to Dictionary

We will be using the same example of LogIn Feature and modify it according to our usage. Before moving on to this chapter, please have a look at the base chapter of Data Driven Testing and see how the simple functionality works for LogIn Scenario.

Tables can be used Vertically and Horizontally. In the very first examples of tables, we will use the Data Vertically like a Key – Valuepair, which can be possible with the help of Dictionary object.

1) Create a New Step

The first step is to create a new Step which will take Data in Table format. It is again an easy job to specify data for the step in Vertical format. Let’s see how to pass Vertical Data in the same User LogIn scenario, which is used in previous tutorials of SpecFlow:

1

2

3

4

When User enter credentials

|Key|Value|

|Username|testuser_1|

|Password|Test@123|

The only difference is in this, we are not passing parameters in the step line the way we used in Data Driven Testingand not even we are using Examples test data the way it was used in Data Driven Testing with Example Scenarios . We have declared the data under the step only. That is how Tables are used in Specflow to pass Tables as arguments to Steps.

2) Create a New Step Definition

The second step is to create a new Steps Definition for newly created step, which can be done if bring the cursor on the created step and press F12. The Specflow will display the pop up with Skeleton body of the step, which can be copied and used accordingly.

1

2

3

4

5

[When(@"User enter credentials")]

publicvoidWhenUserEnterCredentials(Table table)

{

}

3) Transform table in to DataTable

To convert Tablein to Dictionary, it is required to use System.Collections.Generic (using System.Collections.Generic).

Create a new folder in the solution and name it as Utils. Then create a new C# class with the name TableExtensionsin the same folder and create a new static method called ToDictionary(), which will accept Table data.

1

2

3

4

5

6

7

8

9

publicstaticDictionary<string,string>ToDictionary(Table table)

{

vardictionary=newDictionary<string,string>();

foreach(varrow intable.Rows)

{

dictionary.Add(row[0],row[1]);

}

returndictionary;

}

4) Complete Step Definition Method

Now make a use of ToDataTable function and convert the Table data into DataTable.