Enroll in Selenium Training

I hope you have gone through the chapters of CreateSet 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 CreateSet 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 CreateDynamicSet - 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 into 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.

CreateDynamicSet - 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 |
	| testuser_2 | Test@456 |

Note: Multiple rows 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 CreateDynamicSet() method will create the dynamic object which will hold the Table values which are passed as an Argument to the step. This creates a IEnumerable<dynamic> for a several rows. The headers are the property names as before.

IEnumerable <dynamic> credentials = table.CreateDynamicSet();

 IEnumerable <dynamic> credentials = table.CreateDynamicSet();
            foreach(var users in credentials)
            {
                driver.FindElement(By.Id("log")).SendKeys(users.Username);
                driver.FindElement(By.Id("pwd")).SendKeys(users.Password);
                driver.FindElement(By.Id("login")).Click();
            }

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

Note: IEnumerable <dynamic> credentials = table.CreateDynamicSet(); This statement is equivalant to the below code.

var credentials = table.CreateSet<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 |
	| testuser_2 | Test@456 |
	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 System.Collections.Generic;
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)
        {

            IEnumerable <dynamic> credentials = table.CreateDynamicSet();
            foreach(var users in credentials)
            {
                driver.FindElement(By.Id("log")).SendKeys(users.Username);
                driver.FindElement(By.Id("pwd")).SendKeys(users.Password);
                driver.FindElement(By.Id("login")).Click();
                driver.FindElement(By.Id("log")).Clear();
                driver.FindElement(By.Id("pwd")).Clear();
            }

        }

        [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

ScenarioContext Current in SpecFlow
ScenarioContext Current in SpecFlow
Previous Article
CreateDynamicInstance - SpecFlow Assist Dynamic
CreateDynamicInstance - SpecFlow Assist Dynamic
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