但是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
脚本的测试结果如图所示:
文章来源于领测软件测试网 https://www.ltesting.net/