As well as pop-up dialogs, WatiN, the web application automation tool, can handle modal and modeless dialogs. Here’s a step by step WatiN demonstration for a modeless dialog.
1. Create a new Console Application named “WatinDemo4” as in the post Getting Started with WatiN (Part 1) and type the following code into “Program.cs”.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using WatiN.Core; namespace WatinDemo4 { class Program { [STAThread] static void Main(string[] args) { // create a browser instance IE browser = new IE(); // open the html page saved in the debug folder browser.GoTo(AppDomain.CurrentDomain.BaseDirectory+"LaunchPage.html"); // wait for page to load browser.WaitForComplete(); // click on button to open the dialog window browser.Buttons[0].ClickNoWait(); // find the modal or modeless dialog window by title HtmlDialog dialog = browser.HtmlDialog(Find.ByTitle("Bing")); // search for some text string searchText = "Dialog box"; dialog.TextField(Find.ByName("q")).TypeText(searchText); dialog.Button(Find.ByName("go")).Click(); // a new browser instance with the search results will open // attach to this browser IE resultsBrowser = IE.AttachTo<IE>(Find.ByTitle(searchText + " - Bing")); // close the original browser (note the dialog closes too) browser.Close(); browser = null; // narrow the results by language resultsBrowser.Link(Find.ByText("Narrow by language")).Click(); resultsBrowser.Link(Find.ByText("Only English")).WaitUntilExists(); resultsBrowser.Link(Find.ByText("Only English")).Click(); System.Threading.Thread.Sleep(2000); // tidy up resultsBrowser.Close(); resultsBrowser = null; } } }
2. Create a file called “LaunchPage.html” and add the text below. Save “LaunchPage.html” in the Debug folder of the project (“WatiNDemo\WatinDemo4\bin\Debug”).
<html> <head> <title>Launch Page</title> </head> <body> <input onclick="window.showModelessDialog('http://www.bing.com/')" type="button" value="Launch Dialog"/> </body> </html>
If you want to experiment with a modal dialog, change “showModelessDialog” to “showModalDialog”.
3. Start Debugging (F5). The “Launch Page” opens and the “Launch Dialog” button is clicked. A dialog opens which contains the Bing search page. The text “Dialog box” is typed into the search box and the search button is clicked.
The search results open in a new browser.
The search is then narrowed by language to show English results only.