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软件测试网dO2K)M ?7I-P gP
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:iRs yM-@+N0当时想到用程序通过div id取,想到树形结构,于是做出下面的函数,属于递归调用,拿出来分享一下。51Testing软件测试网-`(L$?r1w R:`
当然了,根据的还是w3上dom的定义。51Testing软件测试网6U#R4X_0I2D"CkU@
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软件测试网5B8TsQg%w~8v
Element type NodeType51Testing软件测试网w~)JMnD9Tz&FBu
Element 151Testing软件测试网O\fZ:W;@sVu
Attribute 2
#gY Nt8c h_;`0Text 351Testing软件测试网$eH,Ml'W4`{u%W
Comment 8
8Vpu ]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\W ctr+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软件测试网6I O)}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 L l%MqUX0 'For iteration.
d},C iYZ&v
IJc*mLl8b3a1z0HTML DOM定义了一种访问和控制HTML文档的标准方法。
4SV9G6@@051Testing软件测试网dO2K)M ?7I-P gP
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:iRs yM-@+N0当时想到用程序通过div id取,想到树形结构,于是做出下面的函数,属于递归调用,拿出来分享一下。51Testing软件测试网-`(L$?r1w R:`
当然了,根据的还是w3上dom的定义。51Testing软件测试网6U#R4X_0I2D"CkU@
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软件测试网5B8TsQg%w~8v
Element type NodeType51Testing软件测试网w~)JMnD9Tz&FBu
Element 151Testing软件测试网O\fZ:W;@sVu
Attribute 2
#gY Nt8c h_;`0Text 351Testing软件测试网$eH,Ml'W4`{u%W
Comment 8
8Vpu ]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\W ctr+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软件测试网6I O)}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 L l%MqUX0 'For iteration.
d},C iYZ&v