我正在尝试使用 Specflow 中的场景大纲,它将根据我拥有的示例数量生成测试,然后我可以通过 C# 绑定(bind)使用 selenium 编写测试。所以我创建了以下功能:
Scenario Outline: Login
Given I have navigated to the Login Page
When I enter a valid '<Login>'
And I enter the correct '<Password>'
And I press the Login CTA
Then I am logged into the Home Page
Examples:
| Login | Password |
| LoginA | passwordA |
| LoginB | passwordB |
当我生成步骤定义时,我得到以下信息:
[When(@"I enter a valid '(.*)'")]
public void WhenIEnterAValid(string p0)
{
ScenarioContext.Current.Pending();
}
[When(@"I enter the correct '(.*)'")]
public void WhenIEnterTheCorrect(string p0)
{
ScenarioContext.Current.Pending();
}
我已将它们放入 SpecFlow 步骤定义文件中
我还生成了 2 个名为
登录(“登录A”,“密码A”,null)
登录("登录B","密码B",null)
到目前为止一切都很好(我认为)。接下来的步骤是编写代码来完成步骤定义,以便每个测试都将运行并使用相关数据。这就是我所坚持的一点。
例如,我知道数据是否位于标准场景中的表中,我可以从表中调用,因此功能如下 -
Scenario: Login
Given I have navigated to the Login Page
When I enter a valid Login
| Login |
| loginA |
And I enter the correct Password
| Password |
| passwordA |
And I press the Login CTA
Then I am logged into the Home Page
可以对如下代码感到满意:
public void WhenIEnterAValidLogin(Table table)
{
IWebElement loginField = WebBrowser.Current.FindElement(By.Name("loginBox"));
string loginText = table.Rows[0]["Login"].ToString();
usernameField.SendKeys(loginText);
}
但基本上我不知道如何编写代码,以便它在“示例”表中查找并获取每个测试的相关数据。这是可能的还是我必须为每个测试单独编写代码,即输入“loginA”的步骤和输入“loginB”的步骤?我浏览了网络,但没有找到可以帮助我的示例。
感谢您提前提供的任何帮助!如果我没有说清楚或者犯了一些基本错误,请告诉我,因为我是堆栈溢出的新手,这是我的第一篇文章。
最佳答案
我觉得你想得太深了。如果您执行以下操作:
Scenario Outline: Login
Given I have navigated to the Login Page
When I enter a valid '<Login>'
And I enter the correct '<Password>'
And I press the Login CTA
Then I am logged into the Home Page
Examples:
| Login | Password |
| LoginA | passwordA |
| LoginB | passwordB |
您实际上正在制作以下两个场景:
Scenario: Login_LoginA
Given I have navigated to the Login Page
When I enter a valid 'LoginA'
And I enter the correct 'PasswordA'
And I press the Login CTA
Then I am logged into the Home Page
Scenario: Login_LoginB
Given I have navigated to the Login Page
When I enter a valid 'LoginB'
And I enter the correct 'PasswordB'
And I press the Login CTA
Then I am logged into the Home Page
步骤代码被重用。
场景大纲是创建大量场景的好方法,这些场景可以在短时间内仅使用不同的数据执行相同的操作。
您对创建的两个测试的看法是正确的,这使得它在您的测试运行器中更易于管理。这两个测试是实际的 TestMethods
,它调用名为 Login 的内部方法:
// this method is created to be called from a scenario outline
// this contains the steps that one iteration of the outline should take.
public virtual void Login(string login, string password, string[] exampleTags)
{
TechTalk.SpecFlow.ScenarioInfo scenarioInfo = new TechTalk.SpecFlow.ScenarioInfo("Login", exampleTags);
#line 13
this.ScenarioSetup(scenarioInfo);
#line 14
testRunner.Given("I have navigated to the Login Page", ((string)(null)), ((TechTalk.SpecFlow.Table)(null)), "Given ");
#line 15
testRunner.When(string.Format("I enter a valid \'{0}\'", login), ((string)(null)), ((TechTalk.SpecFlow.Table)(null)), "When ");
#line 16
testRunner.And(string.Format("I enter the correct \'{0}\'", password), ((string)(null)), ((TechTalk.SpecFlow.Table)(null)), "And ");
#line 17
testRunner.And("I press the Login CTA", ((string)(null)), ((TechTalk.SpecFlow.Table)(null)), "And ");
#line 18
testRunner.Then("I am logged into the Home Page", ((string)(null)), ((TechTalk.SpecFlow.Table)(null)), "Then ");
#line hidden
this.ScenarioCleanup();
}
[Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute()]
[Microsoft.VisualStudio.TestTools.UnitTesting.DescriptionAttribute("Login")]
[Microsoft.VisualStudio.TestTools.UnitTesting.TestPropertyAttribute("VariantName", "LoginA")]
[Microsoft.VisualStudio.TestTools.UnitTesting.TestPropertyAttribute("Parameter:Login", "LoginA")]
[Microsoft.VisualStudio.TestTools.UnitTesting.TestPropertyAttribute("Parameter:Password", "passwordA")]
public virtual void Login_LoginA()
{
// Calling the inner method Login
this.Login("LoginA", "passwordA", ((string[])(null)));
}
[Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute()]
[Microsoft.VisualStudio.TestTools.UnitTesting.DescriptionAttribute("Login")]
[Microsoft.VisualStudio.TestTools.UnitTesting.TestPropertyAttribute("VariantName", "LoginB")]
[Microsoft.VisualStudio.TestTools.UnitTesting.TestPropertyAttribute("Parameter:Login", "LoginB")]
[Microsoft.VisualStudio.TestTools.UnitTesting.TestPropertyAttribute("Parameter:Password", "passwordB")]
public virtual void Login_LoginB()
{
this.Login("LoginB", "passwordB", ((string[])(null)));
}
关于c# - 将 Scenario Outline 与 Specflow 和 Selenium C# 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32269563/