How to Use Environment Variables in UFT

Before understanding the concept of environment variables in UFT, let’s first try to understand what actually environment variables are.

Environment variables are variables that describe the environment in which programs run. They are used by all kinds of programs to answer questions like: What is the name of the computer? What is the name of the Operating System? What is the name of the current user account running on the machine? What is my current working directory? Where is Windows installed? Where are the temporary files stored on this computer? and many more.

In Windows, environment variables have a name and its value. For example, the variable windir (“Windows directory”) may have the value “C:\Windows” or another path where Windows has been installed.
You can view your machine environment variables by accessing System Properties and then click on the Environment Variables button.

Environment-Variables

Environment Variable window will appear.

Environment Variable 2

Environment Variables in UFT/QTP

 
Environment variables in UFT/QTP are very special types of variables that are accessible across all actions(either local or external actions), function libraries, and recovery scenarios in a test. UFT environment variables are available only for test script during runtime and can’t be accessed by any other program. Environment variables act as global variables in a test.
 
The value of the environment variable remains the same throughout the run session, regardless of the number of iterations, unless you change the value of the variable programmatically. These variables are generally used when you want a variable to be shared across various reusable actions and functions.
 

Types of environment variables in UFT/QTP

The following are the types of environment variables in UFT:
  • Built-in environment variables
  • User-Defined Internal Environment Variables
  • User-Defined External Environment Variables

Built-in environment variables

These are variables that represent information about the test or scripted component and the computer on which they are run, such as the Test path, Operating system, TestName, and ActionName, etc. These variables are accessible from all tests and scripted components and are designated as read-only.
 
To access environment variables go to File > Settings> Environment Tab

 

Enviroment-Variable-UFT

If you want to know the version of the operating system where your test is running. You can simply write Environment(“OSVersion”) and use a message box to display its value.
Example: Msgbox Environment(“OSVersion”)
 

environment Variable output

User-Defined Internal Environment Variables

User-defined internal environment variables are defined within the test or component. These variables are saved with the test or component and are accessible only within the test or component in which they were defined.
 
There are two ways to define User-defined internal environment variables.  

  • Manually define User-defined Environment Variable in the Environment Tab
  • Manually define a user-defined environment variable in the code itself

Manually define User-defined Environment Variable in the Environment Tab

Add-User-Defined-Environment-Variable-UFT

In the Environment tab, select the variable type as “User-defined” and click on the “+” icon. Enter the name of the variable in the name text box and its value in the Value text box and click the OK button. It will be added to the User-defined environment variable list.

Add-User-Defined-Environment-Variable2-UFT

In the same, you can add more user-defined environment variables as per your requirement. To get its value in your test type the following code in the editor.
Msgbox Environment(“MyTestEnvVar”) 
 
Environment-Variable2-UFT
 

Manually define a user-defined Environment variable in the code itself

This is the most convenient way to manually define Environment variables at any step in your test script or function and used its value across the project.

Syntax: Environment.Value(“Name of Environment Variable”) = “Required value”
Example : Environment.Value(“Language”)= “English”
To get its value,write Msgbox Environment(“Language”)

Environment-Variable-UFT-3.
 

User-Defined External Environment Variables

User-defined external environment variables are predefined in the active external environment variables file. You can create as many files as you want and select an appropriate file for each test or component, or change files for each run. External environment variable values are designated as read-only within the test or component and can not be modified at runtime. 
 

Leave a Reply