Enroll in Selenium Training

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 come under the package of SpecFlow Assist Helpers. To use these helpers, we need to add the TechTalk.SpecFlow.Assist namespace to the top of your file.

using TechTalk.SpecFlow.Assist;

Most of the people get confused with the 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 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

As I said above, the Tables can be used in many ways because it has provided many different methods to use. Let’s just go through a 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 Framework in the next series of this SpecFlow Tutorial.

In this series we will cover the following:

  • Transform Table into Dictionary using Table.Rows
  • Transform Table into DataTable using Table.Header
  • Tables - CreateInstance
  • Tables - CreateSet
  • Tables - ToProjection

Tables in Specflow - Transform Table into 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-Value pair, 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:

	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 Testing and 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.

	[When(@"User enter credentials")]
        public void WhenUserEnterCredentials(Table table)
        {

        }

3) Transform table into DataTable

To convert Table into 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 TableExtensions in the same folder and create a new static method called ToDictionary(), which will accept Table data.

        public static Dictionary<string, string> ToDictionary(Table table)
        {
            var dictionary = new Dictionary<string, string>();
            foreach (var row in table.Rows)
            {
                dictionary.Add(row[0], row[1]);
            }
            return dictionary;
        }

4) Complete Step Definition Method

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

        [When(@"User enter credentials")]
        public void WhenUserEnterCredentials(Table table)
        {
            var dictionary = TableExtensions.ToDictionary(table);
            var test = dictionary["Username"];

            driver.FindElement(By.Id("log")).SendKeys(dictionary["Username"]);
            driver.FindElement(By.Id("pwd")).SendKeys(dictionary["Password"]);
        }

The complete code will look like this:

LogIn_Feature File

Feature: LogIn_Feature
	In order to access my account
    As a user of the website
   I want to log into the website

@mytag
Scenario: Successful Login with Valid Credentials
	Given User is at the Home Page
	And Navigate to LogIn Page
	When User enter credentials
	| Key      | Value      |
	| Username | testuser_1 |
	| Password | Test@123   |
	And Click on the LogIn button
	Then Successful LogIN message should display

Scenario: Successful LogOut
	When User LogOut from the Application
	Then Successful LogOut message should display

TableExtension Class File

using System.Collections.Generic;

using TechTalk.SpecFlow;

namespace SpecFlowDemo.Utils
{
    public class TableExtensions
    {
        public static Dictionary<string, string> ToDictionary(Table table)
        {
            var dictionary = new Dictionary<string, string>();
            foreach (var row in table.Rows)
            {
                dictionary.Add(row[0], row[1]);
            }
            return dictionary;
        }

    }
}

LogIn_Steps File

using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using TechTalk.SpecFlow;
using TechTalk.SpecFlow.Assist;
using SpecFlowDemo.Utils;

namespace SpecFlowDemo.Steps
{
    [Binding]
    public class LogIn_Steps
    {
        public IWebDriver driver;
        [Given(@"User is at the Home Page")]
        public void GivenUserIsAtTheHomePage()
        {
            driver = new FirefoxDriver();
            driver.Url = "https://www.store.demoqa.com";
        }

        [Given(@"Navigate to LogIn Page")]
        public void GivenNavigateToLogInPage()
        {
            driver.FindElement(By.XPath(".//*[@id='account']/a")).Click();
        }

        [When(@"User enter credentials")]
        public void WhenUserEnterCredentials(Table table)
        {
            var dictionary = TableExtensions.ToDictionary(table);
            var test = dictionary["Username"];

            driver.FindElement(By.Id("log")).SendKeys(dictionary["Username"]);
            driver.FindElement(By.Id("pwd")).SendKeys(dictionary["Password"]);
        }

        [When(@"Click on the LogIn button")]
        public void WhenClickOnTheLogInButton()
        {
            driver.FindElement(By.Id("login")).Click();
        }

        [When(@"User LogOut from the Application")]
        public void WhenUserLogOutFromTheApplication()
        {
            ScenarioContext.Current.Pending();
        }

        [Then(@"Successful LogIN message should display")]
        public void ThenSuccessfulLogINMessageShouldDisplay()
        {
            //This Checks that if the LogOut button is displayed
            true.Equals(driver.FindElement(By.XPath(".//*[@id='account_logout']/a")).Displayed);
        }

        [Then(@"Successful LogOut message should display")]
        public void ThenSuccessfulLogOutMessageShouldDisplay()
        {
            ScenarioContext.Current.Pending();
        }
    }
}

Project Solution Explorer

TableRows_1

Data Driven Testing Using Examples Keyword In SpecFlow
Data Driven Testing Using Examples Keyword In SpecFlow
Previous Article
Transform Table into DataTable
Transform Table into DataTable
Next Article
Lakshay Sharma
I’M LAKSHAY SHARMA AND I’M A FULL-STACK TEST AUTOMATION ENGINEER. Have passed 16 years playing with automation in mammoth projects like O2 (UK), Sprint (US), TD Bank (CA), Canadian Tire (CA), NHS (UK) & ASOS(UK). Currently, I am working with RABO Bank as a Chapter Lead QA. I am passionate about designing Automation Frameworks that follow OOPS concepts and Design patterns.
Reviewers
Virender Singh's Photo
Virender Singh

Similar Articles

Feedback