Search code examples
watin

Watin AddDialogHandler method throwing null reference exception in FF


I'm trying to add ConfirmDialogHandler to dialog handler and i get NullReferenceException. This happens on FF 3.6 with latest watin version, the same code works on IE7 and 8.

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

Any ideas?


Solution

  • Old Solution

    Found the problem.

    The DialogWatcher was not initialized when FF session was created added this line to watin code:

        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();
        }
    

    Please Note - This is the workaround

    The provided solution didn't worked This is my workaround: I used AutoIt dll and handled the popups by my self, code sample:

        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;
        }