由于判断不同对象的存在需要采用不同的属性,因此Verify 函数中对不同的对象类型进行判断、分别处理。例如,对于Link 类型的对象,用innertext 属性,对于WebButton ,则采用value 属性,但是最后都需要组合成一条语句,后接“Exist ”,通过Execute 方法执行这个语句,从而实现对象是否存在的判断。
对于页面对象(Page )的存在性检查有点不一样,采用的是以下脚本:
Case "Page"
Execute "rval = " & GenerateDescription(level(1)) & "Exist (0)"
If rval Then
Execute "title = " & GenerateDescription(level(1)) & "GetROProperty(" & Quote("title") & ")"
If title = text Then
rval = true
Else
rval = false
End If
End If
通过GetROProperty 方法获取当前页面的title 属性,然后与传入的“text ”参数进行比较,如果相等,则认为页面对象是存在的。
在测试脚本中可以这样使用Verify 函数:
' 启动浏览器
Launch "website","http://127.0.0.1:1080 "
' 导航到“http://127.0.0.1:1080/WebTours ”
BrowseTo "http://127.0.0.1:1080/WebTours/ "
If Verify ("Link","administration")= False then
Reporter.ReportEvent micFail," 检查链接"," 链接不存在"
Else
' 点击名为“administration ”的链接
Activate "Link","administration"
End IF
脚本中先用Verify 检查名为“administration ”的链接对象是否存在,如果不存在则提示错误,如果存在则进一步调用Activate 函数点击链接。
4.8 在文本框输入字符串
在SAFFRON 中,可以使用EnterTextIn 函数来给输入框(WebEdit 对象)输入字符串。EnterTextIn 函数的定义如下所示:
' Enters text into an edit field
' objname - name of the control -- use Object Spy if you don't know what it is
' text - the text to enter into the control
Public Function EnterTextIn (objname, text)
localDesc = ""
rval = true
If thirdlevel <> "" Then
localDesc = GenerateDescription(level(2))
Else
localDesc = GenerateDescription(level(1))
End If
AutoSync()
localDesc = localdesc & GenerateObjectDescription("WebEdit", "name:=" & objname)
Execute localDesc & "Set (" & Quote(text) & ")"
Report micPass, "Enter Text", "Text: " & Quote(text) & " was entered into " & Quote(objname)
EnterTextIn = rval
End Function
例如,如果我们要在如图所示的登录界面中输入用户名和密码,则可以使用SAFFRON 的EnterTextIn 函数来实现。
测试脚本可以这样编写:
' 输入用户名
EnterTextIn "username","chennengji"
' 输入密码
EnterTextIn "password","123"
4.9 读取文本框的字符串
在SAFFRON 中,可以使用EnterTextIn 函数来给输入框(WebEdit 对象)输入字符串。对应的有一个名为GetTextFrom 的函数,用于读取输入框和文本列表的字符串,GetTextFrom 的定义如下所示:
' Obtains text from a control
' objtype - is the type of control the get the text from
' objname - is the name of the control -- use Object Spy if you don't know the name
' returns - the text of the control
Public Function GetTextFrom (objtype, objname)
text = ""
localDesc = ""
If thirdlevel <> "" Then
localDesc = GenerateDescription(level(2))
Else
localDesc = GenerateDescription(level(1))
End If
AutoSync()
Select Case objtype
Case "WebEdit"
Execute "text = " & localDesc & GenerateObjectDescription("WebEdit", "name:=" & objname) & "GetROProperty (" & Quote("value") & ")"
Case "WebList"
Execute "text = " & localDesc & GenerateObjectDescription("WebList", "name:=" & objname) & "GetROProperty (" & Quote("value") & ")"
End Select
Report micPass, "Capture Text", "Text: " & Quote(text) & " was captured from the control " & Quote(objname)
GetTextFrom = text
End Function
假设我们需要读取如图所示的界面中的“Departure City ”和“Arrival City ”这两个文本列表(WebList 对象)中的字符串,则可以使用GetTextFrom 函数。
测试脚本可以这样编写:
' 获取航班起始城市
DepartureCity = GetTextFrom( "WebList","depart")
' 获取航班终点城市
ArrivalCity = GetTextFrom( "WebList","arrive")
当然,也可以使用相同的函数来读取文本框(WebEdit 对象)的字符串,例如下面的脚本读取“NO. of Passengers ”对应的文本框中的字符串:
' 获取乘客数量
PassengerNumber = GetTextFrom( "WebEdit","numPassengers")
4.10 选择列表中的一项
在SAFFRON 中,可以使用SelectFromList 函数从下拉框列表(WebList 对象)中选择指定的一项。SelectFromList 的定义如下所示:
' Selects a specific value from a listbox, or combobox
' objname - name of the control -- use Object Spy if you don't know the name property
' text - the item in the combobox to select
Public Function SelectFromList (objname, text)
localDesc = ""
rv = ""
rval = false
If thirdlevel <> "" Then
localDesc = GenerateDescription(level(2))
Else
localDesc = GenerateDescription(level(1))
End If
AutoSync()
localDesc = localdesc & GenerateObjectDescription("WebList", "name:=" & objname)
Execute "cnt = " & localDesc & "GetROProperty(" & Quote("items count") & ")"
For i = 1 to cnt
Execute "rv = " & localDesc & "GetItem (" & i & ")"
If rv = text Then
rval = true
End If
Next
If rval Then
Execute localDesc & "Select " & Quote(text)
End If
If rval Then
Report micPass, "WebList Selection", "The WebList item " & Quote(text) & " was selected."
Else
Report micFail, "WebList Selection", "The WebList item " & Quote(text) & " was NOT found."
End If
SelectFromList = rval
End Function
假设我们需要从如图所示的界面中的“Departure City ”的下拉框中选择其中一项,则可使用SelectFromList 函数来实现。
测试脚本可以这样写:
' 选择航班起始城市为"San Francisco"
SelectFromList "depart","San Francisco"
4.11 关闭浏览器
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
脚本的测试结果如图所示:
文章来源于领测软件测试网 https://www.ltesting.net/