库文件是包含VBScript代码的纯文本文件。用于声明函数,变量,类等。库文件可以以任何扩展名类型存储,最常见的是VBS或TXT。库文件有助于将代码组织成可区分的功能模块。库文件使得不同的QTP脚本共享代码。下面将介绍加载库文件的2中方法。
Associating a Library globally with a Test将库文件和测试脚本全局关联
使用该方法,同一个库文件的实例可以被一个测试脚本里的所有Action共享和访问。操作方法:菜单FileàSettings…àResources(标签),添加可文件,如下图所示:
NOTE1: 库文件是从下往上加载的。如果2个不同的库文件中同时含有一个定义完全一样的函数,那么将使用列表中靠上的那个。
NOTE2: 如果库文件B依赖于库文件A中的定义,那么库文件A要位于列表的底部。
NOTE3: QTP添加库文件时使用的是完整路径。不过建议使用相对路径,如”..\test.vbs”。
Dynamically Loading libraries locally at run-time在运行时动态加载局部库文件
QTP提供ExecuteFile函数在运行时动态加载库文件。使用该方法加载的库文件,其内容之对于使用ExecuteFile函数的Action是可见的。下面是一些了例子:
<代码部分暂略>
NOTE: 如果ActionA和ActionB都通过ExecuteFile函数加载了test.vbs,且ActionA调用ActionB,ActionA和ActionB将使用各自的库文件副本和库文件中所有变量和函数的独立的实例。
Dynamically Loading libraries globally at run-time在运行时动态加载全局库文件
如上所述,直接在一个Action中使用ExecuteFile函数来加载库文件,该库文件只对于该Action是可见的。但是每一个QTP脚本可以有一个对于所有Action都可用的全局库文件集。这样,不是通过直接在每个Action中使用ExecuteFile函数来加载库文件,而是在全局库文件中使用一个函数来实现,那么该库文件就可以被所有Action可用了。
'C:\Liboader.vbs
Public Function ExecuteFileGlobal(ByVal fileName)
ExecuteFile fileName
End Function
将上述代码保存成VBS文件并和测试脚本关联,然后再任何Action中使用ExecuteFileGlobal函数来加载文件。通过ExecuteFileGlobal函数加载的文件,该测试脚本中的所有Action都是可用的。
'在全局范围内加载文件
ExecuteFileGlobal "C:\Test.vbs"
如果一个Action要循环执行多次,调用ExecuteFileGlobal函数将重复加载库文件多次,这样每次重新加载库文件时,将破坏该库文件中全局变量的当前状态。
该问题可以通过下面的代码来解决。在ExecuteFileGlobal中加一个重载标记,当重载标记为false时,库文件不会被重复加载。创建一个所有被加载文件的全局数据字典。文件路径用于判断该文件是否已被加载。
' C:\LoadLibrary.vbs
Dim loadedFiles
Set loadedFiles=CreateObject("Scripting.Dictionary")
loadedFiles.CompareMode=vbTestCompare '执行文本比较, 字母不区分大小写. vbBinaryCompare是执行二进制比较, 字母区分大小写的.
'使用ExecuteFileGlobal在运行时加载文件
‘输入参数 strFile:要加载的文件的完全文件名(即绝对路径)
‘reload: 用于标记当文件已被加载过时,是否要重复加载
Public function ExecuteFileGlobal (ByVal strFile, ByVal reLoad )' ByVal是传值, By Rel是传地址. 缺省为By Rel
‘如果reLoad为false, 就检查文件是否已被加载过
If reload=false and loadedFiles.exists(strFile) Then
‘如果已被加载过,就退出加载函数
ExecuteFileGlobal=false
Exit function
End If
‘加载文件
executefile strFile
loadedFiles(strFile)=true ‘将文件加到数据字典中,标记该文件已被加载过
ExecuteFileGlobal=true
End function
为了更好的理解上述代码,我们来举个例子。首先创建一个将在运行时加载的库文件。
‘D:\testA.vbs
dim x
x=2
下面的代码演示了ExecuteFileGlobal的使用方法:
‘在全局范围内加载testA.vbs
ExecuteFileGlobal "D:\testA.vbs", false
msgbox x
x=x+2
‘在全局范围内加载testA.vb,已加载时不重复加载
ExecuteFileGlobal "D:\testA.vbs", false
msgbox x
‘在全局范围内加载testA.vb
ExecuteFileGlobal "D:\testA.vbs", true
msgbox x
问题9-1: 如何在运行时定义全局变量
有时需要在运行时在2个或多个action 之间共享变量的值。同样使用加载全局库文件的方式,来创建全局变量:
<代码部分暂略>
Understanding Executing Scope理解执行范围
理解和区分测试脚本的全局和局部范围很重要。
l 全局范围是QTP加载所有所有源文件和场景恢复库文件的地方。它可以被脚本中所有的Action访问。
l 局部范围是action范围,在action里定义的任何代码,不能被该action以外的代码访问。
图9-2描述了含有2个action的QTP 的执行范围。这就是QTP运行的方式。
l 当脚本开始运行时,QTP创建了一个全局范围。
l QTP首先添加相关场景恢复的库文件。使场景恢复从上往下被关联。
l 当加载了所有场景恢复的文件后,QTP然后加载在TestàSettings…àResource(标签)中关联的文件,这些文件是从下往上关联的。
l 然后QTP按照在测试流中定义的顺序调用每个action。QTP为每个action创建一个局部范围,该范围是私有的,只有该action可见。该局部范围在action每次循环过程中创建和销毁。
l 在action1中定义的函数不能被其他action和全局范围访问。
NOTE: 如果全局范围内有多个库文件包含同名的函数,将调用最新被加载的那个库文件里的函数 。
Applicability of Option Explicit(看不懂)
Executing code in Local scope from within Global Scope
前面我们学习了如何在局部范围加载局部库文件(“在运行时动态加载局部库文件”), 及如何在局部范围内加载全局库文件(“在运行时动态加载全局库文件”)。
有时,由于脚本维护的需要,我们可能会在action的开始或末尾添加代码。这种维护可能需要应用在多个脚本上。QTP只允许一次打开一个单独的脚本,这样要编辑一次编辑多个脚本就很费时。为了避免这中情况,可以在action中调用下面2个特殊的函数。
《代码略》-代码看不懂