Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specflow use parameters in a table with a Scenario Context

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?

like image 468
Wasafa1 Avatar asked Dec 04 '13 08:12

Wasafa1


Video Answer


2 Answers

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.

like image 139
Ben Smith Avatar answered Sep 23 '22 17:09

Ben Smith


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; }
}
like image 25
Brett Veenstra Avatar answered Sep 23 '22 17:09

Brett Veenstra