现在的位置: 首页 > 综合 > 正文

QTP中DOM的递归调用取html页面值

2012年10月24日 ⁄ 综合 ⁄ 共 1616字 ⁄ 字号 评论关闭

参考:

http://www.51testing.com/?uid-173503-action-viewspace-itemid-210466

 

 

The HTML DOM defines a standard way for accessing and manipulating HTML documents.
HTML DOM定义了一种访问和控制HTML文档的标准方法。

All nodes can be accessed through the tree. Their contents can be modified or deleted, and new elements can be created.
所有节点能通过树访问。节点可以被修改或删除,也可以建立新的元素。

这是w3上的描述。我们在测试时遇到过这样的问题,一类信息在一个div下,而随着页面信息的变化,当初用object repository抓的table表对象属性发生变化而报错,信息没有取出来。比如电话号码,001-0033-3242,默认抓的是3个表格对象,页面表格index属性变化时出错。但发现它们在某一个特定的div下,比如名字叫divFamilyTelphone,开发的同事一般都这么设计页面信息显示的。

当时想到用程序通过div id取,想到树形结构,于是做出下面的函数,属于递归调用,拿出来分享一下。
当然了,根据的还是w3上dom的定义。

The nodeType property returns the type of node. nodeType is read only.
节点类型属性返回节点的类型,此属性为只读。
The most important node types are:
最重要的节点类型为:
Element type     NodeType
Element     1
Attribute     2
Text         3
Comment     8
Document     9

这样的话,在qtp里做了如下函数:
1,主调用,返回结果,由qtp的封装方法取得节点做参数调用。
Function getMessageByDivId(strDivId)
    'If the div not found
    On error resume next
        Set nodea = Browser("***").Page("***").Object.getElementById(strDivId)
    If Err.number <> 0 Then getMessageByDivId = "" : Exit Function

    Dim strReuslt
    IterateNode nodea, strReuslt
    getMessageByDivId = strReuslt
End Function
2.递归,根据dom节点属性的定义。文本节点返回。
Function IterateNode(strNodeName, byref strReturn)
    'If is a text node, get value.
    If strNodeName.nodeType = 3 Then
        atext = strNodeName.nodeValue
        strReturn = strReturn & atext
        Exit Function
    End If

    'For iteration.
    Set childNod = strNodeName.childNodes
    If Not IsEmpty(childNod) and childNod.length <> 0 Then
        For i = 0 to childNod.length - 1
            IterateNode childNod(i), strReturn
        Next
    End If
End Function

其实后来想直接qtp的描述一样能实现,在对象库里定义element的属性,然后去运行时的文本值亦可实现。本人没有尝试,大家可以自己试试。

 

抱歉!评论已关闭.