3

每当出现对话框并且没有附加处理程序时,watin 自动关闭对话框。当您不想为应用程序可能具有的不同/几个简单确认添加代码时,这很有帮助。

问题是使用这种默认行为可能会导致一些简单的问题被忽视,例如在不应该出现的场景中出现确认对话框。

我正在寻找一种简单的方法来在出现未处理的对话框时优雅地使测试失败。优雅地,我的意思是当对话框出现异常时测试会立即停止,这会给出一个体面的消息,让您知道这是一个意外的对话框错误。

4

2 回答 2

3

另一种选择可能是使用 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 Jeroen van Menen 领导开发者 WatiN

于 2009-04-10T08:11:45.147 回答
1

现在我们正在使用:

browser.DialogWatcher.CloseUnhandledDialogs = false

它有以下(丑陋的)问题:

  1. 该错误显示为下一个操作的超时(使用消息“Internet Explorer 忙时超时”)。
  2. 由于上述原因,测试中存在不必要的延迟
  3. 意外弹出的实例保持打开状态(在处置之后)。
于 2009-04-08T10:25:05.087 回答