REQUEST A DEMO

Scenario Testing With Watin

 

We are starting to use WatiN to automate the testing of a web application. So far it is working pretty well. One of the things I found confusing was handling confirmation alerts like this:

 

 

image

 

Pop-ups like this need to be handled by Watin. The code for handling this looks like this.

 

 //setup a handler with Watin
 var confirmDialogHandler = new ConfirmDialogHandler();
 browser.DialogWatcher.Add(confirmDialogHandler);

 //click the button that causes a confirmation pop-up
 browser.Element("buttonId").ClickNoWait();

 //Clickon the ok button of the confirmation window.
 confirmDialogHandler.WaitUntilExists();
 confirmDialogHandler.OKButton.Click();
 browser.WaitForComplete();

 //You may want to test that the message is correct.

 //cleanup
 browser.DialogWatcher.RemoveAll(confirmDialogHandler);

 

There is a lot of setup and teardown for the simply clicking a button. Using some .Net 3.5 goodness I created an extension method to make this a little more terse.

 

 //usage
 Browser.OkButtonClickedOnConfirmDialog(ClickOnUnMarkMasterButton);

 //could use a lambda but this utilty method looks cleaner
 private void ClickOnUnMarkMasterButton()
 {
     Browser.Element(Find.ById("unmarkMasterContact")).ClickNoWait();
 }

 //implementation
 public static void OkButtonClickedOnConfirmDialog(this IE browser, Action actionCausingConfirmationDialog)
 {
     var confirmDialogHandler = new ConfirmDialogHandler();
     browser.DialogWatcher.Add(confirmDialogHandler);

     actionCausingConfirmationDialog();

     confirmDialogHandler.WaitUntilExists();
     confirmDialogHandler.OKButton.Click();
     browser.WaitForComplete();
     browser.DialogWatcher.RemoveAll(confirmDialogHandler);
 }

 

Thinking about this more the difficulty in testing this dialog is a sign that we should be using a JQuery lightbox to accomplish the same functionality.