5 Tips to Effectively Use External Environment Variables in UFT

In my previous post, I have described the types of environment variables, how to add the environment variable manually in the UFT Environment tab and directly at any step in the code itself.

In this blog post, I am going to explain the following things on External Environment Variables that can be used appropriately to develop better automation test scripts and enhance the automation framework.

Loading User-Defined external environment variables into UFT Manually

To load a file manually into UFT, you will have to create an XML file in the following format and save it to the required location with the .xml extension like “E:\UFT_WorkSpace\Environment Variables\TestEnvVariable.xml

I have created an XML file having two environment variables(URL, ToolName) as follows:

Now go to the File>Settings and select the Environment menu from the Test Settings window. Select user-defined from Variable type drop-down and click on the select the checkbox “Load variables and values from external file” to enable the browse button.


Browse the TestEnvVariable.xml file and click on the OK button. A dialog will be displayed to convert the file path into a relative path. Click on the Yes button. The environment variables will be loaded in the user-defined variable list.

External Environment Variables in UFT

Now you can use these environment variables anywhere in your automation scripts or functions. One important thing is to be remembered here is that you can’t modify the value of this external user-defined at runtime because they will be available as read-only variables during the script execution.UFT will through an error message at runtime if you erroneously try to modify the value of an external environment variable.

Loading user-defined external environment variables into UFT dynamically

It’s quite simple to dynamically load user-defined external environment variables into UFT.
1. Create an XML file containing environment variables.
2. Write the following line in your test script.

Once the above line of code gets executed all the environment variables in the XML file will be loaded into UFT and will be available for you to be used wherever you want to use them.

Exporting user-defined External Environment variables manually

If you have manually added user-defined environment variables in UFT and want to export them in an XML file, you will have to follow the below steps
1. Go to File->Setting->Environment option.
2. Select User-defined option from Variable type dropdown
3. Press on “Export” button
4. You will have to select the location and file name and press the Save button
You will see that the XML file has been created containing all user-defined variables.

Getting the name of the external environment variable file name at runtime

Write the following line of code in your test script.

The name of the external environment variable file name will be displayed in the message box.


How to use the user-defined External environment variables

There could be various scenarios where user-defined .environment variables can be used. I am providing a few examples.

  • A function or an action is generating an output value that has to be used in many places, in that case, you can define the environment variable in the code itself and used it wherever required.
  • You are developing an automation framework and you are using some external files like MS Access Database to store your test data and a configuration file to store some config details. If you think that the path of the required files might change in the future or it might be different for every user as per their directory structure, in that case, you can manually define the environment variables in Environmental Tab to store the path of the required files that anybody can change from TestSettings rather than doing any change in the code.

Recommended Posts

Leave a Reply