Specflow -Part2(Working with tables)

We may need to work with scenario's where we need to pass a large set of data to test, for example, you are working on Student information management system where you need to create student and enter his/her details into the system.

We can pass the data using tables.
I have added one more feature file to an existing project which has a scenario which takes student details and then prints the details on console

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  |
Then I print details on console



Now we will go ahead an add step definitions for the above steps
namespace SpecflowTest
{
    [Binding]
    public class SpecFlowFeature2Steps
    {
        [Given(@"I have dummy student data as below")]
        public void GivenIHaveDummyStudentDataAsBelow(Table table)
        {
            ScenarioContext.Current.Pending();
        }
        
        [Then(@"I print details on console")]
        public void ThenIPrintDetailsOnConsole()
        {
            ScenarioContext.Current.Pending();
        }
    }
}


Notice specflow automatically detected that we are passing Table data for Given step.
I will implement the steps and then we will talk on the same

Firstly I will create a Student class to hold student details

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 below

namespace SpecflowTest
{
    [Binding]
    public class SpecFlowFeature2Steps
    {
        Student student;
        [Given(@"I have dummy student data as below")]
        public void GivenIHaveDummyStudentDataAsBelow(Table table)
        {
            student = table.CreateInstance<Student>();
        }
 
        [Then(@"I print details on console")]
        public void ThenIPrintDetailsOnConsole()
        {
            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, we created an instance of the student using the CreateInstance method of the specflow.assist package and in then step, we printed the details on console

Run the test and see it pass.
To check the output, under test explorer click on the scenario which we ran and click on output, it will show below

-> Using app.config
Given I have dummy student data as below
  --- table step argument ---
  | StudentId | StudentName | Division    | Age |
  | 100       | James       | Engineering | 30  |
-> done: SpecFlowFeature2Steps.GivenIHaveDummyStudentDataAsBelow(<table>) (0.1s)
Then I print details on console
Student Name:James
Student Age:30
Student Id:100
Student Division:Engineering
-> done: SpecFlowFeature2Steps.ThenIPrintDetailsOnConsole() (0.0s)


Now if the requirement is to print the details of multiple students then we can achieve using below

Modified feature file below

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

Modified 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 we created a set of student details in Given step and we iterated same and print the details in Then step

Run the test and you can see all student details are displayed in the output window.




Comments

Popular posts from this blog

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

Specflow -Part4(Specflow Scenario Outline and Feature Background )

Specflow -Part6(Specflow Hooks)