Watin - Handling Confirm Dialogs with ConfirmDialogHandler

Matt Roberts picture Matt Roberts · May 19, 2009 · Viewed 8.4k times · Source

Using Watin, I'm trying to handle a confirm dialog box and tell watin to press "OK". This is reasoanbly well documented on the internet - you use a ConfirmDialogHandler and the UseDialogOnce method.. Except it isn't working for me. I get the following error:

WatiN.Core.Exceptions.WatiNException: Dialog not available within 5 seconds

I'm using the watin 2.0 beta atm, but I was previously using an earlier version of 1.X which had the same issue. Tested on a colleagues machine running 64 bit Vista, I'm running 64 bit Windows 7.

The code looks like this:

        using (IE ie = new IE("http://localhost/TestApp/TestConfirmPage.asp"))
        {
            var approveConfirmDialog = new ConfirmDialogHandler();

            using (new UseDialogOnce(ie.DialogWatcher, approveConfirmDialog))
            {
                ie.Button(Find.ByName("btn")).ClickNoWait();
                approveConfirmDialog.WaitUntilExists(5);
                approveConfirmDialog.OKButton.Click();
            }
            ie.WaitForComplete();
        }

The ASP page is very simple, it consists of a button that forces a confirm, like this:

<input type="button" name="btn" id="btn" value="Click me" onclick="ConfirmApp()"  />

And ConfirmApp has been stripped down for testing so that now all it contains is:

 bOK = confirm("You clicked a popup. Did you mean to?");
 return bOK;

Answer

andreja picture andreja · May 27, 2009

The code looks fine to me, and I think it should work. The only think I did differently it was to put Wait for Complete inside using Dialog block. Don't know why but before I did that I also have some issues, sometimes it works sometimes it doesn't. And I don't use time limitation at Wait until exists. But you probably already tried that one.

For example:

using (new UseDialogOnce(ie.DialogWatcher, approveConfirmDialog))
        {
            ie.Button(Find.ByName("btn")).ClickNoWait();
            approveConfirmDialog.WaitUntilExists();
            approveConfirmDialog.OKButton.Click();
            ie.WaitForComplete();
        }