Specflow Scenario Outline not generating expected step code

Viewed 6951

Here is my gherkin:

Scenario Outline: Login using valid email address
Given I have not logged into the current computer
And Username <username> and password <password> is a valid account
When I start the client
And Login using username <username> and password <password>
Then The client should navigate to first time screen

Examples: 
| username         | password    |
| valid001@xyz.com | password001 |
| valid002         | password002 |

This generates the following step file:

[Binding]
public class UserLoginSteps
{
    [Given(@"I have not logged into the current computer")]
    public void GivenIHaveNotLoggedIntoTheCurrentComputer()
    {
        ScenarioContext.Current.Pending();
    }

    [Given(@"Username valid(.*)@xyz\.com and password password(.*) is a valid account")]
    public void GivenUsernameValidXyz_ComAndPasswordPasswordIsAValidAccount(int p0, int p1)
    {
        ScenarioContext.Current.Pending();
    }

    [When(@"I start the client")]
    public void WhenIStartTheClient()
    {
        ScenarioContext.Current.Pending();
    }

    [When(@"Login using username valid(.*)@xyz\.com and password password(.*)")]
    public void WhenLoginUsingUsernameValidXyz_ComAndPasswordPassword(int p0, int p1)
    {
        ScenarioContext.Current.Pending();
    }

    [Then(@"The client should navigate to first time screen")]
    public void ThenTheClientShouldNavigateToFirstTimeScreen()
    {
        ScenarioContext.Current.Pending();
    }
}

The problems are as follows:

  • Generated regex for username is based on the first entry in the examples column. This is not what I want as not all examples follow that pattern.
  • The method names are using the first entry in the examples column (i.e. WhenLoginUsingUsernameValidXyz_ComAndPasswordPassword). This is hard to read and if the data in the examples table changes the method name is no longer correct
  • The type for the parameters are int. They should be string.

I would expect the step definition generation to output something like this:

    [When(@"Login using username (.*) and password (.*)")]
    public void WhenLoginUsingUsernameAndPassword(string p0, string p1)
    {
        ScenarioContext.Current.Pending();
    }

Am I missing something? Is there a way to influence the way SpecFlow generates step methods for Scenario Outlines? If not, what is the best way to fix this without having the feature generated code behind clobbering my changes

2 Answers

You should use single quotes to signify that SpecFlow has to use the whole string instead of the number.

Examples: 
| username           | password      |
| 'valid001@xyz.com' | 'password001' |
| 'valid002'         | 'password002' |
Related