Problem Description: How to retrieve the index of a given element in a JavaTree
Does QuickTest Professional have a method to retrieve the index of a given element in a JavaTree? The JavaTree method has a built-in method to retrieve the string from an index, but it does not appear to have a method to perform the reverse.
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.
This function was written against the JTree object in the SwingSet2 demo application that comes with the Sun JDK.
GetIndex(obj, str1)
obj The JTree object
str1 The path to the element the generated index is for
The index of an element in the JTree is dependent on how the tree is displayed. For example, if a branch containing three elements is expanded, the index for the desired element will be three greater than if that branch was collapsed. The GetIndex function collapses the tree to make sure the index returned is the same each time the function is executed.
Public Function GetIndex(obj, str1)
' Collapse the tree
rows = obj.GetROProperty("items count")
For i = rows -1 to 0 step -1
obj.object.collapseRow i
Next
' Expand the specified row
obj.Expand str1
' Get the number of items. Note: this value changes depending on how many branches in the tree are expanded.
' This is why the specified row must be expanded.
rows = obj.GetROProperty("items count")
' Loop through the rows in the expanded tree to find specified item
For i = 0 to rows -1
item1 = obj.GetItem (CStr(i))
match1 = StrComp(str1, item1)
If match1=0 Then
GetIndex = i
Exit Function
End If
Next
The RegisterUserFunc method registers the function for use with a QuickTest test object. For more information, please refer to Problem ID 25943 - How to register a user-defined function for use with objects.作者: volvoo 时间: 2006-7-15 10:47 标题: 谢谢 我也在努力探索和咨询