[英]Using Scenario Outlines with Specflow & Selenium C#
我正在嘗試在 Specflow 中使用場景大綱,它將根據我擁有的示例數量生成測試,然后我可以通過 C# 綁定使用 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 Step Definition 文件中
我還生成了 2 個名為的測試
登錄(“登錄A”,“密碼A”,空)
登錄(“登錄B”,“密碼B”,空)
到目前為止一切順利(我認為)。 接下來的步驟是編寫代碼以完成步驟定義,以便每個測試都能運行並使用相關數據。 這是我堅持的一點。
例如,我知道數據是否在標准場景中的表中,我可以從表中調用,因此功能如下 -
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
步驟代碼被重用。
場景大綱是創建大量場景的好方法,這些場景在短時間內只用不同的數據做同樣的事情。
您對創建的兩個測試是正確的,這使其在您的測試運行程序中更易於管理。 這兩個測試是調用名為 Login 的內部方法的實際TestMethods
:
// 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)));
}
您可以像這樣訪問堆棧跟蹤。 嘗試和測試。 您不僅限於一個屬性名稱。
var stackTraces = new StackTrace();
foreach (var stackFrame in stackTraces.GetFrames())
{
MethodBase methodBase = stackFrame.GetMethod();
TestPropertyAttribute[] attributes = methodBase.GetCustomAttributes(typeof(TestPropertyAttribute), false) as TestPropertyAttribute[];
if (attributes != null && attributes.Length >= 1)
{
variantName = attributes.FirstOrDefault(x => x.Name.Equals("VariantName"))?.Value;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.