This recipe will show you how to retrieve data from the Environment
object, which is a kind of dictionary that stores key-value pairs. As we will see, unlike a regular dictionary, the Environment
object stores two types of variables:
Built-in
User-defined
Built-in Environment variables give access to two types of information:
Static data such as the
OS
,OSVersion
,LocalHostName
,SystemTempDir
,ProductDir
(where UFT is installed),ProductName
,ProductVer
(UFT version),UserName
(the Windows login name), and several settings such asUpdatingCheckpoints
andUpdatingTODescriptions
. In addition, we can retrieve information about the current test, such asTestName
andTestDir
(the path to the current test(s) from theEnvironment
object).Runtime data such as
TestIteration
,ActionName
, andActionIteration
can be retrieved via theEnvironment
object during runtime. The iteration number can be useful, for instance, when we need to perform an initialization procedure that should be done only once. In this case, the iteration number must be equal to theTestIteration
parameter value.
Create a user-defined Environment variable named MyEnvParam
(see the previous recipe, Storing data in the Environment object).
The following code shows how to retrieve either a built-in or a user-defined variable:
Print Environment("TestDir") 'Prints the Built-in TestDir (path) Environment variable to the console Print Environment("MyEnvParam") 'Prints the User-defined MyEnvParam Environment variable to the console
Similar to the workings of the Scripting.Dictionary
object, by accessing an existing key, the Environment
object returns its paired value.
User-defined Environment variables can be stored in an XML file and dynamically loaded during the runtime session. Refer to the Using global variables (Environment) recipe of Chapter 8, Utility and Reserved Objects.