Chapter 6 QTP Environment Variables
QTP中的环境变量作为一种特殊变量贯穿整个测试集。环境变量的生命周期与整个测试集的执行周期一致。环境变量的主要作用于Action之间参数的传递,场景恢复和调用函数库。这些环境变量和Windows 系统的环境变量有所不同,Windows系统的环境变量支持所有系统中可运行的软件,而QTP的环境变量只支持特定测试集的运行。
环境变量的种类
在QTP中有3种环境变量
l Built-In – QTP内部定义了很多环境变量,例如:测试集的名称,测试集路径,操作系统类型,操作系统版本,主机名称。要查找这些内置的环境变量,打开菜单File → Settings→ Environment(Tab)。如图6-1所示。
l User defined Internal – 此类环境变量是用户在测试集内部定义。在脚本运行过程中,可以修改这些变量的值。
l User defined external – 此类环境变量是在外部环境变量文件中定义的。 在运行过程中为只读状态,不能被修改。我们将在后续的章节中介绍如何引用外部环境变量。
图6-1.QTP内置环境变量
环境变量对象
环境变量对象主要用于对环境变量值的操作,以下列举它的属性和方法:
l ExternalFileName Property – 返回外部引用环境变量的文件名,若没有外部引用文件,则返回空字符串。
l LoadFromFile Method – 从外部文件导入指定的环境变量。
l Value Property – 设置或者读取环境变量的值。只有内部定义的环境变量才可以使用该属性。
范例6-1 如何获取环境变量的参数值
'Get the value to Environment variable TestName sTestName = Environment.Value ("TestName") msgbox sTestName 'Since .Value is the default property of the Environment 'object, we can also use shorthand as follows: sTestName = Environment ("TestName") msgbox sTestName |
范例6-2 如何检查环境变量是否存在
当我们试图获取一个不存在的环境变量的参数值时,QTP会抛出异常。因此,我们利用以下代码来检测环境变量是否存在。
'Function to check if an environment variable exist Public Function IsEnvExist(Byval varName) IsEnvExist = true 'In case of error resume execution of next statement On error resume next Dim envVal envVal = Environment(varName) 'check if error occured If err.number <> 0 Then IsEnvExist = false End If On error goto 0 End Function 'Check if "Invalid " environment variable exist msgbox IsEnvExist("Invalid") |
范例6-3 如何在运行时定义环境变量
赋值给不存在的enviroment变量,使得QTP隐式的创建和初始化一个变量
'Check if "Invalid" variable exists in QTP environment MsgBox IsEnvExist("Invalid") 'this returns False 'Create and set the environment variable ar run-time Environment("Invalid") = "Not invalid any more" 'Check if "Invalid" variable exists in QTP environment MsgBox IsEnvExist("Invalid") 'this now returns True |
范例6-4 如何将环境变量导出到外部XML
有2种方法可以实现。第一种方法,使用字符串拼接的方式,来生成XML代码。第二种方式,使用XML对象来创建。在下面的例子中,我们使用简单的字符串拼接。而XML对象的具体使用将在后续的章节中介绍。
我们想要创建的XML代码如下:
<Environment> <Variable> <Name>FirstName</Name> <Value>Tarun</Value> </Variable> <Variable> <Name>LastName</Name> <Value>Lalwani</Value> </Variable> </Environment> |
用QTP来生成XML代码并写入文件:
'And here are the QTP statements to generate that XML code in an external file: 'Function to get XML tags for a variable with name and value Public Function GetVarXML(varName,varValue) GetVarXML ="<Variable>" & vbCrLf & _ "<Name>" & varName & "</Name>" & vbCrLf & _ "<Value>" & varValue & "</Value>" & vbCrLf & _ "</Variable>" & vbCrLf End Function 'Function to write the file to a string Public Sub WriteStringToFile(fileName, varXML) Dim fso, file Set fso = CreateObject ("Scripting.FileSystemObject") 'Create the file Set file = fso.CreateTextFile (fileName,True) 'write the text file.WriteLine(varXML) file.close Set file = nothing Set fso = nothing End Sub Dim strXML 'Create the envrionment XML start strXML = "<Environment>" 'nodes for variables strXML = strXML & GetVarXML("FirstName","Tarun") strXML = strXML & GetVarXML("LastName","Lalwani") 'Create the envrionment XML end strXML = strXML & "</Environment>" & vbCrLf 'Create the XML file from the string WriteStringToFile "C:\Testing.xml",strXML 'Load the XML to test it Environment.LoadFromFile "C:\Testing.xml" 'Read the variables Msgbox Environment("LastName") & ", " & Environment("FirstName") |
Note:环境变量是区分大小写的,FirstName和Firstname是不同的。
范例6-5 QTP如何使用环境变量来改变记录和设置
在做对Web测试之前,需要使用环境变量来定义浏览器的版本和URL地址。用来控制这2个属性的参数名称分别是BROWSER_ENV和URL_ENV。在QTP的其他8.2版本中,这2个环境变量的名称可能有所不同。 这些变量在测试脚本中是相对独立的。首先在函数库中定义这些变量,然后在运行脚本之前产生关联。
将以下2行代码保存为:“C:\LaunchURL.vbs”
然后通过Test – Settings…….-Resources(TAB)来设置关联。
'Change the browser to launch as IE Environment("BROWSER_ENV") = "IE" 'Change the URL to be navigated Environment("URL_ENV") = "http://www.google.com" |
图6-2.关联外部VBS函数库
图6-3.录制和运行设置
当我们执行脚本的时候,QTP并不会打开Baidu,而是打开了Google。
范例6-6 如何申明或者注销一个环境变量
Qtp并没有提供删除环境变量的方法,但是我们可以通过Nothing或者Empty来控制它是否有变量值。
'Delcare the firstname
Environment("FirstName") = "Tarun"
'Check if Firstname exiests
'Display True
msgbox IsEnvExist("FirstName")
'Change it to nothing
Environment("FirstName") = nothing
'Check if Firstname exiests
'Display False
msgbox IsEnvExist("FirstName")
Note:当环境变量不存在时,错误便会产生。当环境变量被设置为nothing时,错误并不相同。而我们使用IsEnvExist方法,只是检查访问环境变量时是否有错误产生,所以可以使用IsEnvExist方法
范例6-7 如何用环境变量来传递对象
当我们试图用SET关键字,直接把一个对象赋值给环境变量时,会产生类型不匹配的错误。有2种方法可以避免这个错误。方法1,把对象转换为字符串,然后赋值给环境变量。方法2,不用SET关键字。
方法1
'String form of the object we need Environment("BrowserObj") = "Browser(""Creationtime:= 0"")" Dim objBrowser 'Execute the code set objBrowser = Browser(""Browser")" Execute "Set objBrowser = " & Environment("BrowserObj") objBrowser.close |
方法2
'String form of object we need Environment("BrowserObj") = Browser("Creationtime:= 0") Dim objBrowser Set objBrowser = Environment("BrowserObj") objBrowser.close |
注意:如果Browser是Action1 OR的一部分那么在acton2中使用如上的代码可能不行,因为action1和action的对象库是不同的,所以如上的代码必须在action范围内执行
范例6-8 如何用环境变量来传递数组
当使用环境变量来传递一个修改过下限的数组时,会抛出“This array is fixed or temporarily locked”的异常。要传递一个数组,首先需要先将数组指向一个普通变量,然后再指定给环境变量。
Dim fixedArr(3) For i = LBound(fixedArr,1) to UBound(fixedArr,1) fixedArr(i) = Cstr(i) Next Dim dynArr 'Assign it to another variable to make it dynamic dynArr = fixedArr 'Assign the dynamic array to the environment variable Environment.Value("PassArray") = dynArr MsgBox Environment.Value("PassArray")(2) 'Get the array from the environment variable retArr = Environment.Value("PassArray") Msgbox retArr(1) |
范例6-9 如何从外部XML文件中读取环境变量值
'Variables in Env_Dev.xml are now available Environment.LoadFromFile "c:\Env_Dev.xml " 'Variables in Env_test.xml are now available Environment.LoadFromFile "c:\Env_test.xml " 'Variables in Env_Dev.xml are not available |
上述代码说明了一个QTP的缺陷,当多次导入不同的环境变量文件时,先导入的文件会失效。在后续的章节”working with XML”里,将给出解决这个问题的方法。