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 - Checkboxes

The Checkboxes method allows access to all the checkboxes on the web page.  It is primarily used to find a checkbox when you are not able to access it by normal means, such as ID or Name.

 

The following test looks for a checkbox that follows a specific text label, and then tests changing its value.

Example of Usage

 

    ''' <summary>
    ''' Test behavior of checkboxes.
    ''' </summary>
    ''' <remarks>
    ''' currentURL is a Private Const =
    ''' "http://localhost:3587/main.html"
    ''' </remarks>
    <Test()> _
        Public Sub TestCheckboxes()
 
        Using ie As IE = New IE(currentURL)
            Dim aCheckboxValue As Boolean = False
            Dim aCheckboxFound As Boolean = False
            Dim aCheckboxID As String = ""
 
            If ie.CheckBoxes.Length > 0 Then
                For Each CheckBoxFound As CheckBox In ie.CheckBoxes
                    If Not IsNothing(CheckBoxFound.TextBefore) Then
                        If CheckBoxFound.TextBefore.IndexOf("label before") > 0 Then
                            aCheckboxFound = True
                            aCheckboxID = CheckBoxFound.Id
                            Exit For
                        End If
                    End If
                Next
            End If
 
            Assert.IsTrue(aCheckboxFound, "Checkbox preceded by text was not found.")
 
            aCheckboxValue = ie.CheckBox(aCheckboxID).Checked
            ie.CheckBox(Find.ById(New Regex(aCheckboxID))).Checked = Not aCheckboxValue
            Assert.AreNotEqual(aCheckboxValue, ie.CheckBox(Find.ById(New Regex(aCheckboxID))).Checked, "Checkbox did not change value.")
 
            ie.CheckBox(aCheckboxID).Checked = aCheckboxValue
            Assert.AreEqual(aCheckboxValue, ie.CheckBox(Find.ById(New Regex(aCheckboxID))).Checked, "Checkbox did not reset to original value.")
 
        End Using
 
    End Sub

 


 

Once the checkbox is found a few CPU cycles are saved by exiting the FOR loop, instead of just letting it go through to the end of the group.  Then the ID of the found control is used to toggle the checked state of the checkbox.

 

Note:  It was not necessary to determine if aCheckboxID contained a value before using it in the later portion of the test.  This is because the Assert statement acts as a pseudo IF statement.  If a checkbox was not found, and hence the aCheckboxID set, then the test would have stopped at that point, so there is no need to check for NULL status or other such checks.

 

Contents: Table of Contents Previous Page: WatiN API Reference - CheckBox Next Page: WatiN API Reference - ClearCache

 

Published Thursday, June 26, 2008 11:10 AM by ddodgen

Comments

No Comments