Enroll in Selenium Training

In the last two chapters of Tables (Transform Tables into Dictionary & Transform Table into DataTable we observed that data can be passed to the Steps as argument in the form of Table. Data can also be used as Vertical & Horizontal according to the scenario need.

But Specflow Table also provides extensive methods to make the best use of it. CreateInstance in SpecFlow Table is also one of the best ways of Parameterizing Data in Vertical format.

CreateInstance<T> is an extension method off of Table that will convert the table data to an object.

CreateInstance in SpecFlow Table

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.

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. Just provide the Fields and their Values.

When User enter credentials
	| Field    | Value      |
	| Username | testuser_1 |
	| Password | Test@123   |

Note: Only Single row of Data can be used with this.

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) Create Plain C# class object

<T> denotes Type, which means any type of object can be used. It can be a string, int or Class Object. In our case, it is required to create a plain C# class with having two string object (Username & Password).

Create a new folder in the solution and name it as Utils. Then create a new C# class with the name Credentials in the same folder and create.

   public class Credentials
    {
        public string Username { get; set; }
        public string Password { get; set; }
    }

4) Complete Step Definition Method

The CreateInstance<T> method will create the Credentials and set properties according to what can be read from the table. It also will use the appropriate casting or conversion to turn your string into the appropriate type.

The headers in this table can be "Field" and "Value", or anything that you want. What matters is that the first column has the property name and the second column has the value.

Credentials credentials = table.CreateInstance<Credentials>();

Or

var credentials = table.CreateInstance<Credentials>();

  [When(@"User enter credentials")]
        public void WhenUserEnterCredentials(Table table)
        {
            var credentials = table.CreateInstance<Credentials>();      
            driver.FindElement(By.Id("log")).SendKeys(credentials.Username);
            driver.FindElement(By.Id("pwd")).SendKeys(credentials.Password);
            driver.FindElement(By.Id("login")).Click();
        }

Note: With this, multiple Data sets can not be used for the same step.

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
	| Field    | 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

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 credentials = table.CreateInstance<Credentials>();      
            driver.FindElement(By.Id("log")).SendKeys(credentials.Username);
            driver.FindElement(By.Id("pwd")).SendKeys(credentials.Password);
            driver.FindElement(By.Id("login")).Click();
        }

        [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();
        }
    }
}

Credentials Class File

namespace SpecFlowDemo.Utils
{
    public class Credentials
    {
        public string Username { get; set; }
        public string Password { get; set; }
    }
}

Project Solution Explorer

CreateInstance in Specflow Table

Transform Table into DataTable
Transform Table into DataTable
Previous Article
CreateSet in SpecFlow Table
CreateSet in SpecFlow Table
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