从Element获取父WebTable

Demo:

<table>

  <tr>

    <table border="1">

      <tr>

        <td>Attribute</td>

        <td>Value</td>

        <td>Description</td>

      </tr>

    </table>

    <table border="1">

      <tr>

        <td>left</td>

        <td>Alignment of the element</td>

      </tr>

    </table>

  </tr>

</table>

 

'Get the QTP WebTable from a specified object

Public Function GetWebTableFromElement(ByVal pObject)

Dim oTable, oParent

'Get the parent table, a DOM object

'Remember to include the Funtion GetParentOfElement!

Set oTable = GetParentofElement(pObject.Object, "TABLE")

'Get QTP test object parent of supplied object

Set oParent = pObject.GetTOProperty("parent")

If oTable is Nothing Then

Set GetWebTableFromElement = Nothing

Else

'Return the table using the source index
Set GetWebTableFromElement = oParent.WebTable("source_Index:=" & oTable.sourceIndex)

End If

End Function



'Funtion to get parent node on element

Function GetParentOfElement(ByVal pObject, ByVal pParentTagName)

On Error resume Next

'In case a QTP test object is passed, convert it
'to the DOM Object. This allows using the
'function on both DOM and QTP Test Objects
Set pObject = pObject.Object

Err.Clear

On Error Goto 0

'Search for object's parent node

Set oParent = pObject

'Search for the node with specified Tag Name

Do

Set oParent = oParent.parentNode

'if oParent == Nothing then we didn't find any parent object

'with the specified tag

Loop While Not(oParent is NothingAnd oParent.tagName <> pParentTagName

'Return the object found, else return Nothing if a tag is not found

Set GetParentofElement = oParent

End Function

 

How to use to find QTP table:

Set QTPTable = GetWebTableFromElement(Browser("name:=.*GetRowColumn.*")_

.WebElement("innertext:=Value"))

QTPTable.Highlight

 

How to register it:

RegisterUserFunc "<TargetWebObject>", "GetWebTableFromElement", "GetWebTableFromElement"

posted @ 2013-11-20 16:37  dushuai  阅读(413)  评论(0编辑  收藏  举报