具有不同 URL 的 Specflow 场景大纲
Specflow Scenario Outline with different URLs
是否可以有一个场景大纲,其中每个示例在不同的 URL 中运行?例如,如果示例中的第一列 table 是某种两个 digit/character 代码,则根据该值打开不同的起始 URL?
给定以下情景大纲:
Scenario Outline: Each page has an help option
Given I am on the '<PageName>' page that has id '<Id>'
Then I expect an help option
Examples:
| PageName | Id |
| Home | 0 |
| Products | 5 |
| FAQ | 42 |
那么你可以这样一步:
[Given(@"I am on the '(.*)' page that has id '(.*)'")]
public void GivenIAmOnThePageThatHasId(string friendlyName, string id)
{
// pageids are shaped like 'http://www.example.com/pageid?4'
var myUrl = @"http://www.example.com/pageid?" + id;
Console.WriteLine("Navigating to the {0} page following url {1}", friendlyName, myUrl);
NavigateToUrl(myUrl);
}
当然可以。为什么不只设置 URL 的步骤,将 URL 分配给某个变量(在 class、ScenarioContext.Current
或自定义中上下文对象),然后在所有调用中使用此 URL。在我的 phone 中,格式化很痛苦,但像这样:
Given I'm using the site '<site>'
When I login
Then something should happen
Examples:
|site |
| aaa.com|
| bbb.com|
然后在给定的步骤中,只需记录 URL 并使用该基础 URL 在 when 步骤中构建完整的 URL。
您的步骤 class 可能看起来像这样。
[Binding]
Public class Steps
{
Private string baseUrl;
[Given ("I'm using the site '(.*)'")]
Public void GivenImUsingTheSite(string baseUrl)
{
This.baseUrl=baseUrl;
}
[When ("I log in")
Public void WhenILogIn()
{
String URL=baseUrl + "\login";
....login
}
}
是否可以有一个场景大纲,其中每个示例在不同的 URL 中运行?例如,如果示例中的第一列 table 是某种两个 digit/character 代码,则根据该值打开不同的起始 URL?
给定以下情景大纲:
Scenario Outline: Each page has an help option
Given I am on the '<PageName>' page that has id '<Id>'
Then I expect an help option
Examples:
| PageName | Id |
| Home | 0 |
| Products | 5 |
| FAQ | 42 |
那么你可以这样一步:
[Given(@"I am on the '(.*)' page that has id '(.*)'")]
public void GivenIAmOnThePageThatHasId(string friendlyName, string id)
{
// pageids are shaped like 'http://www.example.com/pageid?4'
var myUrl = @"http://www.example.com/pageid?" + id;
Console.WriteLine("Navigating to the {0} page following url {1}", friendlyName, myUrl);
NavigateToUrl(myUrl);
}
当然可以。为什么不只设置 URL 的步骤,将 URL 分配给某个变量(在 class、ScenarioContext.Current
或自定义中上下文对象),然后在所有调用中使用此 URL。在我的 phone 中,格式化很痛苦,但像这样:
Given I'm using the site '<site>'
When I login
Then something should happen
Examples:
|site |
| aaa.com|
| bbb.com|
然后在给定的步骤中,只需记录 URL 并使用该基础 URL 在 when 步骤中构建完整的 URL。
您的步骤 class 可能看起来像这样。
[Binding]
Public class Steps
{
Private string baseUrl;
[Given ("I'm using the site '(.*)'")]
Public void GivenImUsingTheSite(string baseUrl)
{
This.baseUrl=baseUrl;
}
[When ("I log in")
Public void WhenILogIn()
{
String URL=baseUrl + "\login";
....login
}
}