Scenario Outline

In the previous chapter we parameterized steps of scenario and instead of two step definition method we have created a common function with the matching pattern. This will work only if there is a single set of parameters to be passed and if we need to test with more data, we need to create more scenario. This will be a lengthy process. To solve such issue specflow provide us with Scenario Outline.


The Scenario Outline in Specflow is like a template for scenarios, allowing you to run the same scenario with different sets of data, making your tests more versatile and efficient.


Scenario Outline helps you run the same scenario multiple times with different values. It's great for testing similar situations with various inputs.


It includes an Examples table where you define different sets of data that should be used to fill in placeholders in the scenario.


Placeholder names (denoted with < >) in the scenario are replaced with actual values from the Examples table when the scenario runs.


Scenario Outline is particularly useful when you have a set of steps that remain the same but need to be tested with different inputs or conditions.

Let's understand by modifying example used in the previous chapter. The updated feature file will be as follows.


Gherkin
Copy
Feature: Login functionality
  Scenario Outline: Successful login
    Given the user is on the login page
    When the user enters username <username> and password <password>
    Then the user should be logged in successfully
    Examples:
      | username  | password  |
      | "root"    | "admin"   |
      | "admin"   | "admin"   |
    
  Scenario: Invalid login attempts
    Given the user is on the login page
    When the user enters username "root" and password "pass"
    Then the login should fail with an error message

In the above feature file example code we have updated When statement to include placeholder for username and password parameter and included list of possible successful username and password under example section. Here the first scenario will run for each row in the example table and will replace associated placeholder with the row cell value.


The step definition for the updated feature file remain the same as we don't have to make changes to existing functions. The step definition will look like the following.


C#
Copy
using TechTalk.SpecFlow;
using Xunit;

[Binding]
public class LoginSteps
{

	[Given("the user is on the login page")]
	public void navigateToLoginPage() {
		// Code to navigate to the login page
		Console.WriteLine("User is on the login page");
	}

	[Then("the user should be logged in successfully")]
	public void verifySuccessfulLogin() {
		// Code to verify successful login
		Console.WriteLine("User is logged in successfully");
	}

	[Then("the login should fail with an error message")]
	public void verifyLoginFailure() {
		// Code to verify login failure with an error message
		Console.WriteLine("Login failed with an error message");
	}

	[When("the user enters username {string} and password {string}")]
	public void theUserEntersUsernameRootAndPasswordAdmin(string username, string password)
	{
		Console.WriteLine("The user enters username " + username + " and password " + password + ".");
	}
}

Build your SpecFlow project and run your tests using the xUnit test runner in Visual Studio or from the command line.