Specflow use parameters in a table with a Scenario Context

Wasafa1 picture Wasafa1 · Dec 4, 2013 · Viewed 18.9k times · Source

I am using Specflow in C# to build automatic client side browser testing with Selenium.

The goal of these tests is to simulate the business scenario where a client enters our website in specific pages, and then he is directed to the right page.

I Want to use parameters inside a Scenario Context, for example:

When I visit url
 | base                         | page      | parameter1       | parameter2     |
 | http://www.stackoverflow.com | questions | <questionNumber> | <questionName> |
Then browser contains test <questionNumber>

Examples: 
    | <questionNumber> | <questionName> |
    | 123              | specflow-q1    |
    | 456              | specflow-q2    |
    | 789              | specflow-q3    |

Note: step "When I visit url" takes base+page+parameter1+parameter2, creates url "base/page/parameter1/parameter2" and goes to this URL.

The problem is that the input table in step "I visit url", is passing the text as-is, without modifying to the equivilent in the Examples section.

It means that the table that the above syntax builds has a row with data the parameter names:

http://www.stackoverflow.com, questions, questionNumber, questionName

Instead of using their value:

http://www.stackoverflow.com, questions, 123 ,specflow-q1

Do you know how can I use it correctly?

Answer

Ben Smith picture Ben Smith · Dec 4, 2013

It is not possible to mix data tables and scenario outlines. Instead I'd rewrite your scenario as follows:

When I visit the URL <base>/<page>/<questionNumber>/<questionName>
Then the browser contains test <questionNumber>

Examples: 
 | base                         | page      | questionNumber | questionName |
 | http://www.stackoverflow.com | questions | 123            | specflow-q1  |
 | http://www.stackoverflow.com | questions | 456            | specflow-q2  |
 | http://www.stackoverflow.com | questions | 789            | specflow-q3  |

Inside the "When I visit the URL" step definition you'd construct the URL from the passed-in table parameter (which is what you are doing currently).

Whilst "base" and "question" values are repeated in the "Examples" section, it is clear to see what exactly is being tested. A non-technical user (e.g. business user) will also be able to easily understand what this test is trying to achieve too.