SpecFlow - Table with CreateSet



CreateSet<T> is an extension of the Table method. It transforms the data in the Table to a group of objects. It is one of the popular techniques to have parameterization of data in a horizontalalignment.

We can handle one or many rows of data with this method. The SpecFlow Assist Helpers package is used to work on tables. Also, we have to add namespace TechTalk.SpecFlow.Assist to our code.

The CreateSet<T> method obtains an IEnumerable<T> depending on the matched data in the Table. It has values for all the objects. It makes sure to have the correct type conversions from string to a linked property.

Step 1: Create a Feature File

The details of how to create a Feature File is discussed in detail in the Chapter − Feature File.

Feature: User credential

Scenario: Login module
   When User types details
   | Name | Password |
   | t1   | pwd      |
   | t2   | pwd1     |
Then user should be able to login

Step 2: Create C# File to access String Objects

We shall create a new folder within the project and have a C# file in it. Right-click on the SpecFlow Project, then click on Add.

Select the option New Folder.

String Object

Give the name Utils to the folder.

Right-click on the new Folder created, then select the option Add. Click on Class.

Folder created

Type C# Class in the search box and search. Select the option Class from the search result and then click on Add to proceed.

Folders Created

Project Folder Structure

Folders Structures

C# Class Implementation

using System;
using System.Collections.Generic;
using System.Text;
namespace SpecFlowProject1.Utils {
   class Class1 {
      public class Input {
         //two string objects declared
         public string Input1 { get; set; }
         public string Input2 { get; set; }
      }
   }
}

Step 3: Create a Step Definition File

The details of how to create a Step Definition File is discussed in detail in the Chapter − Step Definition File.

using System;
using TechTalk.SpecFlow;
using TechTalk.SpecFlow.Assist;
namespace SpecFlowProject1.Features {
   [Binding]
   public class UserCredentialSteps {
      [When(@"User types details")]
      public void WhenUserTypesDetails(Table t) {
         
         //access Table data with CreateSet method
         var i = t.CreateSet<Utils.Class1.Input>();
         
         //iterate over rows
         foreach (var r in i) {
            Console.WriteLine(r.Input1);
            Console.WriteLine(r.Input2);
         }
      }
      [Then(@"user should be able to login")]
      public void ThenUserShouldBeAbleToLogin() {
         Console.WriteLine("User should be able to login");
      }
   }
}

Step 4: Execution & Results

Select User credential(1) Feature, then click on Run All Tests in View.

Test Views

Select Login Module Scenario, then click on Open additional output for this result link.

Additional Outputs

Scenario link

The scenario got executed with data passed from a Table in the feature file within the When step using CreateSet method.

Advertisements