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:
Pop-ups like this need to be handled by Watin. The code for handling this looks like this.
1: //setup a handler with Watin
2: var confirmDialogHandler = new ConfirmDialogHandler();
3: browser.DialogWatcher.Add(confirmDialogHandler);
4:
5: //click the button that causes a confirmation pop-up
6: browser.Element("buttonId").ClickNoWait();
7:
8: //Clickon the ok button of the confirmation window.
9: confirmDialogHandler.WaitUntilExists();
10: confirmDialogHandler.OKButton.Click();
11: browser.WaitForComplete();
12:
13: //You may want to test that the message is correct.
14:
15: //cleanup
16: 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.
1: //usage
2: Browser.OkButtonClickedOnConfirmDialog(ClickOnUnMarkMasterButton);
3:
4: //could use a lambda but this utilty method looks cleaner
5: private void ClickOnUnMarkMasterButton()
6: {
7: Browser.Element(Find.ById("unmarkMasterContact")).ClickNoWait();
8: }
9:
10: //implementation
11: public static void OkButtonClickedOnConfirmDialog(this IE browser, Action actionCausingConfirmationDialog)
12: {
13: var confirmDialogHandler = new ConfirmDialogHandler();
14: browser.DialogWatcher.Add(confirmDialogHandler);
15:
16: actionCausingConfirmationDialog();
17:
18: confirmDialogHandler.WaitUntilExists();
19: confirmDialogHandler.OKButton.Click();
20: browser.WaitForComplete();
21: browser.DialogWatcher.RemoveAll(confirmDialogHandler);
22: }
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.