In the last chapter of Parameterization in SpecFlow, we learned how to parameterize data. But with that trick, only limited functionality can be achieved of Data Driven. As the test can be run multiple times. But by now that you know the anatomy of a Data-Driven test, here’s a trick that simplifies the process of Data Driven testing using SpecFlow. SpecFlow inherently supports Data Driven testing by the use of the Scenario Outline and Examples section. It is with these keywords that SpecFlow allows for easy Data Driven testing to be completed where no changes need to be made to the Java file. In this tutorial we learn, How to Implement Scenario Outline in Data Driven testing using Examples Keyword?

Example keyword can only be used with the Scenario Outline Keyword.

  • Scenario Outline - This is used to run the same scenario for 2 or more different sets of test data. E.g. In our scenario, if you want to register another user you can data drive the same scenario twice.
  • Examples – All scenario outlines have to be followed with the Examples section. This contains the data that has to be passed on to the scenario.

Data Driven Testing Using Examples Keyword

If you understood the concept of Parameterization in SpecFlow, you would find this one very easy. In this tutorial as well I am taking the same LogIn test scenario.

  1. Change the Scenario keyword to Scenario Outline in the Feature file.

  2. Enter the Example Data just below the LogIn Scenario of the Feature File.

Examples:

| username | password |

| testuser_1 | Test@123 |

| testuser_2 | Test@153 |

Note: The table must have a header row corresponding to the variables in the Scenario Outline steps.

The Examples section is a table where each argument variable represents a column in the table, separated by “|”. Each line below the header represents an individual run of the test case with the respective data. As a result, if there are 3 lines below the header in the Examples table, the script will run 3 times with its respective data.

  1. Need to update the Statement in the feature file, which tells SpecFlow to enter Username & Password.

And User enter <username> and <password>

SpecFlow understands the above statement syntax and looks for the Examples Keyword in the test to read the Test Data.

The complete code will look like this:

Scenario Outline: Successful Login with Valid Credentials
	Given User is at the Home Page
	And Navigate to LogIn Page
	When User enter <username> and <password>
	And Click on the LogIn button
	Then Successful LogIN message should display
Examples:
| username   | password |
| testuser_1 | Test@123 |
| testuser_2 | Test@153 |

  1. There is a little change in the syntax of the Definition file for the corresponding method.

Change the below line:

[When(@"User enter '(.)' and '(.)'")]

To

[When(@"User enter (.) and (.)")]

Note: it looks similar, but if you notice carefully there is a little change in the regular expression.

Data_Driven_7

The updated Feature file and Step Definition file for the Test:

LogIn_Feature.feature

Feature: LogIn_Feature
	In order to access my account
    As a user of the website
   I want to log into the website

@mytag
Scenario Outline: Successful Login with Valid Credentials
	Given User is at the Home Page
	And Navigate to LogIn Page
	When User enter <username> and <password>
	And Click on the LogIn button
	Then Successful LogIN message should display
Examples:
| username   | password |
| testuser_1 | Test@123 |
| testuser_2 | Test@153 |

Scenario: Successful LogOut
	When User LogOut from the Application
	Then Successful LogOut message should display

LogIn_Steps.cs

Scenario Outline: Successful Login with Valid Credentials
	Given User is at the Home Page
	And Navigate to LogIn Page
	When User enter <username> and <password>
	And Click on the LogIn button
	Then Successful LogIN message should display
Examples:
| username   | password |
| testuser_1 | Test@123 |
| testuser_2 | Test@153 |using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using TechTalk.SpecFlow;

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 (.*) and (.*)")]
        public void WhenUserEnterAnd(string username, string password)
        {
            driver.FindElement(By.Id("log")).SendKeys(username);
            driver.FindElement(By.Id("pwd")).SendKeys(password);
        }

        [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();
        }
    }
}
  1. Now to run a Feature Test, Right-click on the test in the Test Explorer window and select Run Selected Tests. This will run the selected test and display the output in the console window.

Note: Feature File can also be run by Right-clicking in the feature and choosing Run SpecFlow Scenarios. But sometimes it creates issue.

This takes the parameterization one step further: now our scenario has “variables” and they get filled in by the values in each row. To be clear: by defining this, the scenario will run two times, passing in one row at a time. This makes it very easy to define a lot of examples, edge cases, and special outcomes. Instead of hardcoding the test data, variables are defined in the Examples section and used in the Scenario Outline section.

Note: Please create your own username & password for the test, if you supply wrong UserName & Password 3 times, your IP will get blocked.

Data Driven Testing in SpecFlow
Data Driven Testing in SpecFlow
Previous Article
Tables in Specflow
Tables in Specflow
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