How to do DataDriven testing in specflow api using Selenium?


We can do data driven testing in SpecFlow with/without the Examples keyword. In case we are not using the keyword Examples, then we have to send the data from the steps (enclosed in '') in the Feature file.

Feature File Implementation

Feature: Launching application
Scenario: Launch URL
Given User hits URL 'https://www.tutorialspoint.com/index.htm'

Example

Step Definition File Implementation

using System;
using TechTalk.SpecFlow;
namespace SpecFlowProject1.Features{
   [Binding]
   public class LaunchingApplicationSteps{
      [Given(@"User hits URL '(.*)'")]
      public void GivenUserHitsURL(string url){
         Console.WriteLine(url);
      }
   }
}

Output

Next, we will perform the data driven testing using the Examples keyword. Here, the scenarios are defined within the Scenario Outline keyword. With this approach, we can run the same Scenario with multiple data sets.

The data sets are declared below the Examples section in the form of rows. Also, each data is separated by the | symbol. If there are two rows of data, it means that the same scenario shall execute two times with two different data sets. The steps in the Feature file should contain the header of the Examples table enclosed with <>.

Feature File Implementation

Feature: User credential
Scenario Outline: Login module
Given user types <username> and <password>
Examples:
| username       | password |
| tutorialspoint1|    pwd   |
| tutorialspoint2|    pwd1  |

Example

Step Definition File Implementation

using System;
using TechTalk.SpecFlow;
namespace SpecFlowProject1.Features{
   [Binding]
   public class UserCredentialSteps{
      [Given(@"user types (.*) and (.*)")]
      public void GivenUserTypesUserAndPwds(string username, string password){
         Console.WriteLine(username);
         Console.WriteLine(password);
      }
   }
}

Output

With first data set −

With second data set −

Updated on: 07-Apr-2021

132 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements