xihong2004 发表于 2008-1-18 11:34:49

如何给WebCheckBox的属性赋值

需求:

如下图中,角色名称是会不断增加的,所以我想先判断“地区管理总部”在第几行,然后在“地区管理总部”前的webcheckbox打勾

[ 本帖最后由 xihong2004 于 2008-1-18 11:38 编辑 ]

xihong2004 发表于 2008-1-18 11:42:02

解决方法:

‘遍历表格上的所有值,与"地区管理总部"对照’,得出"地区管理总部"所在的行号,并把行号转为字符型

var_RowCount = Browser("新意新一代证券综合管理平台E-SIM6.0").Page("新意新一代证券综合管理平台E-SIM6.0_2").Frame("frmbody").WebTable("选择").RowCount()
var_GetCellData = Browser("新意新一代证券综合管理平台E-SIM6.0").Page("新意新一代证券综合管理平台E-SIM6.0_2").Frame("frmbody").WebTable("选择").GetCellData(var_RowCount,3)
While not var_GetCellData="地区管理总部"
        var_RowCount = var_RowCount -1
        var_GetCellData = Browser("新意新一代证券综合管理平台E-SIM6.0").Page("新意新一代证券综合管理平台E-SIM6.0_2").Frame("frmbody").WebTable("选择").GetCellData(var_RowCount , 3)
bmjsmc_index = cstr (var_RowCount)

‘然后把bmjsmc_index,赋值到webcheckbox的属性值中’

[ 本帖最后由 xihong2004 于 2008-1-18 11:44 编辑 ]

tiger_86 发表于 2008-1-18 11:44:28

谢谢了

xihong2004 发表于 2008-1-18 11:46:03

问题:

QTP执行时无法取到bmjsmc_index,不会在相应的webcheckbox中打勾

gy21st 发表于 2008-1-18 12:23:40

QuickTest Professional identifies WebCheckBox objects using an index value. The checkbox the user needs to select may appear in different positions on the page. Is there a way to select the checkbox using 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 the checkbox item 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 description for the WebCheckBox object
    Set desc = Description.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 obj = 0 to cnt-1
      ' Get the text next to the checkbox
      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 select checkbox in the WebTable by the content next to it
Document ID 27498 - How to select a checkbox in an HTML table based on a value in another column

gy21st 发表于 2008-1-18 12:25:47

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

gy21st 发表于 2008-1-18 12:27:26

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

lutingting 发表于 2008-1-18 15:24:48

晕了:L

xihong2004 发表于 2008-1-18 16:05:57

狂晕了

gy21st 发表于 2008-1-18 17:05:32

以上是对webcheckbox进行处理的三种方法。代码很简单,就是英文而已,为什么要晕呢?

frankwangzy1103 发表于 2008-1-18 18:28:47

我的建议:
      用webtable的方法获得地区管理总部的行,这样这个webcheckbox的行列都固定了。我觉得这个方法最简单。

frankwangzy1103 发表于 2008-1-18 18:30:05

具体的webtable的函数自己查一下帮助好了,都有的。

xihong2004 发表于 2008-1-21 10:38:21

原帖由 frankwangzy1103 于 2008-1-18 18:28 发表 http://bbs.51testing.com/images/common/back.gif
我的建议:
      用webtable的方法获得地区管理总部的行,这样这个webcheckbox的行列都固定了。我觉得这个方法最简单。

我已经取到了地区管理总部的行,并把这个行号赋到了webcheckbox的index属性中了,问题是现在QTP不会选中这个webcheckbox,也就是说这个行号赋得有问题
另外说明一下,webcheckbox的行号是不固定的

谢谢大家的帮忙!

flyingkite 发表于 2008-1-22 17:50:07

哇...顶阿...偶也正好遇到这样的问题呢 求救呢哇...

frankwangzy1103 发表于 2008-1-23 11:33:45

原帖由 xihong2004 于 2008-1-21 10:38 发表 http://bbs.51testing.com/images/common/back.gif


我已经取到了地区管理总部的行,并把这个行号赋到了webcheckbox的index属性中了,问题是现在QTP不会选中这个webcheckbox,也就是说这个行号赋得有问题
另外说明一下,webcheckbox的行号是不固定的

谢谢大家的 ...
你确定这个webcheckbox的行号就是它的index吗?我的方法:
set obj = Browser().Page().Webtable().ChildItem (Row, Column, "WebCheckBox", Index)
obj.set "on"
你这样试试行吗?
还有行号不固定没关系啊,可以每次调用的时候get这个行号就行了,只要它的innertext"地区管理总部"存在就能得到的。

flyingkite 发表于 2008-1-23 13:06:30

不是吧...楼主现在的问题是qtp录制不了勾选webcheckbox的问题阿,也就是说对象库里也不存在这个对象阿..
我也想过楼上的办法,但不知道webtable括号里面的值怎么取得阿?到哪里取找哇....

frankwangzy1103 发表于 2008-1-23 13:25:05

原帖由 flyingkite 于 2008-1-23 13:06 发表 http://bbs.51testing.com/images/common/back.gif
不是吧...楼主现在的问题是qtp录制不了勾选webcheckbox的问题阿,也就是说对象库里也不存在这个对象阿..
我也想过楼上的办法,但不知道webtable括号里面的值怎么取得阿?到哪里取找哇....
Webtable很好确定的,你用spy随便点一个webtable里面的对象,他的上面的节点里面就有webtable的,因为我看到他的截图是一个table。

flyingkite 发表于 2008-1-23 18:03:44

我用了你的方法
set obj = Browser().Page().Webtable().ChildItem (Row, Column, "WebCheckBox", Index)
obj.set "on"
报错了
object requirede

xihong2004 发表于 2009-8-5 16:17:47

用描述编程搞定了
webcheckbox(index:=行号).set "ON"

千里 发表于 2012-10-16 21:11:48

研究下webtable()方法
页: [1] 2
查看完整版本: 如何给WebCheckBox的属性赋值