Pages

Saturday, September 18, 2010

QTP Environment Variables

swarupa Tags:
There are 3 types of Environment Variables
1.Built-In
2.Internal
3.External
Built-In :-These are predefine variables in Quick Test.They can be viewed under File—>Settings—>Environments Variables.They can be used to know the property values of the corresponding property names.
Example:- The following are few of  the list of environment variables.
OS = give the Operating System currently used on the system
OSVersion = gives the Operating System  version number
TestName = gives the current test name that is open
ProductDir = Folder path where the product is installed
1.Process to use Built-In Environment variables
go to Files—>Settings—>Environment the below window opens showing the list of all Built-In variables

image
remember any property name for which you want to see the corresponding value
Example1:-
a=Environment("ProductDir")
MsgBox a
when you run the above line of script in QT the output will be as follows
image
Example2:-
a=Environment("ProductName")
MsgBox a
output:-
image
Note :- The product names mentioned in QT should be written in the same as gives(case-sensitive) other wise you will get error like below
a=Environment("Productname")
MsgBox a
output
image
Internal Environment Variables:-
go to file—>settings—>Environment—>click on the dropdown box it will be showing you the option “User-defined variables” like below window
image
select “user-defined”—>click on green + symbol the below window opens –>enter the Name = x and Value = Welcome to the world of Automation—>click on OK—>OK—>it will be showing you the created “internal “ Environment variable.
image image
go to Expert view in QT and the below line of script
a=Environment("x")
MsgBox a
output:-
image
Note :- There is no restriction on creating the number of environment variables
External Environment variables:-
Step1:
open Notepad and the following lines of code(case-sensitive)
<Environment>
<Variable>
<Name>City</Name>
<Value>HYDERABAD</Value>
</Variable>
<Variable>
<Name>Area</Name>
<Value>Cyber-City</Value>
</Variable>
</Environment>
Note :- We can create any number of variables in XML file
save the above file as xml .i have saved it as “address.xml” file in “d” drive
Step2:-
Go to Files—>Settings—>Environment->user-defined—>select the checkbox “Load variables and values from External file” and at “file” browse and select the location where you have selected the “address.xml” file –> in the  below window it is showing the external environment variables added  in blue font—>click on apply-->OK
image
Step3:-
in expert view write the below line of script
a=Environment("Area")
MsgBox a
output:-
image
a=Environment("City")
MsgBox a
output:-
image 
Example :-External Environment Variables for Flight application
step1:-
Record the script for login in QT it will generate the script as follows
SystemUtil.Run "C:\Program Files\HP\QuickTest Professional\samples\flight\app\flight4a.exe","","C:\Program Files\HP\QuickTest Professional\samples\flight\app\","open"
Dialog("Login").WinEdit("Agent Name:").Set "agent"
Dialog("Login").WinEdit("Password:").SetSecure "4c94b515b5ade453a27b0162cb431a4b265d2546"
Dialog("Login").WinButton("OK").Click
Window("Flight Reservation").Close
Step2:-
go to Files—>Settings—>Environment—>user-defined—>and environment variables as follows
image image
image
click on apply—>OK
Step2:-
Go to expert view in QT
do the following changes in the generated script
agn = Environment.Value("agentname")
pwd = Environment.Value("password")
SystemUtil.Run "C:\Program Files\HP\QuickTest Professional\samples\flight\app\flight4a.exe","","C:\Program Files\HP\QuickTest Professional\samples\flight\app\","open"
Dialog("Login").WinEdit("Agent Name:").Set agn
Dialog("Login").WinEdit("Password:").Set pwd
Dialog("Login").WinButton("OK").Click
Window("Flight Reservation").Close
Note :- In  Environment.value() the values created internal should match the values given in the script
step3:-
Run now it will execute with the values you have given under environment tab
External:-
step A:-
create login.xml file as follows
- <Environment>
- <Variable>
<Name>agn</Name>
<Value>swarupa</Value>
</Variable>
- <Variable>
<Name>pwd</Name>
<Value>mercury</Value>
</Variable>
</Environment>
step B :- write the below line of code in QT Expert View
agn = Environment.Value("agentname")
pwd = Environment.Value("password")
SystemUtil.Run "C:\Program Files\HP\QuickTest Professional\samples\flight\app\flight4a.exe","","C:\Program Files\HP\QuickTest Professional\samples\flight\app\","open"
Dialog("Login").WinEdit("Agent Name:").Set agn
Dialog("Login").WinEdit("Password:").Set pwd
Dialog("Login").WinButton("OK").Click
Window("Flight Reservation").Close
Step C :-
Go to Files—>Settings—>Environment->user-defined—>select the checkbox “Load variables and values from External file” and at “file” browse and select the location where you have selected the “address.xml” file –> in the  below window it is showing the external environment variables added  in blue font—>click on apply—>OK it show as follows
image
step D :-
Run
it will execute with agentname as swarupa and password as mercury as mentioned in the xml file
LoadFromFile:-
we can direclty specify the file path like below in QT and run
Environment.LoadFromFile( "D:\login3.xml")
agn = Environment.Value("agentname")
pwd = Environment.Value("password")
SystemUtil.Run "C:\Program Files\HP\QuickTest Professional\samples\flight\app\flight4a.exe","","C:\Program Files\HP\QuickTest Professional\samples\flight\app\","open"
Dialog("Login").WinEdit("Agent Name:").Set agn
Dialog("Login").WinEdit("Password:").Set pwd
Dialog("Login").WinButton("OK").Click
Window("Flight Reservation").Close