Tricentis Tosca 16.0 Released on Feb-2023 ----- UFT has been upgraded from UFT 15.0.1 to UFT One 15.0.2, Beginning at November 2020.

Thursday 9 May 2019

Environment variables in QTP are like global variables in other programming languages which can be accessed through any part of the script. The values of these variables remains same irrespective of the number of iterations (unless you change them through scripting). These variables can prove to be very useful when you want a variable to be shared across various reusable actions.
There are two types of environment variables in QTP.
1. Bulit In
2. User defined
BuiltIn environment variable:
These are the internal variables that are provided by QTP. Among others they can provide you valuable information like the path of the folder where test is located, the path of the results folder, the name of the action iteration or the OS version. 
These environment variable are built in QTP. You can get following information from builtIn environment variables in QTP
– Local host name
– Operating System name
– OS version
– Path of the directory where the QTP is installed
– Name of the product (QTP)
– Name of test ( Main project file)
– Current directory path at you are working
And lots many thing, To know builtIn variable just go to File->setting->environment tab.
You can see list of built in environment variables with name and description.
Now You might be thinking of following question. This you will find very useful when you want to get some value dynamically(when QTP is running).
How to get value from environment variable during run time?Answer: If you want to know the path of the folder in which test is located. Just write following lines in your script, you will get what you want.
Dim strTestDir
strTestDir=environment.Value(“TestDir”) ‘ Make sure T and D is capital
msgbox strTestDir
Note:- Please write variable name as it appears in environment tab. It is case sensitive.
User defined environment variable:
These can be further defined into two types.
  • User defined Internal
    • These are the variables that we define within the test.
    • These variables are saved with the test and are accessible only within the test in which they were defined.
    • So how can we define and use them?
      To define them: Environment.Value(“name”)= “Ankur Jain”To call them: msgbox Environment.Value(“name”)
  • User defined External
    • These are the variables that we predefine in the active external environment variables file.
    • These can be created using a list of variable-value pairs in an external file in .xml format. This is a topic of a separate post that we will discuss later.
User can define environment variable.
There are two ways to define user specific environment variable manually.
1. Load variables and values from external file.
2. Enter one by one by variable name and its value.
I am describing here one way to define environment variable.
Steps:
Go to File->setting->environment tab
Select variable type to User defined
Than clik on “+” sign.
add name for e.g qtp
add value for e.g. http://quicklearnqtp.blogspot.com/
Click on apply and ok.
You can check it using following line and run it.
msgbox environment.Value(“qtp”)
You will see one msgbox with value of qtp which is “http://quicklearnqtp.blogspot.com”.
1. How to Load user defined environment variables from XML file to QTP manually and dynamically.
2. How to export user defined environment variables to any XML file manually and dynamically.
How to load user defined environment variables from file to QTP manually and dynamically?
Steps:
1. Create file named qtp.xml and write it in a following format and save it in c directory.
Load and Export User defined environment variables in QTP :
2. Go to File->setting->Environment tab.
3. select variable type to User-defined.
4. check “Load variables and values from external file” named box.
5. enter file name and path for e.g here c:\qtp.xml
6. Press apply and ok.
Now Lets talk about how to load it dynamically. You need to do following things.
1. Create one XML file described in above first step.
2. Write following line in your test script.
environment.LoadFromFile(“c:\qtp.xml”)
msgbox environment.Value(“qtp”)
After running above code You will have one message box with this content http://quicklearnqtp.blogspot.com/.
How to export user defined environment variables to any XML file manually and dynamically.
Steps:
1. Go to File->setting->Environment tab.
2. Select variable type to User-defined.
3. Press on “Export” button and give the name of file for e.g qtptest.xml
You will see all the user defined variable will be exported to qtptest.xml file( In XML format).
Now Lets talk about how to export variables to any XML file it dynamically/programmatically. You need to do following things.
Write following line in your test script.
————————————————-
environment.ExternalFileName(“c:\qtptest.xml”)
————————————————–
All user defined environment variables will be exported to qtptest.xml file.
Note: If you don’t have any user-defined variable stored in QTP. Than error will occur.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.