I hope you have gone through the chapters of CreateInstance in SpecFlow Table  where we used Credentials POCO object to store Username & Password. In case of many Steps in Feature file with different Test Data sets, we are bound to create equivalant number of POCO (Plain Old C Object) objects and we will end up with many different classes in our framework like we created Credentials class in  CreateInstance in SpecFlow Table.

The other and easy way is to create Plain C# class's object Dynamically at the runtime. To achieve that we can use CreateDynamicInstance - SpecFlow Assist Dynamic .

SpecFlow Assist Dynamics

This is a little tool to help write less code in the step definitions, and focus on the actual step instead of infrastructure. Go through this post as it explains about what SpecFlow.Assist.Dynamic is?  In this post I’ll show you how some ways I use the dynamic features to and some tricks that you might not know about. First let's see how to get this library in to the project.

Installation

  1. Go to Tools >> NuGet Package Manager >> Package Manager Console.

CreateDynamicInstance_1

  1. This will open up a window at the Center Bottom of the Visual Studio. Now paste this line: Install-Package SpecFlow.Assist.Dynamic

CreateDynamicInstance_2

  1. Hit Enter and Console will display the Successful Message for the Installation. You might receive the Pop Up message to ReGenerate the feature file because of changes. Just press Yes.

CreateDynamicInstance_3

  1. To verify the Successful Installation, open your Project Solution Explorer >> References and look for SpecFlow.Assist.Dynamic.

CreateDynamicInstance_4

Note: The same can also be install from the Tools >> NuGet Package Manager >> Manage NuGet packages for Solution and search for SpecFlow.Assist.Dynamic and Install it.

CreateDynamicInstance - SpecFlow Assist Dynamic

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 Horizontal format. Just provide the Headers their Values.

When User enter credentials
	| Username   | Password |
	| testuser_1 | 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) Complete Step Definition Method

The CreateDynamicInstance() method will create the dynamic object which will hold the Table values which are passed as an Argument to the step. It also will use the appropriate casting or conversion to turn your string into the appropriate type.

dynamic credentials = table.CreateDynamicInstance();

 [When(@"User enter credentials")]
        public void WhenUserEnterCredentials(Table table)
        {
            dynamic credentials = table.CreateDynamicInstance();
            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.

Note: dynamic credentials = table.CreateDynamicInstance(); This statement is equilvaant to the below code.

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

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

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
	| Username   | Password |
	| testuser_1 | 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 TechTalk.SpecFlow;
using TechTalk.SpecFlow.Assist;

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)
        {
            dynamic credentials = table.CreateDynamicInstance();
            driver.FindElement(By.Id("log")).SendKeys(credentials.Username);
            driver.FindElement(By.Id("pwd")).SendKeys(credentials.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

CreateDynamicInstance_5

CreateDynamicSet - SpecFlow Assist Dynamic
CreateDynamicSet - SpecFlow Assist Dynamic
Previous 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