Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WatiN driving the IE "Are you sure you want to leave this page?" popup

Tags:

watin

I'd like to extend my WatiN automated tests to drive a page that guards against the user accidentally leaving the page without saving changes.

The page uses the "beforeunload" technique to seek confirmation from the user:

$(window).bind('beforeunload', function (event) {
    if (confirmationRequired) {
        return "Sure??";
    }
});

My WatIn test is driving the page using IE. I cannot find a way to get WatIn to attach to the popup dialog so I can control it from my test.

All the following have failed (where the hard-coded strings refer to strings that I can see on the popup):

Browser.AttachTo<IE>(Find.ByTitle("Windows Internet Explorer");
browser.HtmlDialog(Find.FindByTitle("Windows Internet Explorer));
browser.HtmlDialog(Find.FindByTitle("Are you sure you want to leave this page?));
browser.HtmlDialog(Find.FindFirst());

Thanks!

like image 663
MrBlueSky Avatar asked Feb 15 '12 15:02

MrBlueSky


1 Answers

You'll need to create and add the dialog handler.

Example Go to example site, click link, click leave page on confirmation dialog:

IE browser = new IE();
browser.GoTo("http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/onbeforeunload.htm");

WatiN.Core.DialogHandlers.ReturnDialogHandlerIe9 myHandler = new WatiN.Core.DialogHandlers.ReturnDialogHandlerIe9();

browser.AddDialogHandler(myHandler);
browser.Link(Find.ByUrl("http://www.microsoft.com")).ClickNoWait();
myHandler.WaitUntilExists();

myHandler.OKButton.Click();
browser.RemoveDialogHandler(myHandler);

The above is working on WatiN2.1, IE9, Win7. If using IE8 or before, you will likely need to use the ReturnDialogHandler object instead of the Ie9 specific handler

like image 82
OCary Avatar answered Jan 03 '23 14:01

OCary