Tuesday, August 10, 2010

QTP: Working with QTP Environment Variables

This posts describes how to Working with QTP Environment Variables in vbscript. Note that this is NOT Windows Environment Variables ( see seperate post on that issue ).

Different Types of Environmental Variables
There are 3 kinds Envrionemental Variables
Built-In ( Read only )
User defined internal ( Read and Write )
User defined external ( Saved in XML on Disc or in QC, Read Only )

Add test variable
For the sake of this post I added a environment variable called "jonas"
In QTP: In File > Settings > Environment > Variable type: User-defined add a internal user varible and give it a value. I gave mine 'apa'

Read
Reading the environment variable is now easy

'Save to local variable
UserVar = environment("jonas")
'Now UserVar contains 'apa'
or
'Show messagebox with environment variable
msgbox environment("jonas")
' "apa" is displayed

Write
Writing is even easier ( Can only be done for internal user variables )
' Save new value to environment variable
environment("jonas") = "monkey"
Note, this is not saved after the test script has finished.

List of Built-In Variables
ActionIteration
ActionName
ControllerHostName
GroupName
LocalHostName
OS
OSVersion
ProductDir
ProductName
ProductVer
ResultDir
ScenarioId
SystemTempDir
TestDir
TestIteration
TestName
UpdatingActiveScreen (true/false)
UpdatingCheckpoints (true/false)
UpdatingTODescriptions (true/false)
UserName
VuserId

Printing all Built-In Environment Variables
Here is code for showing a msgbox with all the built-in variables with names and values.
a =Array("ActionIteration", "ActionName", "ControllerHostName", "GroupName", "LocalHostName", "OS", "OSVersion", "ProductDir", "ProductName", "ProductVer", "ResultDir", "ScenarioId", "SystemTempDir", "TestDir", "TestIteration", "TestName", "UpdatingActiveScreen", "UpdatingCheckpoints", "UpdatingTODescriptions", "UserName", "VuserId")
message =""

for each x in a
message = message & x & ":" & environment(x) & Chr(13) & Chr(10)
next

msgbox(message)

No comments: