QTP基础代码收集《三》

来源:互联网 发布:数量关系知乎 编辑:程序博客网 时间:2024/05/22 06:08
5、WebTable功能函数集合:
' ************************************************** FunctionLibrary ********************************
    
  ' Registering bothfunctions
  RegisterUserFunc "WebTable","ObjectsByMicClass", "ObjectsByMicClass"
  RegisterUserFunc "WebTable","ItemByKeyColumn", "ItemByKeyColumn"
    
  ' Function:ObjectsByMicClass
  ' Descrīption: Returns acollection of objects all the objects in a
  ' WebTable that have thespecified MicClass
  ' Return Value: A Collection ofObjects
  ' Arguments:
  ' Obj - Test Object(WebTable)
  ' micClass - The micClass ofthe objects to retrieve
  '-----------------------------------------------------------------------------------------------------------
  Function ObjectsByMicClass(Obj,micClass)
  Set Table = Obj
  ' Create a collection object tohold the items
  Set ōbjCollection =CreateObject("scrīpting.Dictionary")
  ' Go over all the cells in thetable, and look for objects with the specified micClass
  For row=1 toTable.RowCount
  ColumnCount=Table.ColumnCount(row)
  For col=1 to ColumnCount
  For ItemIndex=0 toTable.ChildItemCount(row, col, micClass)-1
  Set childItem=Nothing
  Set childItem =Table.ChildItem(row, col, micClass, ItemIndex)
  If Not childItem is NothingThen
  ' If the cell contains amicClass object, add it to the collection
  ItemKey = objCollection.Count +1
  objCollection.Add ItemKey,childItem
  End if
  Next
  Next
  Next
  Set ōbjectsbyMicClass =objCollection
  End Function
  
  
  ' Function:ItemByKeyColumn
  ' Descrīption: Returns an itemfrom a column, based on the value of a
  ' key column
  ' Return Value: Object
  ' Arguments:
  ' Obj - Test Object(WebTable)
  ' KeyColumnIndex - Index of theKeyColumn
  ' KeyColumnValue - Value tosearch for in the key column
  ' KeyItemIndex - Index of thevalue in the key column (if there is
                         morethan one). If 0, the first item will be used.
  ' TargetColumnIndex - Columnfrom which to retrieve the target item
  ' micClass - The micClass ofthe target item
  ' TargetItemIndex - Index ofthe target item to retrieve (if there is
                           more than one). If 0, the first item will be used.
  '------------------------------------------------------------------------------------------------------------------------------------
  Function ItemByKeyColumn(Obj,KeyColumnIndex, KeyColumnValue, KeyItemIndex, TargetColumnIndex,micClass, TargetItemIndex)
  Table = Obj
  rowCount = Table.RowCount
  
  ' if TargetItemIndex was notspecified, use 1 as deafult
  If TargetItemIndex< 1 Then
  TargetItemIndex = 1
  End If
  ' if KeyColumnIndex was notspecified, use 1 as default
  If KeyItemIndex< 1 Then
  KeyItemIndex = 1
  End If
  
  ' look for KeyColumnValue inthe key column to determine which
  ' row to retrieve the targeitem from
  Row = 0
  foundIndex = 0
  While Row <=RowCount And foundIndex < KeyItemIndex
  Row = Row + 1
  CellData =Table.GetCellData(Row, KeyColumnIndex)
  If CellData = KeyColumnValueThen
  foundIndex = foundIndex +1
  End If
  Wend
  If foundIndex <KeyItemIndex Then
  Exit Function
  End If
  
  ' Now that we know the row,retrieve the item (according to its micClass)
  ' from the target column.
  ChildItemsCount =Table.ChildItemCount(Row, TargetColumnIndex, micClass)
  If ChildItemsCount> =1 And ChildItemsCount >=TargetItemIndex Then
  Set GetItemByKeyColumn =Table.ChildItem(Row, TargetColumnIndex, micClass,TargetItemIndex-1)
  End If
  End Function
  
  
  '************************************ Examples that use thesefunctions*******************************************************
  
  
  ' Using the ItemByKeyColumnFunction
  Set &#333;bj = Browser("Table withobjects").Page("Itenerary: Mercury Tours").WebTable("Acapulco toZurich").ItemByKeyColumn(1,"FLIGHT",2,3,"WebElement",1)
  msgboxobj.GetROProperty("innerhtml")
  
  ' Using the ObjectsByMicClassfunction
  Set collection =Browser("Browser").Page("Page").WebTable("Table").ObjectsByMicClass("WebCheckBox")
  For i=1 tocollection.count
  Ifcollection(i).GetROProperty("checked") Then
  collection(i).Set "OFF"
  Else
  collection(i).Set "ON"
  End If
  Next
0 0