Environment Variable -An advanced concept in QTP

All about environment variable

Before going further into details I would like to take you to a general level view of Environment variable...please visit here:

Till now you have learnt that environment is kind of Global variable. Now think about Global variable in any programming language. The scope of the global variable is inside the test while the test or the program is running. If you look at machine view architecture it is nothing but a memory allocation to store some value. So when program ends the memory allocation is also released.

Now say you have used many global variable inside your test. You will be doing some operation like....

You have done Built In Variables are activated at runtime only. by using this variables we can know test execution information. That is which user is executed, how Iterations were executed, which Actions were executed, which test was executed, test directory path..like that.

User Defined Internals..we can create them and these variables are used only a test where they were created. Once you created that variable you can not modify the name of variable and u can modify the value. If u want to use these variable in many tests you can export them in XML file. There u can modify the name of the variable.

User defined Externals...The variables which are Imported from XML file are called External variables. These are read only variables. u can modify name and value of these variables. If u want to modify them..go to XML file which are stored and right click on that file click n edit. The script will open, there u can edit name and value.

Now you have configured the test and run on your machine ...it is running fine. You have delivered your test to the client or the internal automation team for a QA review or a person is doing review of the script. Will it run proper? Or will he be able to check the script in his machine? No...what went wrong....It will throw error on these environment variable. This machine requires the variable to be configured and set as per your machine. Or else you need to document the entire variables used as environment variable. Now go back to the 3 types of operations for environment variable. The last two operation is very much important for portability. If you export the variable list from your machine and hand it over ,along your script. The second person or whoever is running the script can make use of this. You are free from documentation.

So here what exactly you are doing is ..you are saving the variable for future use.So you need to save those variable inside your permanent memory not like a global variable used in program and vanishes after execution. Hence creating this light weight .xml file.
Also these variable can be added to .txt file. Those are basically .ini file.

.ini stands for Initialization/Configuration File Various programs use this extension; Take clues from the location of the file as a possible pointer to exactly which program is producing the file. These files are typically ASCII text with various configuration options set into them

Dim App ‘As Application
‘ Launch QTP
Set App = CreateObject(“QuickTest.Application”)
App.Visible = True
‘ Load an INI file with user-defined parameters
App.Test.Environment.LoadFromFile “C:\Test\environment_file1.ini”
‘ Set the value of a specific user-defined Environment variable
App.Test.Environment.Value(“a_variable”) = “new value”
As you can see from the example, the Environment variable file is actually an .ini file. The structure would be:

Use of .INI file in QTP

It's used to store user defined external environment variables in 5x and 6x versions of QTP.
Because of the increased XML usage in all technologies from 8x, QTP is given a facility to store user defined external environment variables in XML files. QTP is having backward compatibility so that it will support .INI files in latest versions of QTP.

How to store environment variables in INI files?

step1: open a notepad file
step2: on top of the file write [Environment]
step3: specify variable names and values
step4: Save it as .ini file
step5: Associate environment file in QTP
File-->Settings-->environment-->User Defined



or else

We can load environment variables from an xml file.
File --> Settings and click the "Environment" tab
You can see a check box "Load environment variables " check it and give the path of the xml file.
when u run QTP , the variables in XML file will be available.And you can retrieve those values using Environment.value
("variable_name") from any actions.

You can retrive data from database(XML fromet)..

u can use SQL..
LIKE e.g. Select * FROM TableName For XML Type, AUTO

Dynamically you can access the XLM file as well....

Const XMLDataFile =

Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.Async = False

Set nodes = xmlDoc.SelectNodes()
MsgBox "Total Students in class 10 " & nodes.Length

Set nodes = xmlDoc.SelectNodes("/text()")

' get their values
For i = 0 To (nodes.Length - 1)
Title = nodes(i).NodeValue
MsgBox "Name #" & (i + 1) & ": " & Title

Inside your xml you will see the structure..

No of student in class 10

If this XML format is like this then we can write script :
dataToCompare = Environment.Value("No of student in class 10")
XMLpath =Environment.Value("QTPxmlPath")
Environment.LoadFromFile =(XMLpath&"\data.xml")
TestCaseID=Environment.Value("No of student in class 10”)

I have seen retrieving environment from ini file here as...
The following example retrieves information from an external INI file using GetPrivateProfileString, and uses it in the test. Note the use of the micByRef flag to indicate the out argument.

Extern.Declare micInteger,"GetPrivateProfileStringA", "kernel32.dll","GetPrivateProfileStringA", micString, micString, micString, micString+micByRef, micInteger, micString

Dim key, i, key2
key = String(32, "-")
i = Extern.GetPrivateProfileStringA("WREnv","addons","xxx", key, 32, "wrun.ini")
key2 = Left(key,i)
msgbox key & ";" & key2 & ";" & CStr(i)
Environment Variable -An advanced concept in QTP Environment Variable -An advanced concept in QTP Reviewed by Animesh Chatterjee on December 10, 2010 Rating: 5

No comments:

Powered by Blogger.