Specflow -Part3(Working with tables using Specflow.Assist.Dynamic)

In the previous lecture, we learned how can we work with tables to handle test data from the feature file.I will paste the feature and step definitions from earlier lecture and then we will talk about the shortcomings and how Specflow.Assist.Dynamic can help us to overcome them

Feature File

Feature: SpecFlowFeature2
 In order to show specflow capabilities for working on tables
 We are going to write some scenarios here


Scenario: Verify specflow can read data from table and print on console

Given I have dummy student data as below 
| StudentId | StudentName | Division    | Age |
| 100       | James       | Engineering | 30  |
| 101       | Jon         | Medical     | 40  |
| 102       | Steve       | Engineering | 50  |
Then I print details on console

Custom Class

public class Student
   {
       public int StudentId;
       public string StudentName;
       public string Division;
       public int Age;
 
       public Student(int StudentId,string StudentName,string Division,int Age)
       {
           this.StudentId = StudentId;
           this.StudentName = StudentName;
           this.Division = Division;
           this.Age = Age;
 
       }
   }

Step definitions

namespace SpecflowTest
{
    [Binding]
    public class SpecFlowFeature2Steps
    {
        IEnumerable<Student> studentDetails;
        [Given(@"I have dummy student data as below")]
        public void GivenIHaveDummyStudentDataAsBelow(Table table)
        {
            studentDetails = table.CreateSet<Student>();
 
        }
 
        [Then(@"I print details on console")]
        public void ThenIPrintDetailsOnConsole()
        {
            foreach (var student in studentDetails)
            {
                Console.WriteLine("Student Name:" + student.StudentName);
                Console.WriteLine("Student Age:" + student.Age);
                Console.WriteLine("Student Id:" + student.StudentId);
                Console.WriteLine("Student Division:" + student.Division);
            }
        }
    }
}

Here things to notice, when we need to pass data from the feature file, we created a custom class and then created Set which we iterated in Then step.

So as of now, we created a custom class for the student. We may need to create more custom classes for the Department, Libray etc

As we are only using custom classes to read data from feature file, creating multiple such classes Sounds like an overhead?

What if there was a way where Specflow creates those classes dynamically depending upon the table we pass, we can save a lot of code.

Yes, it's possible. CreateDynamicSet or CreateDynamicInstace methods of Specflow.Assist.Dynamic package allows us to do so.

Now let's refactor our original code using Specflow.Assist.Dynamic package

Installation

  1. Right-click the project and select 'Manage Nuget packages'
  2. Search for specflow.assist.dynamic and install
Below is the refactored step definitions

namespace SpecflowTest
{
    [Binding]
    public class SpecFlowFeature2Steps
    {
        //IEnumerable<Student> studentDetails;
        IEnumerable<dynamic> studentDetails;
        [Given(@"I have dummy student data as below")]
        public void GivenIHaveDummyStudentDataAsBelow(Table table)
        {
            // studentDetails = table.CreateSet<Student>();
            studentDetails=table.CreateDynamicSet();
 
 
        }
 
        [Then(@"I print details on console")]
        public void ThenIPrintDetailsOnConsole()
        {
            foreach (var student in studentDetails)
            {
                Console.WriteLine("Student Name:" + student.StudentName);
                Console.WriteLine("Student Age:" + student.Age);
                Console.WriteLine("Student Id:" + student.StudentId);
                Console.WriteLine("Student Division:" + student.Division);
            }
        }
    } 
} 

Notice, the old lines are commented

We started with creating an IEnumerable list which holds dynamic values as we want to get rid of custom student class and in Given step we created a dynamic set using CreateDynamicSet method.

Now we can get rid of student class and our code works as expected and yes we saved some lines of code

It feels good when u save some lines of code 😇



Comments

Popular posts from this blog

Specflow -Part4(Specflow Scenario Outline and Feature Background )

C# Basics-Part 2(Namespaces,Properties,Constants and Read-Only Fields)