转贴--如何使用qtp检查网页中显示的文字颜色为指定的颜色
#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~"cbgo0Set Elements = Browser("Register: Mercury Tours").Object.Document.all.tags("TD")51Testing软件测试网2^8n k'z+RsH
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;{(~hT C0 Reporter.ReportEvent 0, Element.InnerText, "Text color is red!"
'M9OE F6sl,BN6CHg0 End If51Testing软件测试网zIPuP
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
所以大家要根据代码的实际情况来编程验证,这里只提供了一种验证的方法。
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