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.

Saturday, 18 February 2017

What are environment variables in UFT/QTP and what are the different types of environment variables


Environment Variables are similar to that of Global Variables when compared to traditional programming languages like "C".

Values of the Environment Variables can be accessible to all the Actions or functions or Tests and API using simple call

Syntax:


Environment("EnvironmentVariableName") = "Value"

We have two type of Environment Variables

1) Built In
2) User Defined
    i. User Defined - Internal
    ii. User Defined - External


Built In Environment variables are predefined variables defined by QTP/UFT.  Example of these variables are 


Msgbox Environment("OS")
Msgbox Environment("LocalHostName")

Note Please remember Environment Variable Names are case sensitive 




User can also create custom environment variables apart from the built-in environment variables. These variables are called User-defined Environment variables.

To create an user defined environment variable user can click on the Add(+) button which is shown in the below snapshot and define the Environment variable. 

In the snapshot below I have created an user defined environment variable ( Internal) . the Name of the Environment variable is "ProjectName" and when the user calls the environment variable it return the value "HealthCare"

Calling user defined variable is no different from built in environment variables. 

Example
Msgbox Environment("ProjectName")

































User Can export the created user defined( Internal) environment variables to XML file. To do this user has to click on the Export button as shown in the above snapshot.

The content of the xml file contains the following content

<Environment>
 <Variable>
  <Name>ProjectName</Name>
  <Value><![CDATA[HealthCare]]></Value>
 </Variable>
</Environment>




User can also add their own environment variables by adding necessary tag to the existing xml content

<Environment>
 <Variable>
  <Name>ProjectName</Name>
  <Value><![CDATA[HealthCare]]></Value>
 </Variable>
 <Variable>
  <Name>BlogName</Name>
  <Value><![CDATA[Http://BHOJARAZU.BLOGSPOT.IN]]></Value>
 </Variable>
 <Variable>
  <Name>BlogAuthorName</Name>
  <Value>Bhojarazu</Value>
 </Variable>
</Environment>


If you are willing to try you can copy the above code and save the code in a .xml file
The below snapshot shows how to add an external environment file to UFT/QTP. You can provide the fill path of
 the xml file which has valid tags format to display the content in UFT/QTP










How to clear the object from memory


Where objectName refers to the object you have created earlier.


Set ObjectName=Nothing



How to get used range in excel

Code:-

strFilePath = "C:\UFT_Folder\UserData.xlsx"

Set Excel_Obj = CreateObject("Excel.Application")


Set objworkbook = Excel_Obj.Workbooks.Open(strFilePath)
Set objworksheet = objworkbook.Worksheets("Sheet1")

msgbox objworksheet.usedrange.rows.count
msgbox objworksheet.usedrange.columns.count
msgbox objworksheet.usedrange.Address

Set objworksheet = nothing
Set objworkbook = nothing

Excel_Obj.Quit


Set Excel_Obj = nothing

' The First message box will give you used Rows Count
' The First message box will give you used columns Count
' The First message box will give you address of the last cell used like $F$8:$G$10



How to open a text file using VBScript or How to open a flat file using VBScript


Open text file can be opened using "Scripting.FileSystemObject"


OpenTextMode represents the File Read mode and Any Text file can be opened in the following modes

ForReading  1  Open a file for reading only. You can't write to this file.
ForWriting 2 Open a file for writing.
ForAppending 8 Open a file and write to the end of the file.



Syntax

filename = "c:\sample.txt"
OpenTextMode= 2

Set OpenTextMode_Obj = Createobject("Scripting.Filesystemobject")

If OpenTextMode_Obj.FileExists(filename) = true Then
    set r1 = OpenTextMode_Obj.OpenTextFile filename,OpenTextMode
 else
    ExitTest
End If

Msgbox r1.readall()



How to open a browser with google.com as url using VBSript & How to open a browser with google.com using UFT or QTP.

Syntax


Set Browser_Obj= Createobject("InternetExplorer.Application")

Browser_Obj.Visible = true

Browser_Obj.Navigate "www.google.com"

Set Browser_Obj = nothing


How to get certain part of string using VBScript

We Can use the mid method
'output = Mid(InputString, StartPosition, NoofCharacterRequired)

Syntax
output = Mid("UFT is Great Fun", 5, 5)
Msgbox output