快捷搜索:  as  test  1111  test aNd 8=8  test++aNd+8=8  as++aNd+8=8  as aNd 8=8

新黄金城线路检测:透析QTP自动化测试框架SAFFRON



1、为什么要应用框架?

框架是一组自动化测试的规范、测试脚本的根基代码,以及测试思惟、常规的聚拢。可用于削减冗余代码、前进代码临盆率、前进代码重用性和可掩护性。例如QTestWare 便是QTP 自动化测试框架中的一类。

2 、SAFFRON 简介

SAFFRON 是针对Web 开拓的一个简单的QTP 测试框架原型,是Adam Gensler 于06 年写的,必要QTP 9.1 版本以上。完备的SAFFRON 脚本代码可到以下地址获取:http://www.itestware.com/ctest/index.php?option=com_content&view=article&id=62:webqtp-saffron&catid=35:testing_is_believing

3 、若何应用SAFFRON?

SAFFRON 框架以外部VBS 文件的形式呈现,是以应用措施对照简单,直接在测试脚本中以资本形式导入即可应用,如图所示:

导入后,可在"Available Keywords" 视图中看到SAFFRON 的所有函数,如图所示:

选中某个函数,拖拽到专家视图的编辑器中,如图所示:

后接一个URL 地址,例如http://www.itestware.com ,即可应用SAFFRON 框架中的BrowseTo 函数导航到指定的URL 地址,如下脚本所示:

'BrowseTo(url)

BrowseTo “http://www.itestware.com

4 、SAFFRON 框架代码剖析

为了深入懂得SAFFRON ,以及框架的应用措施,下面我们将分手先容SAFFRON 中的主要函数,对SAFFRON 代码进行深入剖析。

4.1 导航到指定URL

SAFFRON 应用名为BrowseTo 函数来认真导航到指定的URL ,假如浏览器尚未启动,则先调用函数Launch 来打开浏览器。BrowseTo 函数的定义如下所示:

Public Function BrowseTo (url)

thirdlevel = ""

Report micPass, "Navigate to URL", "Navigating to URL: " & Quote(url)

If initialized Then

Execute GenerateDescription("Browser") & "Navigate " & Quote(url)

Else

Launch "website", url

End If

Reporter.Filter = rfDisableAll

End Function

在脚本中,会判断是否初始化了浏览器,假如有则履行导航动作,导航到指定的URL 。导航动作是履行这行脚原先完成的:

Execute GenerateDescription("Browser") & "Navigate " & Quote(url)

Execute 是一个用于履行指定VBScript 脚本语句的函数,GenerateDescription 函数的定义如下所示:

' Generates a generic description based up on the "l新黄金城线路检测evel" viarable

' levelstr - will be one of the values that is in the level array

' returns - string representative of the object hierarchy

Public Function GenerateDescription (levelstr)

l = IndexOf(level, levelstr)

If l >=0 Then

fdesc = level(0) & "(" & Quote(desc(0)) & ")."

If l >= 1 Then

fdesc = fdesc + level(1) & "(" & Quote(desc(1)) & ")."

If 2 >= l Then

If thirdlevel"" Then

localDesc = GenerateDescription(level(2))

Else

localDesc = GenerateDescription(level(1))

End If

AutoSync()

Select Case objtype

Case"Link"

Execute localDe新黄金城线路检测sc & 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."

End Select

End Function

函数首先判断工具的类型,然后根据工具类型分手处置惩罚,假如是链接工具,则经由过程以下语句组合成可履行的VBScript 语句,然后用Execute 函数来履行:

Execute localDesc & GenerateObjectDescription("Link","innertext:=" & text) & "Click"

假如是按钮工具,则组合成:

Execute localDesc & GenerateObjectDescription("WebButton", "value:=" & text) & "Click"

在这里,调用了GenerateObjectDescription 函数,GenerateObjectDescription 函数的感化与GenerateDescription 函数的感化类似,都是用于返回一个测试工具的描述,不合的是GenerateObjectDescription 函数必要传入测试工具的描述数组,GenerateObjectDescription 函数的定义如下:

' Generates an object description based upon the object, and objectDescription arrays

' obj - name of the object in the object array

' prop - additional property to help uniquely identify the object

' returns - a string representative of the object description

Public Function GenerateObjectDescription (obj, prop)

i = IndexOf(object, obj)

ndesc = ""

If i"" Then

localDesc = GenerateDescription(level(2))

Else

localDesc = GenerateDescription(level(1))

End If

AutoSync()

Select Case objtype

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

Case "CurrentFrame"

If thirdlevel"" Then

Execute estr + "Exist (0)"

End If

If rval Then

Report micPass, objtype & " Verification", "The " & objtype & " " & Quote(text) & " was verified to exist"

Else

Report micFail, objtype & "Verification", "The " & objtype & " " & Quote(text) & " was not found"

End If

If "True" = rval Then

rval = True

Else

rval = False

End If

Verify = rval

End Function

因为判断不合工具的存在必要采纳不合的属性,是以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()

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","dep新黄金城线路检测art")

' 获取航班终点城市

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()

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

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

免责声明:以上内容源自网络,版权归原作者所有,如有侵犯您的原创版权请告知,我们将尽快删除相关内容。

您可能还会对下面的文章感兴趣: