11

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?

Ben Smith
  • 19,589
  • 6
  • 65
  • 93
Wasafa1
  • 785
  • 2
  • 10
  • 21
  • possible duplicate of [SpecFlow/Cucumber/Gherkin - Using tables in a scenario outline](http://stackoverflow.com/questions/5118860/specflow-cucumber-gherkin-using-tables-in-a-scenario-outline) – Adam Houldsworth Dec 04 '13 at 09:00
  • 1
    Thanks Adam, The question is indeed similar but not resolved. I thought perhaps by simlifying the code, the question will be more clear and hopefully someone will have a good advice. – Wasafa1 Dec 04 '13 at 09:10
  • "without modifying to the equivilent in the Examples section" - What do you mean by this? – Ben Smith Dec 04 '13 at 11:16
  • @Fresh - Edited question to answer your comment. Thanks. – Wasafa1 Dec 04 '13 at 11:44

2 Answers2

16

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.

Ben Smith
  • 19,589
  • 6
  • 65
  • 93
  • This sounds like a nice workaround, the problem is that I already have lots of tests writen with the "I visit url" method, and I don't want to reeducate my users. Are you sure this is imposible? – Wasafa1 Dec 04 '13 at 12:57
  • @Wasafa1, I know I am commenting after long gap, but your I visit URL is a generic step and you need additional step it build the url as it has parameters. Also your table header cannot contain a parameter names. I am sure you can refactor or add a step which will reuse your already written code. – Hemant Dec 19 '18 at 07:40
5

This is now possible (at least I'm doing it with SpecFlow v2.0)

[When(@"When I visit url")]
public void WhenIVisitUrl(Table table)
{
  var url = table.CreateInstance<UrlDTO>();
}

public class UrlDTO{
  public string base { get;set; }
  public string page { get;set; }
  public string parameter1 { get;set; }
  public string parameter2 { get;set; }
}
Brett Veenstra
  • 47,674
  • 18
  • 70
  • 86