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

 这里我们使用DOM(Document Object Model )编程技术来完成这一功能的检查。下面举个例子来说明一下:
    以Mercury自带的定票网站为例,在用户注册页面中,用户名、密码和确认密码三项的文字应为红色显示,下面的代码验证了“密码”为红色。

Set Elements = Browser("Register: Mercury Tours").Object.Document.all.tags("TD")
For each Element in Elements
  If Element.InnerText=“Password:" Then
    strhtml=Element.Innerhtml
    n=instr(strhtml,"color=read")
    If n<>0 Then
      Reporter.ReportEvent 0, Element.InnerText, "Text color is red!"
    End If
  End If
Next

    其中Element.Innerhtml的值为<FONT face="Arial, Helvetica, sans-serif" color=red size=2><B>Password:</B></FONT>, 可以看见color=red就是定义了文字的颜色为红色,所以我们在之后的代码中只要验证Element.Innerhtml中存不存在color=red就可以了。

    不是所有的html代码都是如上编写的,有的时候会把style单独存放在一个css文件中,然后在网页中调用,如<FONT class="redcode">Password:</FONT>,所以我们要根据实际取到的Element.Innerhtml值来编写后面的程序,这里就要验证Element.Innerhtml中存不存在class=redcode,即代码第五行改为 n=instr(strhtml,"class=redcode")。

    所以大家要根据代码的实际情况来编程验证,这里只提供了一种验证的方法。

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

How to know color of an object
' Example:
Dim color
'Grey. Use "#000000" for black
color = "#999"
'Get the DOM object for the text
Set TxtObj = Browser("myBrowser").Page("myPage").WebElement("myWebElement").Object
retVal = FontColorCheckPoint ( TxtObj, color )

Function FontColorCheckPoint ( obj, vExpColour )
Dim vActualcolor, sReportStepName, sErrMsg

' get object's color
vActualcolor = obj.currentStyle.color
sReportStepName = "Custom Checkpoint ""Font Color"""
sMsg = "The expected color " & vExpColour & " was found"
'sErrMsg = "The expected color " & vExpColour & " was NOT found. Found " & vActualcolor & " instead."
sErrMsg = "The expected color " & vExpColour & " for the object " & Chr(34) & obj.innertext & Chr(34) & " was NOT found. Found " & vActualcolor & " instead."
If vActualcolor <> vExpColour Then
Reporter.ReportEvent 1, sReportStepName, sErrMsg
FontColorCheckPoint = False
Else
Reporter.ReportEvent 0, sReportStepName, sMsg
FontColorCheckPoint = True
End If

posted @ 2013-07-05 17:04  芯树  阅读(396)  评论(0编辑  收藏  举报