Introduction
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.
Background
WatiN is a .NET web automation testing tool. More information can be found at http://watin.sourceforge.net/.
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:
<html>
<body>
<input id="myButton1" type="button" value="this is a button"
onclick="confirmMe(); return false;"><br>
<script>
function confirmMe() {
var answer = confirm ("Are you having fun?")
if (answer)
document.getElementById("myButton1").value="Clicked OK";
else
document.getElementById("myButton1").value="Clicked Cancel";
}
</script>
</body>
</html>
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;
[TestMethod()]
public void ConfirmOKTest()
{
using (IE ie = new IE("http://localhost/confirmtest.htm"))
{
ConfirmDialogHandler handler = new ConfirmDialogHandler();
using (new UseDialogOnce(ie.DialogWatcher, handler))
{
ie.Button("myButton1").ClickNoWait();
handler.WaitUntilExists();
handler.OKButton.Click();
}
ie.WaitForComplete();
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)
ie.Button("myButton1").ClickNoWait()
handler.WaitUntilExists()
handler.OKButton.Click()
End Using
ie.WaitForComplete()
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.
ie.Button("myButton1").ClickNoWait();
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.
handler.WaitUntilExists();
handler.OKButton.Click();
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".
ie.WaitForComplete();
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.
[TestMethod()]
public void ConfirmCancelTest()
{
using (IE ie = new IE("http://localhost/confirmtest.htm"))
{
ConfirmDialogHandler handler = new ConfirmDialogHandler();
using (new UseDialogOnce(ie.DialogWatcher, handler))
{
ie.Button("myButton1").ClickNoWait();
handler.WaitUntilExists();
handler.CancelButton.Click();
}
ie.WaitForComplete();
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.
History
- 08/26/2009: Added the VB version of the confirm OK test.
- 08/25/2009: First writing.