关于Webcheckbox属性赋值

上一篇 / 下一篇  2008-02-18 10:58:00 / 个人分类:QTP

  • 文件版本: V1.0
  • 开发商: 本站原创
  • 文件来源: 本地
  • 界面语言: 简体中文
  • 授权方式: 免费
  • 运行平台: Win9X/Win2000/WinXP
QuickTest Professional identifies WebCheckBox objects using an index value. Thecheckboxthe user needs to select may appear in different positions on the page. Is there a way to select thecheckboxusing its adjacent text?

Solution

Use the custom WebCheckBoxByLabel function (Internet Explorer only)
Note:
This function is not part of QuickTest Professional. It is not guaranteed to work and is not supported by Mercury Customer Support. You are responsible for any and all modifications that may be required.

WebCheckBoxByLabel( parent, arg )
parent        The Page object containing thecheckboxitem you want to select
arg        The text to locate


This function uses the DOM object, so it will only work on Internet Explorer.

Function WebCheckBoxByLabel( parent, arg )

    ' Create a descrīption for the WebCheckBox object
    Set desc = Descrīption.Create()
    desc("type").Value = "checkbox"
    desc("html tag").Value = "INPUT"

    ' Retrieve all WebCheckBox objects on the page
    Set cbcol = parent.ChildObjects(desc)

    cnt = cbcol.count
    ' Loop through the WebCheckBox objects
    For ōbj = 0 to cnt-1
        ' Get the text next to thecheckbox
        text = cbcol(obj).object.getAdjacentText("afterend")
        ' Is this the text we're looking for?
        If arg = Trim(text) Then
            cbcol(obj).Set "ON"
            Exit For
        End If
    Next

End Function

RegisterUserFunc "Page", "WebCheckBoxByLabel", "WebCheckBoxByLabel"

Example:
Browser("Browser").Navigate "http://support.mercury.com"
Browser("Browser").Sync
Browser("Browser").Page("Page").WebCheckBoxByLabel "Remember me"


The above method sets the first instance of the WebCheckBox with the specified text that is found on the page. If you need to specify the instance, the function will need to be modified.

If the WebCheckBox objects are contained within a WebTable object, you can also refer to the following articles:
Document ID 21805 - How to selectcheckboxin the WebTable by the content next to it
Document ID 27498 - How to select acheckboxin an HTML table based on a value in another column
 
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 acheckboxin 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 correspondingcheckbox.
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 thecheckboxes.
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
      '  checkboxlocated 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 thecheckboxin 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 descrīptive 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 thecheckboxobjects in that row.
3. Loop through the collection to set thecheckbox.
Example:
The following code clicks thecheckboxnext 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 thecheckboxes
For Each obj In CheckBoxes
    ' Once found, click thecheckbox...
   If (obj.type="checkbox") Then
      obj.click
   End If
Next

TAG: QTP

 

评分:0

我来说两句

日历

« 2024-04-27  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 11834
  • 日志数: 10
  • 文件数: 7
  • 书签数: 2
  • 建立时间: 2007-01-07
  • 更新时间: 2009-08-19

RSS订阅

Open Toolbar