Modal dialog’s and dropdowns in Watin

Although I don’t yet have time to start using WATIN full time at work, I managed to convert a few more tests over to compare how the tests work. I am doing this in my spare time (i.e lunch and after work), to building A proof of concept and see how the framework will go in real world testing situations. For this post I have removed all references to our real application and mocked up a sample application.

The part of our app that I have simulated has a model dialog, a group of drop down’s in that each refresh when the one above it changes, and an image button implemented on an ‘input’ tag to launch the dialog. The sample dialog looks like this mock-up.

The test case was a very simple one, click the image, select a value in each of the drop-down’s, click OK then validate the page contains some text. There were a few interesting challenges, but nothing that I couldn’t solve by adding some code to the Watin framework and recompiling. Here is an altered version of the resultant code.

 [TestMethod]
 public void AddNewUser()
 {
    using (IE ie = new IE(baseURL))
    {
        // Click on the 'Manage customers' button to open the customer
        // management screen
        ie.MainDocument.Button(Find.ByName(btnManageCustomers)).Click();

    // Click on the add customer image button (an INPUT tag with type image)
    // We need to use ClickNoWait as the Create customer dialog is a modal dialog
    // and the readystate does not return to complete until the dialog is closed.
    ie.MainDocument.Input(Find.ById(btnSelectTicketType_id)).ClickNoWait();

    // Find the new 'Create new customer' dialog that opens in the collection
    // of dialogs and wait a maximum of 2 seconds for the dialog
    HtmlDialog dialog = ie.HtmlDialog(Find.ByTitle("Select ticket type"),2);

    // Select Melbourne, Qantas and Business class
    // We need the thread.sleep as these contols load dynamically based on
    // the previous selection and we are in dialog mode
    dialog.MainDocument.SelectList(Find.ById(ddlDestination_Id)).Select("Melbourne");
    Thread.Sleep(2000);
    dialog.MainDocument.SelectList(Find.ById(ddlCustomerAirline_Id)).Select("Qantas") ;
    Thread.Sleep(2000);
    dialog.MainDocument.SelectList(Find.ById(ddlCustomerClass_Id)).Select("Business");
    Thread.Sleep(2000);

    dialog.MainDocument.Button(Find.ById(btnOk_Id)).ClickNoWait();
    ie.WaitForComplete();

    // Test that the text 'Melboune' is in the page
    Assert.IsTrue(ie.MainDocument.ContainsText("Melbourne"));

    // Test that the text 'Qantas' is in the page
    Assert.IsTrue(ie.MainDocument.ContainsText("Qantas"));

    // Test that the text 'Business class' is in the page
    Assert.IsTrue(ie.MainDocument.ContainsText("Business class"));
}</pre>

So what were the traps?

Internet Explorer implements a ReadyState property which indicates that the browser is loading, rendering the page, etc. ReadyState typically returns an enum value of READYSTATE_COMPLETE when the browser is loaded and ready for more input. Where the challenge comes in is that his behaviour changes when when a modal dialogue is shown The browser does not return READYSTATE_COMPLETE, yet it is waiting for user input. To work around this issue Watin implements a ClickNoWait() method to use instead of the Click() method under these circumstances.

The problem I ran into (apart from an un-handled exception being thrown from the click method) was that the applicaiton dynamically loads the value of one dropdown after a value is selected in the one above it. This required a Thread.Sleep call to pause the script for 2 seconds, after each drop-down list is selected. I don't think that this solution is very elegant, and it adds some 6 additional seconds to the execution time for this test. This definitely warrants some further investigation into what the IE ReadyState is when the dialog is shown, writing another wait method for the Watin ie object, that waits for that state to change.

Anyway so far Watin is meeting my expectations and still looks like it will be able to handle just about everything I can throw at it.

Testing Watin3 comments