【问题标题】:How to fail tests by default if a dialog appears in watin如果 watin 中出现对话框,默认情况下如何使测试失败
【发布时间】:2009-04-08 10:24:13
【问题描述】:

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

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

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

【问题讨论】:

    标签: c# .net testing automation watin


    【解决方案1】:

    另一种选择是使用 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

    【讨论】:

    • 我遇到了这种方法的问题,它有时有效,有时无效。
    【解决方案2】:

    目前我们正在使用:

    browser.DialogWatcher.CloseUnhandledDialogs = false
    

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

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

    【讨论】:

      猜你喜欢
      • 2021-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-30
      • 1970-01-01
      • 1970-01-01
      • 2012-08-02
      • 1970-01-01
      相关资源
      最近更新 更多