Saturday, April 6, 2013

Environment Variables

Environment variables in QTP are variables whose value is available for all actions in a test. These values are available globally for any actions, libraries, recovery scenarios etc.

There are mainly two types of environment variables:
Built-In and User-Defined.

Built-in variable - Built-in variables are QTP defined variables that contains information of Test Properties, computer’s information, Information related to operating system and Action’s property.

User defined variable - Other than environment variable defined in-built in QTP, We can define user defined environment variables. The main purpose of defining user defined variables is to define global variables that can be used throughout test execution irrespective of working with different Actions. So scope of user defined variable is throughout the test execution. This can be of type internal or external if loaded from a file.
User-Defined internal environment variable: Variables defined in the test and saved with the test. You can change these environment variables during run time.



Environment.Value("URL")= "http:\\www.google.com"
URL=Environment.Value("URL")
Systemutil.Run "iexplore.exe",URL

  •  User-defined external environment variable: Variables declared in an external environment file. You cannot change these environment variables during run time.
Steps to follow:
File --> settings --> Environment --> select variable type as user defined --> click add icon (+) --> enter variable name & Value --> click OK --> click export --> browse path and enter file name --> save with xml extension --> click OK.
2. Associate environment variable file(xml)
File --> settings -->Environment --> select variable type as user defined --> check “load variables and values from the external file” --> browse path of the xml file --> click apply --> Click OK.
XML file with variable name and value should be mentioned
 Loading of variables from external file as shown above can be done through code:
'Check if an External Environment file is loaded and if not, load it.
fileName = Environment.ExternalFileName
if (fileName = "") Then
    Environment.LoadFromFile("C:\Env.xml")
End If


Use of Environment variable 
  • We can define parameters in QTP using environment variables.
  • Environment variables can act as global variables which can be used across different  tests/actions.
  • We can dynamically load environment variables during start up of QTP execution and can store url, environment, login details in a xml file,which will be loaded each time test is executed.
  • Built- in Environment variables gives us useful information about the test environment and extracting the information can be useful.

1. Built-In Environment Variables: These are the variables defined by Quick Test Professional. This includes many useful variables like OS, OSVersion, UserName, VUSerId etc.

  • ActionIteration - Indicates which action iteration is currently running.
  • ActionName - Indicates which action is currently running
  • ControllerHostName - the name of the computer which serves as a controller
  • GroupName - The scenario identification number
  • LocalHostName - Local Host Name
  • OS - Operation System
  • OSVersion - Operating system version
  • ProductDir - folder path where the product is installed
  • ProductName - Product Name
  • ProductVer - Product Version
  • ResultDir - Folder path where the results are saved
  • Scenarioid - The scenario identification number
  • SystemTempDir - System Temporary Directory
  • TestDir - Path of the Test
  • TestIteration - Indicates which test iteration is currently running
  • TestName - The name of the test
  • UpdatingActiveScreen -
  • UpdatingCheckpoints -
  • UpdatingTODescriptions -
  • UserName - Windows Login User Name
  • VUserId -
There are two types of User-Defined environment variables namely, internal and external.
'User can create their own environment variables like "URL"

User defined External: 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.

1. Create External user defined variable

Loading Environment File Using ExternalFileName Property and LoadfromFile Method -
Using environment variables in QTP
Environment variables have many uses while scripting with QTP. Consider a scenario where we are designing an automation framework. While running a framework we have to load many files, for instance an excel sheet containing object description. Path to this excel file my change when you move the framework from one computer to another. TestDir environment variable can come in handy in such a scenario. Environment.Value (“TestDir”) will return the path of the folder in which the test is located. By manipulating this value we can find the path to the excel file.
Consider another scenario where you want to open the test result folder after a test is completed. To do this we can make use of the environment variable ResultDir that returns the path where test results are stored.
Basic syntax to get an environment value is
Enviornment.Value (variable name)
To get test "OS" you have to use
Enviornment.Value (OS)
Default property or Environment is value so we can get the same value with
Environment (OS)

No comments:

Post a Comment