Specflow use parameters in a table with a Scenario Context

左心房为你撑大大i 提交于 2019-12-21 03:57:23

问题


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?


回答1:


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.




回答2:


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; }
}


来源:https://stackoverflow.com/questions/20370854/specflow-use-parameters-in-a-table-with-a-scenario-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!