Environment Variable usage
There are two types of environment variables in qtp:
- Built-in Environment Variables
- Customize Internal Environment Variables
- Customize external environment variables
Built-in Environment Variables
Description: It is encapsulated in qtp.
Method: Go to Settings> environment and select the built-in option to query all the built-in environment variables. And the values of these environment variables are all encapsulated by qtp.
Example: msgbox environment. Value ("testname ")
Result: The name of the current test script is obtained.
Customize Internal Environment Variables
Description: it is an environment variable that can be customized within qtp.
Method: directly go to Settings> environment and select the User-Defined option. Click "+" next to it to define the name and value.
Example: msgbox environment. Value ("custom name ")
Result: You can directly obtain the value of the current custom environment variable.
Customize external environment variables
Introduction: Directly Reading environment variables from XML format files
Method: Choose Settings> environment, select load variables and values from external file, and provide the XML file path.
Example: XML file
<Environment> <variable> <Name> URL </Name> <value> http://www.iquicktest.com </value> </variable> </environment>
Msgbox environment. Value ("url ")
Result: The result is printed as a http://www.iquicktest.com.
Note: attribute names in environment variables are case sensitive and must be noted.
Dynamic generation and loading of Environment Variables
Dynamic generation -- use FSO to dynamically write the required variables into an XML file
Here, we mainly use two functions:
The first function, getvariable, allows us to add environment variables and add them through iteration of pointer variables,
The second function, generateenvfile, is to generate an XML file of the Environment Variable Based on the pointer variable.
********** ********************
Getvariable allenvvar, "url", "http://www.baidu.com"
Getvariable allenvvar, "username", "emilyzhang88"
Getvariable allenvvar, "password", "123456"
************* ***************
Generateenvfile "C: \ readxml \ login. xml", allenvvar
'*************************************** *****************************
'Description: gets all environment variable strings.
'Parameters: xmlvar (pointer variable, environment variable character), arname (environment variable name), varvalue (environment variable value ),
'Return value: None
'*************************************** *****************************
Function getvariable (byref xmlvar, byval varname, byval varvalue)
Xmlvar = xmlvar + _
"<Variable>" + vbcrlf + _
"<Name>" + varname + "</Name>" + vbcrlf + _
"<Value>" + varvalue + "</value>" + vbcrlf + _
"</Variable>" + vbcrlf
Msgbox xmlvar
End Function
'*************************************** *****************************
'Description: generate the XML file of the Environment Variable
'Parameters: envpathname (path name), getvar (obtained Environment Variable characters)
'Return value: None
'*************************************** *****************************
Function generateenvfile (envpathname, getvar)
Envstream = _
"<Environment>" + vbcrlf + _
Getvar ++ _
"</Environment>"
Set FSO = Createobject ("scripting. FileSystemObject ")
Set envstreamfile = FSO. createtextfile (envpathname)
Envstreamfile. Write envstream
Set envstreamfile = nothing
Set FSO = nothing
End Function
Call the XML file automatically generated above to dynamically load Environment Variables
Environment.LoadFromFile "C:\readXML\Login.xml"
msgbox Environment.Value("username")
Note:
- There are two types of dynamic loading files: [*. xml] and [*. INI].
- Only one file type can be loaded statically: [*. xml]
First in qtp 5. X and 6. when X is used, qtp uses the INI file as an external environment variable. After 8.0, the XML file is used for storage. Therefore, the static loading method has been blocked, however, you can still use the INI file for dynamic loading, but the file must follow a certain format.
The format is as follows:
[Environment]
Username = xxx12345
Password = 123456
I recently wrote an automated test script for the PayPal system. To declare a global variable, you can use it in another action.
That is, in Action1, after selecting a value in a weblist, add Action2 through insert-> call to copy of action and use the value selected in Action1 in action2.
The procedure is as follows:
1. In Action1, file-> Settings-> environment adds an environment variable "batch" to the User-Defined variables. The Code is as follows:
2. Reference this environment variable in Action2 as follows:
3. Run the script.