WatiN 似乎找不到 JavaScript 警报 [英] WatiN seems to not find JavaScript alert

查看:23
本文介绍了WatiN 似乎找不到 JavaScript 警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Web 应用程序,在某些情况下,它会弹出我需要在 WatiN 测试中做出反应的 JavaScript 警报().Google 向我指出了在 WATIN 中处理警报在 2007 年,这似乎很有希望,我将该帖子中的示例代码修改为以下内容(匿名):

I have a web application that, under some conditions, pop up JavaScript alert()s that I need to react to in a WatiN test. Google pointed me at Handling alerts in WATIN from way back in 2007 that seemed promising, and I adapted the example code in that post into the following (anonymized):

    private void MyAssert(IE browser, WatinHelper helper)
    {
        AlertDialogHandler alertDialogHandler = new AlertDialogHandler();

        using (new UseDialogOnce(browser.DialogWatcher, alertDialogHandler))
        {
            // DoWrong() causes a JavaScript alert(); false means use nowait.
            DoWrong(helper, false);

            alertDialogHandler.WaitUntilExists(10 /*seconds*/);

            if (!alertDialogHandler.Exists())
            {
                Assert.Fail("No JavaScript alert when it should have been there");
            }

            alertDialogHandler.OKButton.Click();
        }

        SecondAssert(browser);
    }

然而,虽然当 DoWrong() 被调用时,警报几乎是立即显示的(正如它应该的那样),对 alertDialogHandler.WaitUntilExists() 的调用最终会失败,并显示 WatiNException: Dialog not available in 10 秒...唯一的问题是我可以看到对话框肯定在屏幕上.

However, while the alert is displayed virtually instantaneously (as it is supposed to) when DoWrong() is called, the call to alertDialogHandler.WaitUntilExists() eventually fails with a WatiNException: Dialog not available within 10 seconds... The only problem was that I could see that the dialog most definitely was up on the screen.

我可能遗漏了一些简单的东西;有人能指出我正确的方向吗?

I'm probably missing something simple; can someone point me in the right direction please?

我还尝试了以下两种变体,以及它们的一些变体,但都没有成功;我一直收到同样的错误.

I have also tried the following two variants, and some variations of them, with no luck; I keep getting the same error.

        AlertDialogHandler alertDialogHandler = new AlertDialogHandler();

        DoWrong(helper, false);

        System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
        stopwatch.Start();

        do
        {
        }
        while (!alertDialogHandler.Exists() && stopwatch.Elapsed.TotalMilliseconds < 3000);

        Assert.IsTrue(alertDialogHandler.Exists(), "No JavaScript alert when it should have been there");

        alertDialogHandler.OKButton.Click();
        SecondAssert(browser);

        AlertDialogHandler alertDialogHandler = new AlertDialogHandler();
        browser.DialogWatcher.Add(alertDialogHandler);
        DoWrong(helper, false);
        alertDialogHandler.WaitUntilExists();
        alertDialogHandler.OKButton.Click();
        browser.WaitForComplete();
        Assert.IsFalse(alertDialogHandler.Exists());
        SecondAssert(browser);

是的,我知道代码变得有点难看,但现在我主要是试图让它工作.如果由于我第二次尝试中的紧密循环而使 CPU 以 100% 的利用率等待几秒钟,但只执行我需要的操作(简单明了,忽略 alert()),没关系.

Yes, I know that code is getting a bit ugly, but right now I'm mostly trying to get it to work at all. If it sits for a few seconds cooking the CPU at 100% utilization because of the tight loop in my second attempt, but only does what I need it to (plain and simple, dismiss that alert()), it's OK.

推荐答案

这是 WatiN 和 IE8 的问题,以及 IE8 改变其创建弹出窗口的方式.该问题已在项目的 Sourceforge SVN 存储库中可用的当前代码中得到解决.得到它,编译它,你的问题就解决了.

This is an issue with WatiN and IE8 and the way IE8 changed the way it creates popups. The issue is fixed in the current code available at the Sourceforge SVN repository for the project. Get it, compile it and your problem is solved.

WatiN 的新版本将在今年年底前推出.

A new release of WatiN will be available before the end of this year.

HTH,杰伦

这篇关于WatiN 似乎找不到 JavaScript 警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆