Passing parameters to steps in SpecFlow

We can pass any number of parameters to the SpecFlow tests. Consider below feature file.

Feature: Parameters in Feature file

@param
Scenario: Parameter Scenario 1
	Given I am on the web page "https://www.softpost.org"
	Then I verify that title of the page contains "free tutorial"

@param
Scenario: Parameter Scenario 2
	Given I am on the web page "https://www.yahoo.com"
	Then I verify that title of the page contains "yahoo"

In above scenarios, we have used re-used same steps but with different parameters.

Here is the step definition class containing methods for steps in above scenarios. Note that we can pass other data types like int, double in similar way.

using System;
using TechTalk.SpecFlow;

namespace SpecFlowRunnerProject
{
    [Binding]
    public class ParametersInFeatureFileSteps
    {
        [Given(@"I am on the web page ""(.*)""")]
        public void GivenIAmOnTheWebPage(string p0)
        {
            Console.WriteLine("I am on page " + p0);
        }
        
        [Then(@"I verify that title of the page contains ""(.*)""")]
        public void ThenIVerifyThatTitleOfThePageContains(string p0)
        {
            Console.WriteLine("Title of the web page is " + p0);
        }
    }
}

You may also like...