Handling an Alert Dialog with WatiN

WatiN, the web application automation tool, can easily handle dialog boxes. Here we show how to handle an alert dialog on the great resource that is Paul’s Online Math Notes.

On the homepage of Paul’s Online Math Notes there is a table on the left-hand side, the first cell of which contains the text ‘n/a’ and a hyperlink:

Paul's Online Maths Notes

We want to automate the process of clicking on this link and then clicking on the Ok button in the resulting alert dialog:

Alert Dialog

The HTML for the cell we want to click on is:

Table Cell HTML

Create a Console Application named “WatinDemo3” as in the post Getting Started with WatiN (Part 1) and paste the following code into “Program.cs”

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WatiN.Core;
using WatiN.Core.DialogHandlers;

namespace WatinDemo3
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {           
            IE browser = new IE();
            
            // maximise the window
            browser.ShowWindow(WatiN.Core.Native.Windows.NativeMethods.WindowShowStyle.Maximize);
            
            browser.GoTo("http://tutorial.math.lamar.edu/");
            
            // wait for page to load
            browser.WaitForComplete();
            
            // create a dialog handler and add to browser
            AlertDialogHandler handler = new AlertDialogHandler();
            browser.AddDialogHandler(handler);

            // find the table cell (<td> tag) containing the text '-n/a' 
            TableCell tableCell = browser.TableCell(Find.ByText("-n/a-"));
            
            // get all the links in the table cell and click on the first one (index 0) 
            // Using .Click() will fail as WatiN will wait indefinitely for something to happen within the browser
            tableCell.Links[0].ClickNoWait();
            
            // wait until the alert dialog appears
            handler.WaitUntilExists();
            
            // click on the Ok button
            handler.OKButton.Click();

            // tidy up
            browser.RemoveDialogHandler(handler);
            browser.Close();            
        }
    }
}

Note that WatiN allows us to find both the table cell and the link it contains without using their ids.

Try turning off the pop-up blocker if you have difficulties getting this program to run.

Getting Started with WatiN (Part 2)

Here is a step-by-step guide to setting up an NUnit test with WatiN. In this example, the program opens a browser window, goes to bing.com, searches for the phrase “WatiN” and then tests that the results page contains the phrase “WatiN”.

1. Add a new project to the WatiNDemo solution created in the post Getting Started with WatiN (Part 1)

Step210

2. Select “Class Library” and enter the name “WatiNDemo2”

Step211

3. “Set as StartUp Project”

Step212

4. Open “Manage NuGet Packages…”

Step213

5. Search for and install “NUnit”

Step214

6. Search for and install “NUnit.Runners”

Step215

7. Go to “Add Reference…”

Step216

8. Select “WatiN.Core.dll” at location “WatiNDemo\packages\WatiN.2.1.0\lib\net40”

Step217

9. Copy the code below into the class “Class1.cs”

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using WatiN.Core;

namespace WatiNDemo2
{
    [TestFixture, RequiresSTA]
    public class Class1
    {
        private IE browser;       

        [TestFixtureSetUp]
        public void SetUp()
        {
            browser = new IE();
            browser.GoTo("http://www.bing.com/");
        }

        [TestFixtureTearDown]
        public void TearDown()
        {
            browser.Close();
            browser = null;
        }

        [Test]
        public void BingSearchForWatiN()
        {
            browser.TextField(Find.ByName("q")).TypeText("WatiN");
            browser.Button(Find.ByName("go")).Click();     
            
            Assert.IsTrue(browser.ContainsText("WatiN"));
        }
    }
}

10. Go to the project “Properties”

Step219

11. Go to “Debug” and select “Start external program”

Step220

12. Select “nunit.exe” at “WatiNDemo\packages\NUnit.Runners.2.6.2\tools”

Step221

13. Start Debugging (F5). When the NUnit window opens, go to “File”, “Open Project”

Step222

14. Select “WatiNDemo2.dll” at “WatiNDemo\WatiNDemo2\bin\Debug”

Step223

15. Click on “Run” in NUnit window

Step224

Getting Started with WatiN (Part 1)

This is a step-by-step guide to creating a simple WatiN project. When run this program will complete a Bing search for the word WatiN.

Prerequisites: MS Visual Studio 2010, NuGet package manager

1. Create a new Console Application with the name “WatiNDemo1”. Name the solution “WatiNDemo”

Step11

2. Open “Manage NuGet Packages…”

Step12

3. Search for and install “WatiN”

Step13

4. Remove the references to Interop.SHDocVw.dll and Microsoft.mshtml.dll

Step14

5. Go to the project “Properties”

Step15

6. Set the “Target Framework” as “.NET Framework 4”

Step16

7. Copy the code below into the class “Program.cs”

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WatiN.Core;

namespace WatiNDemo1
{
    public class Program
    {
        [STAThread]
        public static void Main(string[] args)
        {
            BingSearchForWatiN();
        }

        public static void BingSearchForWatiN()
        {
            using (var browser = new IE("http://www.bing.com"))
            {
                browser.TextField(Find.ByName("q")).TypeText("WatiN");
                browser.Button(Find.ByName("go")).Click();
            }
        }
    }
}

8. Start Debugging (F5)

Open Source Web Application Testing Tools

I recently had to research open source tools for the functional testing of web applications. My quick review led to the following table.

Tool Languages Stable Release Browsers Test Recorder
Watir Ruby 4.0 / September 30, 2012 IE, Firefox, Chrome, Safari, Opera Yes (TestWise Recorder – Firefox extension)
Watij Java, Ruby 1.3.1 / December 16, 2010 IE, Firefox, Safari No
WatiN C# 2.1 / April 12, 2011 IE, Firefox No
Selenium C#, Java, Perl, PHP, Python, Ruby 2.31 / February 27, 2013 IE, Firefox, Chrome, Safari, Opera Yes (Firefox extension)
Sikuli Python, Java 1.0.0 / May 22, 2013 Any GUI No
Sahi Open Source JavaScript 4.3 / February 6, 2013 Browser independent Yes
Canoo WebTest XML 3.0 / March 5, 2009 HtmlUnit Yes (Firefox extension)