【发布时间】:2014-12-16 15:10:17
【问题描述】:
我结合使用 SpecFlow、MSTests 进行单元测试,并使用 WatiN 驱动浏览器来测试我们的 Web 应用程序。
- Visual Studio 2010
- 类库风格项目
- 规范流
- 质谱测试
- 等待
如果用户在未填写所有必填字段的情况下提交我们的表单,则会弹出 JavaScript alert。我正在尝试使用 WatiN 检测此弹出窗口。触发警报的 SpecFlow 步骤与断言弹出窗口存在的 SpecFlow 步骤不同,因此等待 WatiN 对话处理程序不起作用,因为警报已经打开。
示例 SpecFlow 场景:
Scenario: Form Fields are required
# This step spawns the alert dialog
When I click the "Save and Continue" button
# Test fails here because the alert is already open
Then I should see the validation error alert
When I click the "Save and Continue" button 的步骤定义
[When(@"I click the ""(.*)"" button")]
public void WhenIClickTheButton(string buttonText)
{
Button button = BitWeb.Browser.Button(Find.ByValue(buttonText).Or(Find.ByText(buttonText)));
Assert.IsTrue(button.Exists, "No button with text '{0}' was found", buttonText);
button.Click();
browser.WaitForComplete();
}
Then I should see the ... alert 的步骤定义:
[Then(@"I should see the validation error alert")]
public void ThenIShouldSeeTheValidationErrorAlert()
{
var alert = new WatiN.Core.DialogHandlers.AlertDialogHandler();
alert.WaitUntilExists();
StringAssert.Contains(alert.Message, "An error has occurred. Check entire", "The validation error alert was not visible.");
}
对alert.WaitUntilExists(); 的调用会引发异常:
WatiN.Core.Exceptions.WatiNException:对话框在 30 秒内不可用。
当我断言警报对话框可见时,DialogHandler 找不到警报,因为它已经打开。
如何找到已使用 WatiN 打开的警报对话框?
更新 #1: 我倾向于使用 ScenarioContext.Current 对象的解决方案,我只是不确定如何将它们连接在一起,这样浏览器就不会等待 30 秒每次点击按钮只是为了查看是否弹出警告框。
更新#2:经过一番调查,一步点击按钮会导致整个测试框架暂停,直到alert 对话框被关闭。单击“确定”按钮关闭对话框,这允许测试运行程序前进到下一步,在此我断言对话框是可见的——先有鸡还是先有蛋的场景。致电button.ClickNoWait() 解决了这个问题。
【问题讨论】:
-
对话框是html对话框(jquery类型)还是实际弹出对话框(win32)?
-
这是一个
alert("message")对话框。
标签: javascript c# internet-explorer-8 watin specflow