TIB 自动化测试工作室
http://www.cnblogs.com/testware/
为什么要使用框架?
框架是一组自动化测试的规范、测试脚本的基础代码,以及测试思想、惯例的集合。可用于
减少冗余代码、提高代码生产率、提高代码重用性和可维护性。
SAFFRON 简介
SAFFRON 是针对 WEB 开发的一个简单的 QTP 测试框架原型,是 Adam Gensler 于 06 年写
的,需要 QTP 9.1 版本以上。
如何使用 SAFFRON?
SAFFRON 框架以外部 VBS 文件的形式出现,因此使用方法比较简单,直接在测试脚本中
以资源形式导入即可使用,如图所示:
导入后,可在“Available Keywords”视图中看到 SAFFRON 的所有函数,如图所示:
函数,对 SAFFRON 代码进行深入剖析。
导航到指定 URL
SAFFON 使用名为 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 "level" 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
fdesc = fdesc + level(2) & "(" & Quote(desc(2)) & "," & Quote("name:="
& thirdlevel) & ")."
End If
End If
End If
End If
GenerateDescription = fdesc
End Function
返回测试对象的描述
GenerateDescription 函数用于返回对象的描述性语句,例如,指定 Browser,则返回如下语
句:
"Browser("micclass:=Browser")."
该语句代表了当前浏览器对象,并且后面加了个点号,这是为了方便后接"Navigate "这个浏
览器对象的导航操作,以及指定的 URL 字符串,例如
"http://blog.csdn.net/testing_is_believing"。在 Execute 时,其实执行的 VBScript 语句如下所示:
Browser("micclass:=Browser").Navigate "http://blog.csdn.net/testing_is_believing"
经过 SAFFRON 的框架封装后,则只需要使用如下语句即可达到同样的效果:
BrowseTo "http://blog.csdn.net/testing_is_believing"
启动浏览器
SAFFON 使用名为 BrowseTo 函数来负责导航到指定的 URL,但是如果浏览器未启动,则会
先调用函数 Launch 来打开浏览器。Launch 函数的定义如下所示:
prepares the framework for usage, and configures all internal framework
' variables and structures
' apptype - used to launch different types of applications based
' upon different technologies -- currently there is only web
' val - string that represents what to launch
' returns - always returns true
Public Function Launch (apptype, val)
If "website" = apptype Then
thirdlevel = ""
Report micPass, "Initialize", "Initializing Framework"
level = split(webLevels, leveldelimiter, -1, 1)
desc = split(webLevelsDesc, leveldescdelimiter, -1, 1)
object = split(objects, objectdelimiter, -1, 1)
objectDescription = split(objectsDescription, objectsDescriptiondelimiter, -1, 1)
CloseBrowsers
Set IE = CreateObject("InternetExplorer.Application")
IE.visible = true
IE.Navigate val
While IE.Busy
wait 1
Wend
End If
initialized = true
Launch = true
End Function
可看到脚本中创建了 IE 的 COM 对象,然后设置 IE 的 Visible 属性设置为 Tue,让浏览器可
见,然后调用 IE 对象的 Navigate 方法导航到指定的 URL。除了创建 IE 的 COM 对象外,在
Launch 函数中还进行框架其它方面的初始化。
给指定字符串前后加双引号
在 BrowseTo 函数的定义脚本中,调用了一个名为 Quote 的函数,该函数的定义如下所示:
' generates a string with embedded/surrounding quotes
Public Function Quote (txt)
Quote = chr(34) & txt & chr(34)
End Function
该函数的作用是给指定的字符串前后加上双引号字符,例如下面代码
Msgbox "The message is " & Quote("hello world!")
执行结果显示如图所示。
如果我们不使用这个函数,则需要这样写我们的代码来实现同样的功能:
Msgbox "The message is ""hello world!"""
很明显,这样的写法写出来的代码的可读性和可维护性都差一截。
点击链接
作为一个针对 WEB 应用的脚本框架,除了能启动浏览器导航到指定的页面外,还需要针对
页面的各种元素进行测试操作,例如链接的点击、按钮的点击操作。在 SAFFRON 框架中,
使用 Activate 函数来点击链接、按钮,其函数定义如下所示:
' 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
- 1
- 2
- 3
- 4
前往页