Blog to understand automation concepts in QTP, Selenium Webdriver and Manual Testing concepts

How to use and load Environment variables in QTP

Environment variables are a set of dynamic named values that can affect the way running processes will behave on a computer. Environment variables in qtp are very useful to define the environment condition in which QTP is working.

Types of Environment variables

There are two types of Environment variables in QTP

Built-in variable

Built-in variables are QTP defined variables that contains information of Test Properties, computer’s information, Information related to operating system and Action’s property.

User defined variable

Other than environment variable defined in-built in QTP, We can define user defined environment variables. The main purpose of defining user defined variables is to define global variables that can be used throughout test execution irrespective of working with different Actions. So scope of user defined variable is throughout the test execution. This can be of type internal or external if loaded from a file.

Loading of User defined Environment variable


Environment variable can be loaded in following ways:


1.  Through QTP UI

 User defined Environment variables can be created through UI as shown below

Adding new environment Parameter

 Option to export user-defined environment variables and loading of environment variables from external file is also available.


2.  Using ExternalFileName Property and LoadfromFile Method

 Loading of variables from external file as shown above can be done through code:

'Check if an External Environment file is loaded and if not, load it.
fileName = Environment.ExternalFileName
if (fileName = "") Then
    Environment.LoadFromFile("C:\Env.xml")
End If

3.  Using Environment.Value

 Value of environment variable can be extracted at runtime as 
StrEnv=environment.value(envName)

Similarly value of environment variable can be set at runtime as
Environment.value(“envname”)=”some value”

Where to Access Environment Variable 


To access the environment variable, Go to File>Settings>Environment and select built-in or user defined from the drop down.

Accessing environment variables

Use of Environment variable 


  • We can define parameters in QTP using environment variables.
  • Environment variables can act as global variables which can be used across different  tests/actions.
  • We can dynamically load environment variables during start up of QTP execution and can store url, environment, login details in a xml file,which will be loaded each time test is executed.
  • Built- in Environment variables gives us useful information about the test environment and extracting the information can be useful.



1 comment:

  1. Does one way have a benefit over the other like using Environment variable vs Dictionary object ? Is Dictionary more memory consumption

    ReplyDelete