Click here to Skip to main content
13,505,369 members
Click here to Skip to main content
Add your own
alternative version


14 bookmarked
Posted 25 Aug 2009

Quick and easy WatiN recipe: How to handle a confirm dialog

, 27 Aug 2009
Rate this:
Please Sign up or sign in to vote.
How to process confirm dialogs in WatiN.


This is a bite-sized how-to guide, or recipe, that demonstrates how to use WatiN to automate a web page with a confirm dialog.

We'll start by creating a simple HTML page and write two tests that will exercise the dialog.


WatiN is a .NET web automation testing tool. More information can be found at

We'll be using the Microsoft Team Test in Visual Studio 2008 Team System, but any Unit Test framework will do.

Creating the Page

In order to test a confirm dialog, we'll need a web page. Our sample page is very simple and very testable:

    <input id="myButton1" type="button" value="this is a button" 

      onclick="confirmMe(); return false;"><br>
      function confirmMe() {
        var answer = confirm ("Are you having fun?")
        if (answer)
          document.getElementById("myButton1").value="Clicked OK";
          document.getElementById("myButton1").value="Clicked Cancel";

As you can see, we have a button that calls a function. The function pops up a confirm dialog that sets the button's caption depending on whether the user clicked OK or Cancel.

Now, we need to test the page!

Writing the Tests

We are going to create two test methods, one for testing the OK button, and the other for testing the Cancel button. First, create your Unit Test project and set a reference to your WatiN binary, WatiN.Core.dll. Again, we're using the Microsoft Unit Test framework, so we'll create a new Test Project and add a Unit Test.

using WatiN.Core;
using WatiN.Core.DialogHandlers;

public void ConfirmOKTest()
    using (IE ie = new IE("http://localhost/confirmtest.htm"))
        ConfirmDialogHandler handler = new ConfirmDialogHandler();
        using (new UseDialogOnce(ie.DialogWatcher, handler))
        Assert.AreEqual("Clicked OK", ie.Button("myButton1").Text);

If you'd rather see the test in VB:

<TestMethod()>Public Sub ConfirmOKTest()
  Dim ie As New IE("http://localhost/ConfirmTest.htm")
  Dim handler As New ConfirmDialogHandler()
  Using udo As New UseDialogOnce(ie.DialogWatcher, handler)
  End Using
  Assert.AreEqual("Clicked OK", ie.Button("myButton1").Text)
End Sub

For our little recipe, the test is fairly simple. We make sure we are using the two WatiN classes we'll need: Core for the base functionality and IE automation, and DialogHandler to actually control the dialog.

The IE object is disposable, and we want to make sure it is released when we are done with it. We'll create a new IE instance named ie and set the starting URL to the page we created above.

Next, we need to add a handler. This is the object that actually manages the button clicks in the dialog box that we'll pop up. Once again, this object is disposable and it only needs to stay in scope while we're actually using it. Specifically, we are going to use a ConfirmDialogHandler and tell WatiN we're only going to handle it one time with a new UseDialogOnce, instantiated with our browser's built-in DialogWatcher and the ConfirmDialogHandler we just created.

Our button has an ID of myButton1, so we'll reference that and click the button without waiting for the browser to respond to our click. Our dialog handler will take care of that for us.


Now, we just wait until the confirm dialog exists and tell the handler which button we want to click. For this test, we'll click OK.


Now the dialog has been handled, so we wait for IE to finish processing our clicks. We can check whether our action was successful, which would result in the caption of the button changing from "this is a button" to "Clicked OK".

Assert.AreEqual("Clicked OK", ie.Button("myButton1").Text);

Naturally, we'll want to check the behavior of the Cancel button, too. Our test is almost identical, but instead of the OK button and our OK button caption change, we'll check for the Cancel action.

public void ConfirmCancelTest()
    using (IE ie = new IE("http://localhost/confirmtest.htm"))
        ConfirmDialogHandler handler = new ConfirmDialogHandler();
        using (new UseDialogOnce(ie.DialogWatcher, handler))
        Assert.AreEqual("Clicked Cancel", ie.Button("myButton1").Text);

This test clicks Cancel and checks that the button caption has been changed to "Clicked Cancel".

Easy as pie!

Points of Interest

This was a pretty simple solution that can handle surprisingly complicated scenarios. WatiN makes it extremely easy for my team to automate testing of our web applications.


  • 08/26/2009: Added the VB version of the confirm OK test.
  • 08/25/2009: First writing.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Jonathan Franks
Software Developer (Senior)
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Kanasz Robert28-Sep-12 5:57
mvpKanasz Robert28-Sep-12 5:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180417.1 | Last Updated 27 Aug 2009
Article Copyright 2009 by Jonathan Franks
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid