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 set 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.

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.

3) 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 look for the ExamplesKeyword in the test to read the Test Data.

The complete code will look like this:

1

2

3

4

5

6

7

8

9

10

Scenario Outline:Successful Login with Valid Credentials

Given User isat the Home Page

AndNavigate toLogIn Page

When User enter<username>and<password>

AndClick on the LogIn button

ThenSuccessful LogIN message should display

Examples:

|username|password|

|testuser_1|Test@123|

|testuser_2|Test@153|

4) There is a little change in the syntax of 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.

5) 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 Scenario Outline section.