Tuesday 13 December 2011

QTP Environment Variables and or Objects

How Can I make a variable/Object environmentally available i.e. throughout execution?


QTP has Environment Object which has Execution wide or global scope.

To Store your variable.

Environment.Value("MyVariable") = "MyValue"

or you can write this way

Environment("MyVariable") = "MyValue"


To Access Variable Value

strValue = Environment.Value("MyVariable")

or

strValue = Environment("MyVariable")

Do you know apart from storing simple values (like String, integer etc ), we can also store objects like dictionary, array etc.

Suppose I have defined a Dictionary Object

Set oDic = CreateObject("Scripting.Dictionary")

Following are the key-values pair defined
oDic.Add "Key1", "Value1"
oDic.Add "Key2","Value2"


Now Somewhere in code, I will store this dictionary object in Environment.

Environment("MyDictionaryObj") = oDic


How will I access it now? Here it is . . .

MyValue1 = Environment("MyDictionaryObj")("Key1")

Now try to understand the above line, the Blue background will evaluate to Dictionary Object that we had stored. Now see below Debug viewer Window to make you visualize about these objects.


Similarly we can also store array
MyArray = Array("Pankaj", "Anand", "Hussain")
Environment("MyEnviroArray") = MyArray

So How will I retrieve the value, below screenshot is self explanatory.





This was all about the programmatic approach to play with Environment Object.
Now let's see what QTP has in Environment Object.

Well their are 2 types of QTP Environment object.
1. In built
2. User Defined

Under QTP IDE, go to file--> Settings

Now whatever environment variable we defined in code will be User Defined type of Environment Variables.

QTP has inbuilt defines Variables which can be leveraged in your code from Framework designing perspective like "TestDir", "TestName", "UserName" etc . . .
Below is the screenshot of debug viewer that is during run time this variables are also available

Now Talking about User Defined Variable
You can also define variable initially also. like this . . .



Now Below is the screenshot of "Test Settings" Window during Run Time.
All user defined variables can be seen in this window.
Can you see it? . . . Open your QTP- try it

And Last but not the least you can also take bunch of Environment variable in an XML file and import in QTP. Create XML file like below fig and then use import functionality which is available in File--> Settings window.


Below is the figure after importing XML based Environment Variables


So, go ahead and try it . . .
Have a great time.

-Pankaj Dhapola

No comments: