Wednesday, July 20, 2011

Environment Variables in QTP

The general questions that get raised regarding Environment Variables are:

1. What are Environment Variables?

2. How many types of Environment Variables are there?

3. How to define Environment variables and when to use them?


Environment Variables are those variables which can be used globally across the test(s). There are two types of Environment variables:

1. Built in Variables

2. User Defined Variables

1. Built in Variables:

Built in variables are predefined variables in QTP. These variables enable user to get the information about the test that is running and to get information about the operating system on which the test is running. Some of the built in variables are Action name, Test directory, Action iteration, Operating system, Operating system version, local host name etc.,

Go To File-->Settings-->Environment Tab to see Environment Variables section to see the list of Environmental Variables.

The values from Environment variables can be retrieved and used where ever necessary during run session. Values can be retrieved using the following command:

Environment.Value("name of variable")

To understand how to use built in variables, see the following example:

If you want to know where your test is located, just go to File-->Settings-->Environment Tab-->Select 'TestDir' in Built in Variables section. Location of the test will be displayed in Current Value field.



If you want to make use the same value in your script define it in the way it is shown in the script below:

a=environment.value("TestDir")
msgbox a


Note: Names of the variables are case sensitive. It should be exactly in the same way how it is defined in Environment tab section. See the following example:


Instead of ProductName if the variable name is entered as productname, an error would be displayed.



2. User defined Variables:

These are the variables that are defined by the user before the execution of test. These cane be used globally across different tests or they can also be restricted to one test.

User defined variables are again classified into two types:

a. Internal

b. External


a. User defined - Internal Variables : These are the variables that are defined by the user before running the test and these are restricted only to a particular test.

To define a user defined variable, go to File-->Settings-->Environment Tab-->Select 'User defined' in Variable type drop down-->


Click on Add icon(+) to add new variables. Add new parameter window gets opened with Name and Value fields. Enter the name of the variable, a value for the variable defined and click on Ok button.



In the same way add the other values that needs to be defined. As the values that are defined here are internal, the type column gets displayed as Internal.



b. User defined - External Variables:

These are the variables that are defined by the user and can be globally used across different tests. These variables are loaded from an external XML file. To define these variables, open a new test and define them in the same way how internal variables are defined and after adding all the variables click on Export button that gets displayed in User defined variable screen and save them in the form of an XML file.

Loading the external variables can be done in two ways. They can be loaded manually before running the test through Environment tab - User defined screen by checking the Load Variables check box and then by selecting the XML file.



After the file is selected, all the variables in XML file are loaded into the current test. And also the Type column gets displayed as 'External' indicating that the variables are loaded from external file. These can be used in the test using the same command that is used for Built in Variables i.e., Environment.Value("name of the variable")

External variables can also be loaded during execution of script directly without loading through Environment tab screen using the following statement:

Environment.LoadFromFile "C:/scripts/userinfo.xml"

Replace C:/scripts/userifno.xml in the above statement with the actual location of xml file in your system.

Lets see the usage of both internal and external variables in a script with an example for better understanding.

Assume that you have 5 tests which uses user name and password fields. And also assume that the values for user name and password fields gets changed frequently. Instead of going to each and every test and change the values for these fields every time when ever they are changed, declare them as environment variables and export them to a XML file. And load the XML file during script execution. See the following script:

Note: External Variables XML file is stored in the same folder where the tests are stored i.e., in C:/QTPScript.



In the second statement, location of test is obtained using "TestDir" built in variable. Now test path value would be "C:/QTPScript/Login". In the third statement XML file is loaded. As the XML file is present in QTPScript folder, to move from C:/QTPScript/login to C:/QTPScript ".."(two dots) are used and then the xml file name is added. 4th and 5th statements are to get the username and password values from the loaded XML file. 6 th and 7th staements uses the same values while execution of script.

Reference: Quick test user guide

2 comments:

  1. Hi,

    First of all want to Thank You for your useful post.This Software Testing article is very useful for me.
    I would like to introduce another good site which is having Software testing and QTP resource site, Have a look on QTPbook

    ReplyDelete
  2. hi, thanx a lot for the post. helped a lot.
    but i have a doubt when u need to run the test for 5 different username and password how do u do that? i mean when you are creating a variable it allows only to enter one value and we can only parameterize for that particular value..

    ReplyDelete