0

Using scenario outlines in SpecFlow, e.g.

Scenario Outline: Invalid Login Details

Given some pre-conditions...
When user "Larry" enters username <username> and password <password>
Then the message "Invalid Login Details" should be displayed

Examples:
|username|password|
|larry06 |        |
|larry06 |^&*%*^$ |
|%^&&**  |pass123 |
|        |pass123 |

I expected that the "When" step would be evaluated as such:

public void WhenUserEntersUsernameAndPassword(String username, String password){}

And that the scenario would be run 4 times - for each row of the table, passing the values as required. That's not the case.

Instead, SpecFlow creates 1 of 4 required step definitions:

[When(@"""(.*)"" provides the following new username larry(.*) and password ")]
public void WhenUserEntersUsernameLarryAndPassword(string p0, int p1)
{
//TODO
}

And to get the remaining 3 to 'work' I need to manually write methods explicitly matching other values in the table.

I've since realised that I can just say:

When "Larry" enters username "<username>" and password "<password>"

And I get:

[When(@"""(.*)"" provides the following ""(.*)"" and ""(.*)""")]
public void WhenUserEntersUsernameAndPassword(string p0, string name, string pass)
{
//TODO
}

Perfect.

But all documentation seems to suggest I don't need "" and that should just work (e.g. https://github.com/cucumber/cucumber/wiki/Scenario-outlines). I note:

"Your step definitions will never have to match a placeholder. They will need to match the values that will replace the placeholder"

I just don't really see the value of writing separate step definitions for each line of the table.

Is this nuance specific to SpecFlow?

Tom Tom
  • 422
  • 4
  • 14

1 Answers1

2
When "Larry" enters username <username> and password <password>

will match

[When(@"""(.*)"" enters username (.*) and password (.*)")]
public void WhenEntersUsernameAndPassword(string p0, string name, string pass)
{
//TODO
}

so the documentation is fine.

The problem you experienced is that the auto generation of step text doesn't anticipate what regexs to insert without the "..." - which I guess it must be using as an indicator that you are passing interchangable strings - matchable with "(.*)", but if you don't want the quotes, you can still manually correct it just fine.

perfectionist
  • 4,256
  • 1
  • 23
  • 34
  • Aha, that makes sense. I hadn't tried removing the quotation marks once the method name had been generated (nor writing the regex manually!). So it's just a 'glitch' in the automatic method name generation in SpecFlow - easy! – Tom Tom Oct 10 '12 at 14:55
  • For me the critical part was to differentiate "Larry" and Larry, since in Scenarios the name comes with quotes and regex needs to match it, but in Scenario Outline the name is without quotes. – kaskelotti Apr 05 '17 at 08:23