本站内容均来自网络转贴内容,如涉及版权问题,请及时联系我,我会及时删除。。。

转贴--如何使用qtp检查网页中显示的文字颜色为指定的颜色

上一篇 / 下一篇  2007-02-08 11:05:51

#X8L,\Mh,X[ Op0我的第一片帖子就是从SAI的网友上抄袭过来的。

/T T D-R`8y0 51Testing软件测试网_~*f@*O4IX\

以下是17testing.com 的skisky 所给出的答案51Testing软件测试网9{%F|n#K.M^:s4O m3MQ.b D

UHO.~r1ZA Jf(aP0这里我们使用DOM(Document Object Model )编程技术来完成这一功能的检查。下面举个例子来说明一下:
x*jXHa$M0以Mercury自带的定票网站为例,在用户注册页面中,用户名、密码和确认密码三项的文字应为红色显示,下面的代码验证了“密码”为红色。51Testing软件测试网 O&H0@ T [E#Q:s,{

5q~"c bgo0Set Elements = Browser("Register: Mercury Tours").Object.Document.all.tags("TD")51Testing软件测试网2^8nk'z+Rs H
For each Element in Elements
u6qJ.k#fU:Zd0  If Element.InnerText=“Password:" Then51Testing软件测试网)VL duNv%o*a4_
    strhtml=Element.Innerhtml51Testing软件测试网UbZ#AqZJ W
    n=instr(strhtml,"color=read")51Testing软件测试网"YIa+sv&B'g
    If n<>0 Then
F*C;{(~hTC0      Reporter.ReportEvent 0, Element.InnerText, "Text color is red!"
'M9OEF6sl,BN6CHg0    End If51Testing软件测试网zI PuP
  End If51Testing软件测试网_9W3D P H8@M ^PH
Next51Testing软件测试网&B#A3G'cd\(Vc-L!A#BX
51Testing软件测试网dg'bR0gkDORp
其中Element.Innerhtml的值为<FONT face="Arial, Helvetica, sans-serif" color=red size=2><B>Password:</B></FONT>, 可以看见color=red就是定义了文字的颜色为红色,所以我们在之后的代码中只要验证Element.Innerhtml中存不存在color=red就可以了。51Testing软件测试网$bQ-]!i)F/v"Z
51Testing软件测试网.y'WL!h:O"Y}9gQ
不是所有的html代码都是如上编写的,有的时候会把style单独存放在一个css文件中,然后在网页中调用,如<FONT class=redcode>Password:</FONT>,所以我们要根据实际取到的Element.Innerhtml值来编写后面的程序,这里就要验证Element.Innerhtml中存不存在class=redcode,即代码第五行改为 n=instr(strhtml,"class=redcode")。
/Fhu^R!S @ G"i5[(Y?051Testing软件测试网tt\!r6p!y"Q
所以大家要根据代码的实际情况来编程验证,这里只提供了一种验证的方法。

YE,Slj0 51Testing软件测试网(N4Ys/T Z3A;zD;[

 51Testing软件测试网o5y1H/H(c

51Testing软件测试网];LP!}+J)k

我也找到另外一种,紧供参考:

N C pm*]#S w0

$y;m;Wt6V.vo;A0How to know color of an object 51Testing软件测试网} P5Of tQ'v+x?
' Example:
@;a0EFWP,s.h0Dim color
r~7~{_C#O$P0'Grey. Use "#000000" for black 51Testing软件测试网.~-Yv _:lt
color = "#999"
j(b|tg1M"q:rY?'g\0'Get the DOM object for the text
yoC/Io-~hxT!P[0Set TxtObj = Browser("myBrowser").Page("myPage").WebElement("myWebElement").Object
1W(`-\s r#N3r?p0retVal = FontColorCheckPoint ( TxtObj, color )
&g1o#Cc c.J"H0
zgS~ jlB0Function FontColorCheckPoint ( obj, vExpColour ) 51Testing软件测试网e!X2k#a:AR
Dim vActualcolor, sReportStepName, sErrMsg
h,U}s"fR;l?,o0
&O*I6FPK J8c2v0' get object's color 51Testing软件测试网b4]zj6[ rT
vActualcolor = obj.currentStyle.color
~.~ @L4D5_uo6M0C0sReportStepName = "Custom Checkpoint ""Font Color""" 51Testing软件测试网u8nWuk2b
sMsg = "The expected color " & vExpColour & " was found" 51Testing软件测试网+?j|1gNu4|#Htdk
'sErrMsg = "The expected color " & vExpColour & " was NOT found. Found " & vActualcolor & " instead." 51Testing软件测试网k@^vms|}
sErrMsg = "The expected color " & vExpColour & " for the object " & Chr(34) & obj.innertext & Chr(34) & " was NOT found. Found " & vActualcolor & " instead." 51Testing软件测试网"WDq{"y}Cc
If vActualcolor <> vExpColour Then
fBC.C8?:\OR6}0Reporter.ReportEvent 1, sReportStepName, sErrMsg
N/\d r;g1l0FontColorCheckPoint = False 51Testing软件测试网 M1G!vn}%x&{.g
Else 51Testing软件测试网8l9ja$}#O szW
Reporter.ReportEvent 0, sReportStepName, sMsg
Z,f8s@ S:S0FontColorCheckPoint = True 51Testing软件测试网+p [2r]+j6s YX+NB
End If 51Testing软件测试网2Dy+rQa9R.w8wi

-Y]$Dh.Ib@X0

:Voi/k8NM NI4U051Testing软件测试网/oaa,c;b"Z

O4jz%y(LW-N0 51Testing软件测试网Pn AA!e

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1261772

,Cso*}Uzc|4wC0

TAG:

 

评分:0

我来说两句

Open Toolbar