Recent Post

QTP - Environment Variables

QTP & Environment Variables
Welcome
  QTP – VBScript (Part 1)
  QTP – VBScript (Part 2)
  QTP – VBScript (Part 3)
  QTP – VBScript (Part 4)
  QTP – VBScript (Part 5)
  QTP – VBScript (Part 6)
  QTP – VBScript (Part 7)
  QTP – VBScript (Part 8)
  QTP – VBScript Examples (Part9)
  QTP – VBScript Examples (Part10)
  QTP – Environment Variables
  QTP – Arrays
  QTP – Error Handling
  QTP – Functions
  QTP – Frameworks
  QTP – MORE….
Contact@
G A Reddy
QuickTest QTP
http://QuickTestQTP.BlogSpot.com
Environment variables
Environment variables represents the QTP Environment object, which enables you to set or retrieve the value of environment variables
Environment variables are like global variables
Environment variables can be accessed through out from 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/tests or functions.
Environment Variable - Types
Built-in
Built-In: These are the internal variables that are provided by QTP.
These provide 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.
User Defined.
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.
Example:
AppURL=Environment.value(“URL”)=http://newtours.demoaut.com
SystemUtil.Run AppURL
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.
Example:
MyEnvFile = “C:/EnvVars.xml”
Environment.LoadFromFile MyEnvFile
Browser().Page().WebEdit(“userName”).set Environment.value(“Uname”)
Environment variables
  To set the value of a user-defined, environment variable:
  Environment (VariableName) = NewValue
  To retrieve the value of a loaded environment variable:
  CurrValue = Environment (VariableName)
  Example:
  Environment.Value("MyVariable")=10
  MyValue=Environment.Value("MyVariable")
Environment variables: Methods and Properties
Associated Methods and Properties
ExternalFileName Property
LoadFromFile Method
Value Property
ExternalFileName
It returns the name of the loaded external environment variable file specified in the Environment pane of the Test Settings dialog box.
If no external environment variable file is loaded, returns an empty string.
Syntax
Environment.ExternalFileName
Example:
'Check if an External Environment file is loaded and if not, load it.
fileName = Environment.ExternalFileName
If (fileName = "") Then
    Environment.LoadFromFile("C:\Environment.xml")
End If
'display value of one of the Environment variables from the External file
msgbox Environment("MyVarName")
LoadFromFile
It loads the specified environment variable file.
The environment variable file must be an XML file using the following syntax:
Syntax
Environment.LoadFromFile(Path)
Example:
  Environment.LoadFromFile("C:\QuickTest\Files\MyVariables.xml")
  Environment.LoadFromFile("C:\MyVariables.xml")
Value
  Sets or retrieves the value of environment variables.
  You can retrieve the value of any environment variable.
  You can set the value of only user-defined, environment variables.
  Syntax
  To set the value of a user-defined, environment variable:
  Environment.Value(VariableName) = NewValue
  To retrieve the value of a loaded environment variable:
  CurrValue = Environment.Value (VariableName)
  Example:
  Environment.Value("MyVariable")=10
  MyValue=Environment.Value("MyVariable")
Environment Variable file structure
<Environment>
<Variable>
       <Name> UN </Name>
       <Value>GAReddy</Value>
</Variable>
<Variable>
       <Name> Pwd</Name>
       <Value>GAReddy</Value>
</Variable>
</Environment> 
Environment Variables @ Scripts
Getting the Environment variables
  ' How to get the Environment variables
  myname=Environment.Value("name")
  'OR
  myname=Environment(“Uname")
  ‘Assigning Env Variable values
  Browser().Page().WebEdit(“userName”).set Environment.value(“Uname”)
  ‘OR
  Browser().Page().WebEdit(“userName”).set myname

Check if the specific variable exists in Env file
  'Checking if the specific variable exists on the Environment file
  On Error Resume Next
  myvar=Environment("name")
  If Err.Number <> 0 Then
  MsgBox "var does not exist"
  MsgBox err.number
  MsgBox Err.Value
  else
  MsgBox "var Exists"
  End If
Adding Environment variables dynamically
' Adding Environment variables dynamically
Environment("MyNewVar")="I am new Environment Variable"
' To check the above one
On Error Resume Next
myvar=Environment("MyNewVar")
If Err.Number <> 0 Then
MsgBox "var does not exist"
MsgBox err.number
MsgBox Err.Value
Environment("MyNewVar")="Iam new var"
else
MsgBox "var Exists"
End If
Empty the Environment value
Loading Environment files dynamically
' To empty the Environment value
Environment("MyVar")=Nothing
 ' Loading Environment files dynamically
 Environment.LoadFromFile EnvFile
Check if an External Environment file is loaded
  'Check if an External Environment file is loaded and if not, load it.
  fileName = Environment.ExternalFileName
  If (fileName = "") Then
  Environment.LoadFromFile("D:\EnvVars.xml")
  End If
  'display value of one of the Environment variables from the External file
  msgbox (Environment("UN"))
  '‘Second method
  On Error Resume Next
  fileName = Environment.ExternalFileName
  If Err.Number <> 0Then
      MsgBox "The Specified Environment File  does not exist"
  '    Environment.LoadFromFile("C:\EnvironmentFile.xml")
  Environment.LoadFromFile("D:\EnvVars.xml")
  Else
       MsgBox "The Specified Environment File exist"
  End If


1 comments:

Post a Comment

GAReddy @ OneTestingCenter @ All Articles