另一种选择是使用 AlertAndConfirmDialogHandler。这个处理程序会关闭每个弹出的警报或确认对话框,但首先它会获取对话框显示的文本并将其存储。您可以检查此 Alerts 字符串数组并查看 Count 是否为零。您可以在测试类的 Teardown 或 FixtureTeardown 中执行此操作。
从 WatiN 单元测试中复制一份测试,向您展示如何使用此处理程序:
[Test]
public void AlertAndConfirmDialogHandler()
{
DialogWatcher dialogWatcher;
Assert.AreEqual(0, Ie.DialogWatcher.Count, "DialogWatcher count should be zero before test");
// Create handler for Alert and confirm dialogs and register it.
var dialogHandler = new AlertAndConfirmDialogHandler();
using (new UseDialogOnce(Ie.DialogWatcher, dialogHandler))
{
Assert.AreEqual(0, dialogHandler.Count);
Ie.Button("helloid").Click();
Assert.AreEqual(1, dialogHandler.Count);
Assert.AreEqual("hello", dialogHandler.Alerts[0]);
// remove the alert text from the queue by using Pop
Assert.AreEqual("hello", dialogHandler.Pop());
Assert.AreEqual(0, dialogHandler.Count);
// Clear the queue
Ie.Button("helloid").Click();
Assert.AreEqual(1, dialogHandler.Count);
dialogHandler.Clear();
Assert.AreEqual(0, dialogHandler.Count);
dialogWatcher = Ie.DialogWatcher;
}
Assert.AreEqual(0, dialogWatcher.Count, "DialogWatcher count should be zero after test");
}
这也促使我使 AutoClose 行为更具可插入性。如果可以注册一个对话框处理程序,如果没有其他处理程序可以处理一个对话框,它将被调用,而不是仅仅自动关闭对话框,那将是很好的。
HTH
杰伦·范梅宁
首席开发者WatiN