透析QTP自动化测试框架SAFFRON(5)

发表于:2014-11-25来源:uml.org.cn作者:陈能技点击数: 标签:
Web页面测试的最后一个步骤一般都是关闭浏览器,在SAFFRON中,也把这个过程封装成了一个名为CloseBrowsers的函数,该函数的定义如下: close all opened browse

Web页面测试的最后一个步骤一般都是关闭浏览器,在SAFFRON中,也把这个过程封装成了一个名为“CloseBrowsers”的函数,该函数的定义如下:
' close all opened browsers
Public Function CloseBrowsers
If Browser("micclass:=Browser").Exist (0) Then
Browser("micclass:=Browser").Close
End If
While Browser("micclass:=Browser", "index:=1").Exist (0)
Browser("index:=1").Close
Wend
If Browser("micclass:=Browser").Exist (0) Then
Browser("micclass:=Browser").Close
End If
End Function

CloseBrowsers函数会把当前所有打开的浏览器都关闭,脚本中采用描述性编程的方式获取所有对象类型为“Browser”的测试对象,然后循环逐个关闭所有这种类型的测试对象。

5、对SAFFRON框架进行扩展

SAFFRON是一个基本的框架,它封装了浏览器的相关测试操作、封装了一些基本对象的测试操作,例如Link、 WebButton、WebEdit、WebList等控件,可用于基本的WEB页面的测试,并且简化了测试脚本的编写,可以让代码的可读性和可维护性得到增强。

但是SAFFRON仅仅是一个基础框架,我们还需要进一步地对其扩展才能应用到实际的WEB自动化测试项目中去,例如扩展对更多的控件的支持。下面是一个对Activate函数扩展Image对象的点击操作的过程:

(1)首先打开SAFFRON框架的VBS文件,找到开头的变量定义处,添加Image对象,让框架可以识别和支持Image对象:
' 扩展对Image对象的支持
objects = "Link|WebButton|WebList|WebEdit|Image"
objectsDescription = "micclass:=Link|micclass:=WebButton|micclass:=WebList|micclass:=WebEdit|micclass:=Image"

(2)修改Activate方法,添加对Image对象的Click操作的支持,脚本修改成如下所示:
' Activates an object based upon its object type
' objtype - the type of object should be limited to values in the object array
' text - identifying text for the control - for a link, it's the text of the link
Public Function Activate (objtype, text)
localDesc = ""
If thirdlevel <> "" Then
localDesc = GenerateDescription(level(2))
Else
localDesc = GenerateDescription(level(1))
End If

AutoSync()

Select Case objtype
Case "Link"
Execute localDesc & GenerateObjectDescription("Link","innertext:=" & text) & "Click"
Report micPass, "Link Activation", "The Link " & Quote(text) & " was clicked."
Case "WebButton"
Execute localDesc & GenerateObjectDescription("WebButton", "value:=" & text) & "Click"
Report micPass, "WebButton Activation", "The WebButton " & Quote(text) & " was clicked."
' 扩展对Image类型的按钮的支持
Case "Image"
Execute localDesc & GenerateObjectDescription("Image", "alt:=" & text) & "Click"
Report micPass, "ImageButton Activation", "The ImageButton " & Quote(text) & " was clicked."
End Select
End Function

(3)调试和测试修改后的脚本,例如采用下面的脚本来看对Activate函数的扩展是否生效:
' 启动浏览器
Launch "website","http://127.0.0.1:1080"
' 导航到“http://127.0.0.1:1080/WebTours”
BrowseTo "http://127.0.0.1:1080/WebTours/"

' 输入用户名
EnterTextIn "username","chennengji"
' 输入密码
EnterTextIn "password","123"

' 单击Login按钮
Activate "Image","Login"

' 单击"Flights"按钮
Browser("Web Tours").Page("Web Tours").Frame("navbar").Image("Search Flights Button").Click

' 获取航班起始城市
DepartureCity = GetTextFrom( "WebList","depart")
' 获取航班终点城市
ArrivalCity = GetTextFrom( "WebList","arrive")
' 获取乘客数量
PassengerNumber = GetTextFrom( "WebEdit","numPassengers")

' 选择航班起始城市为"San Francisco"
SelectFromList "depart","San Francisco"

If Verify ("Link","administration")= False then
Reporter.ReportEvent micFail,"检查链接","链接不存在"
Else
' 点击名为“administration”的链接
Activate "Link","administration"
End IF

脚本的测试结果如图所示:

 

原文转自:http://www.uml.org.cn/Test/200810108.asp