TestDriven.Me

I hope to spend some time discussing testing with VB.NET. I don't think of myself as an expert, but I have had some experience and certainly some opinions. I will try to share some of those opinions along with some information and hopefully generate some discussion so that we all can improve our skills a bit.
WatiN API Reference - ActiveElement

WatiN contains classes and methods to assist in automating activities within Internet Explorer and Firefox.  Each of the methods available will be discussed along with examples of usage in the sections that follow:

Examples of how to use each command available to the IE instance will be provided.  All examples will use either the simple default.aspx page created earlier or one of the html pages distributed with WatiN.  Each command will be provided with at least one test which can be run as is.  You should copy all of the html files that are in the \src\UnitTests\html folder inside of the folder in which WatiN was installed into your test solution using the following procedure if you wish to run these tests: 
  • Copy the entire contents of the WatiN\src\UnitTests\html\ folder including any sub-folders into the folder where your solution resides.  Make note of the new folders that will be needed.
  • Right click on the WatiNTest project where you placed the default.aspx page and choose Add – New Folder and add a folder for each of the folders noted previously. 
  • Right click on each of the new folders and choose Add – Existing Item.
  • Browse to the respective folder under your solution and add all of the items therein.
  • Right click on the WatiNTest project where you placed the default.aspx page and choose Add – Existing Item. 
  • Browse to the folder where you solution is located and CTRL-click on each html file, then click on the Add button.
 

ActiveElement

Each web page can have one and only one element with the focus at any one time.  It is possible when a page has first been opened that there may be no active element (but the page itself).  ActiveElement may be used to assign the element with the focus to a variable or to issue actions against the element.

Assignment

You can assign the active element with a command like the one that follows:

 

    ''' <summary>
    ''' Ensure that the textbox displays what is set to ActiveElement
    ''' </summary>
    ''' <remarks>
    ''' currentURL is a Private Const =
    ''' "http://localhost:3587/Default.aspx"
    ''' MessageTextBox is a Private Const = "MessageTextBox"
    ''' </remarks>
    <Test()> _
     Public Sub TestActiveElementAssignment()
        Using ie As IE = New IE(currentURL)
 
          ie.TextField(Find.ById(New Regex(MessageTextBox))).Value =
ie.ActiveElement.Id
         End Using
    End Sub

This simple example opens the default web page created previously and displays the ID of the active element in the message textbox.  Since no control will have focus, the message textbox should be blank.

 

Next is a more verbose test which uses the text box on the default.aspx to hold information about the active element and checks to see if it is information about the correct control.

 

    ''' <summary>
    ''' Ensure that the OK button is set to ActiveElement
    ''' </summary>
    ''' <remarks>
    ''' currentURL is a Private Const =
    ''' "http://localhost:3587/Default.aspx"
    ''' OKButton is a Private Const = "OKButton"
    ''' MessageTextBox is a Private Const = "MessageTextBox"
    ''' </remarks>
    <Test()> _
     Public Sub TestActiveElementAssignment()
        Dim aStringValue As String = ""
 
        Using ie As IE = New IE(currentURL)
          'Set the focus on the OKButton
          ie.Button(Find.ById(New Regex(OKButton))).Focus()
 
          ie.TextField(Find.ById(New Regex(MessageTextBox))).Value =
ie.ActiveElement.Id
          aStringValue = _
    ie.TextField(Find.ById(New Regex(MessageTextBox))).Text.Trim
          Assert.IsTrue(aStringValue.Equals(OKButton),
    "The ID of the OKButton should be in the textbox now.")
 
        End Using
    End Sub

Example of Usage

Once you have an active element, you can use the methods exposed by it.  Each type of element will have its own set of available methods, of course.  The following example shows setting a Button to the active element as was done in the last test.  This time, the test will exercise the click event on the button:
    ''' <summary>
    ''' Ensure that the OK button is set to ActiveElement
    ''' </summary>
    ''' <remarks>
    ''' currentURL is a Private Const =
    ''' "http://localhost:3587/Default.aspx"
    ''' OKButton is a Private Const = "OKButton"
    ''' MessageTextBox is a Private Const = "MessageTextBox"
    ''' </remarks>
    <Test()> _
     Public Sub TestActiveElementClick()
        Dim aStringValue As String = ""
 
        Using ie As IE = New IE(currentURL)
 
            'Set the focus on the OKButton
            ie.Button(Find.ById(New Regex(OKButton))).Focus()
            ie.ActiveElement.Click()
 
            aStringValue = ie.TextField(Find.ById(
New Regex(MessageTextBox))).Text.Trim
            Assert.IsTrue(aStringValue.Equals("OK"),
"The ID of the OKButton should be in the textbox now.")
 
        End Using
    End Sub

The command ie.ActiveElement.Click() works because the active element happens to be a button with a click event.  Generally, you will find it more stable to find buttons that you wish to click specifically, rather than relying on ActiveElement. 

 

The ActiveElement method comes in most handy, when you need to determine what has the focus when you have not already set something yourself, such as on page load.  There may be a requirement that a specific input field or button have focus when the page loads and you can check for it with ActiveElement.  The following example shows a test to determine if the message textbox has the initial focus:

 

    ''' <summary>
    ''' Ensure that the MessageTextBox is set to ActiveElement
    ''' </summary>
    ''' <remarks>
    ''' currentURL is a Private Const =
    ''' "http://localhost:3587/Default.aspx"
    ''' MessageTextBox is a Private Const = "MessageTextBox"
    ''' </remarks>
    <Test()> _
     Public Sub TestActiveElementInitialFocus()
        Dim aStringValue As String = ""
 
        Using ie As IE = New IE(currentURL)
 
            If Not ie.ActiveElement.Id Is Nothing Then
                aStringValue = ie.ActiveElement.Id.ToString
            End If
 
            Assert.IsTrue(aStringValue.Contains(MessageTextBox),
"The initial focus should have been the textbox but instead
was [" & aStringValue & "].")
 
        End Using
    End Sub

Contents: Table of Contents Previous Page: Testing Web Pages - Things To Consider Next Page: WatiN API Reference - AddDialogHandler

Published Wednesday, May 07, 2008 4:04 PM by ddodgen

Comments

No Comments