Watin AddDialogHandler方法在FF中抛出空引用异常

时间:2010-06-20 14:56:11

标签: watin

我正在尝试将ConfirmDialogHandler添加到对话框处理程序中,并且我得到NullReferenceException。 这种情况发生在最新的watin版本的FF 3.6上,相同的代码适用于IE7和8。

   ConfirmDialogHandler confirmDialogHandler = new ConfirmDialogHandler();
   browser.AddDialogHandler(confirmDialogHandler); // --> exception here

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

旧解决方案

发现问题。

创建FF会话时未初始化DialogWatcher将此行添加到watin代码中:

    private void CreateFireFoxInstance(string url)
    {
        Logger.LogAction("Creating FireFox instance");

        UtilityClass.MoveMousePoinerToTopLeft(Settings.AutoMoveMousePointerToTopLeft);

        var clientPort = GetClientPort();
        clientPort.Connect(url);
        _ffBrowser = new FFBrowser(clientPort);
        StartDialogWatcher();  // <-- Added line
        WaitForComplete();
    }

请注意 - 这是解决方法

提供的解决方案无效 这是我的解决方法: 我使用AutoIt dll并通过我自己的代码示例处理弹出窗口:

    using AutoItX3Lib;

    public static void RunAutomaticEnterCommand(Session session)
    {
        var autoIt = GetPopupHandle();
        autoIt.Send("{ENTER}");
        Thread.Sleep(1000);
    }


    /// <summary>
    /// Cancel the popup
    /// For FF
    /// </summary>
    /// <param name="session"></param>
    public static void RunAutomaticCancelCommand(Session session)
    {
        var autoIt = GetPopupHandle();
        autoIt.Send("{TAB}");
        autoIt.Send("{ENTER}");
        Thread.Sleep(1000);
    }

    /// <summary>
    /// Return the autoit popup handler
    /// AutoIt is a script language, we using it to handle the firefox popups
    /// </summary>
    /// <returns></returns>
    private static AutoItX3Class GetPopupHandle()
    {
        var autoIt = new AutoItX3Class();
        autoIt.AutoItSetOption("WinTitleMatchMode", 2);
        const string partialTitle = "The page at"; //the popup in Firefox starts with this text
        autoIt.WinWait(partialTitle, "", 30);
        string fullTitle = autoIt.WinGetTitle(partialTitle); //Get the whole popup title

        autoIt.WinActivate(fullTitle); //Get focis to the popup
        if (autoIt.WinWaitActive(fullTitle, "", 20) == 0)
        {
            reporter.Report("Failed to get the FireFox popup handle", false);
            return autoIt;
        }
        return autoIt;
    }