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

上一篇 / 下一篇  2010-09-26 16:09:52 / 个人分类:QTP

The HTML DOM defines a standard way for accessing and manipulating HTML documents.
IJc*mLl8b3a1z0HTML DOM定义了一种访问和控制HTML文档的标准方法。
4SV9G6@ @051Testing软件测试网d O2K)M ?7I-PgP
All nodes can be accessed through the tree. Their contents can be modified or deleted, and new elements can be created.
f\9M9FX0所有节点能通过树访问。节点可以被修改或删除,也可以建立新的元素。
gyJQt D:P051Testing软件测试网IV n5o-a5?

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

sW!~)T:iR syM-@+N0当时想到用程序通过div id取,想到树形结构,于是做出下面的函数,属于递归调用,拿出来分享一下。51Testing软件测试网-`(L$?r1w R:`
当然了,根据的还是w3上dom的定义。51Testing软件测试网6U#R4X _0I2D"C kU@

J%I1{/G%? ]0The nodeType property returns the type of node. nodeType is read only.51Testing软件测试网\?(n#a1`,C_
节点类型属性返回节点的类型,此属性为只读。
(d$G!M]"GKr;Y0The most important node types are:
#Ms[ ne+z0最重要的节点类型为:51Testing软件测试网5B8TsQ g%w~8v
Element type     NodeType51Testing软件测试网w~)J MnD9Tz&FBu
Element     151Testing软件测试网O\fZ:W;@s Vu
Attribute     2
#gY Nt8ch_;`0Text         351Testing软件测试网$eH,Ml'W4`{u%W
Comment     8
8V pu ]Rxkr-i0Document     9
%~ xF(no;s9_ i051Testing软件测试网:l-Zj#tX6X
这样的话,在qtp里做了如下函数:
5CY~ })TL|T01,主调用,返回结果,由qtp的封装方法取得节点做参数调用。51Testing软件测试网%x^6s8wwsQA%lu
Function getMessageByDivId(strDivId)
+kq,p,z"W-e,qho0B;D5_-N0    'If the div not found
0S;YIz{4b0    On error resume next
;[i#hg^)Q|[0        Set nodea = Browser("***").Page("***").Object.getElementById(strDivId)51Testing软件测试网X6K8f;f~
    If Err.number <> 0 Then getMessageByDivId = "" : Exit Function51Testing软件测试网A \h$}1\Wctr+f

Ff/?~$j0    Dim strReuslt51Testing软件测试网 lo A3p$Mz Y
    IterateNode nodea, strReuslt
X[p]tj*z(N0    getMessageByDivId = strReuslt51Testing软件测试网Y"]WZx1YZ"Te
End Function51Testing软件测试网z+q-U%kS!a
2.递归,根据dom节点属性的定义。文本节点返回。
;K{pPk2N4De0Function IterateNode(strNodeName, byref strReturn)
Jbjn)A&^(L1{0    'If is a text node, get value.51Testing软件测试网6IO)}5H'u9Q
    If strNodeName.nodeType = 3 Then51Testing软件测试网t2Q1A ^5^Ta
        atext = strNodeName.nodeValue51Testing软件测试网`r9^O#L
        strReturn = strReturn & atext
x0{;xF$zGr0        Exit Function
z$R,e4n,Y&NpyU0    End If
;[:dPj{c |0
2S l Ll%MqUX0    'For iteration.
d},CiYZ&v|0    Set childNod = strNodeName.childNodes
S7K ch5v"f)@1J0    If Not IsEmpty(childNod) and childNod.length <> 0 Then51Testing软件测试网C N3l$d;o#K(Tc$`[%Y
        For i = 0 to childNod.length - 1
se2F3hCq vk/sO0            IterateNode childNod(i), strReturn
Hft-`M0        Next51Testing软件测试网:dca;u4qV
    End If
gw0y9z9r^7Wm0End Function51Testing软件测试网`@)oq&Ra/ym^wG
51Testing软件测试网1vt }7J:{lP:X

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

TAG:

 

评分:0

我来说两句

Open Toolbar