|
Use the custom user-defined function WebTableSelectCheckbox
To locate a row in a WebTable based on the content of a specified cell, use the GetRowWithCellText method. For inforamation on GetRowWithCellText, refer to Document ID 47222 - How to find a row in a WebTable using the text of a specified cell.
To select a checkbox in a WebTable based on text near it, you can also use the custom WebTableSelectCheckbox function.
Note:
This function is not part of QuickTest Professional. It is not guaranteed to work and is not supported by Mercury Technical Support. You are responsible for any and all modifications that may be required.
The WebTableSelectCheckbox function will search through a Web (HTML) table, locate the item being searched for, and select the item's corresponding checkbox.
WebTableSelectCheckbox (WebTblObj, ItemToLocate, ItemColumn, ChkBoxCol)
WebTblObj The table object which is created before executing the subprocedure.
ItemToLocate The item in the Web table to locate.
ItemColumn The column number that needs to be searched.
ChkBoxCol The column number that contains the checkboxes.
Sub WebTableSelectCheckbox (WebTblObj, ItemToLocate, ItemColumn, ChkBoxCol)
' Total number of rows in html_table
TotalRows = WebTblObj.RowCount
' Remain in loop until all rows are searched or until the item is found.
For row= 3 To TotalRows
' Retrieve the item from the specified column
ValueShown = WebTblObj.GetCellData(row,ItemColumn)
' If item retrieved matches the item searching for, set the
' checkbox located in the specified column, and exit the for loop.
If (ItemToLocate = Trim(ValueShown)) Then
Set ChkBoxObj = WebTblObj.ChildItem(row, ChkBoxCol,"WebCheckBox", 0)
ChkBoxObj.Set "ON"
Exit For
End If
Next
End Sub
Example:
' Corresponding column numbers for each column
colSELECT = 1
colNAME = 2
colEMPID = 3
colJOBTITLE = 4
colDEPARTMENT = 5
' Create an object for the Web table, this object will be passed into the sub procedure.
Set MyObj = Browser("Multiple Oper").Page("Multiple Oper").WebTable("Direct Reports")
' Locate the "323214" object in the colEMPID column and set the checkbox in the first column.
WebTableSelectCheckbox MyObj, "323214", colEMPID, 1
Note:
You can also register the function so it can be used directly with the WebTable object. For information on registering custom functions for use with test objects, refer to Document ID 25943 - How to register a user-defined function for use with objects
Example:
RegisterUserFunc "WebTable", "WebTableSelectCheckbox", "WebTableSelectCheckbox"
Browser("Multiple Oper").Page("Multiple Oper").WebTable("Direct Reports").WebTableSelectCheckbox "323214", colEMPID, 1
Another solution would be to use the .Object method to access the DOM to find and select the object.
Note:
This suggestion is supported only in Internet Explorer.
1. Use the DOM to locate and get a reference to the row with the text you want. You can use descriptive programming to find the TR element with an "innertext" value containing your needed text.
For more information on accessing the DOM and using the .Object property, please refer to the following articles:
Document ID 13590 - How to access all the objects in a webpage (Internet Explorer)
Document ID 21834 - How to access an object's methods and properties
2. Get a collection of the checkbox objects in that row.
3. Loop through the collection to set the checkbox.
Example:
The following code clicks the checkbox next to the word "Rachel" in the table in the attached HTML file:
' find the table row that contains the text we are looking for
Set row = Browser("Friends").Page("Friends").WebTable("Favorite Friends:").WebElement("html tag:=TR","innertext:=.*Rachel.*").Object
' get all the objects in the row that have an INPUT tag
Set CheckBoxes = row.getElementsByTagName("INPUT")
' click on the checkboxes
For Each obj In CheckBoxes
' Once found, click the checkbox...
If (obj.type="checkbox") Then
obj.click
End If
Next |
|