Académique Documents
Professionnel Documents
Culture Documents
VB Script Procedures/Functions
---------------------------------
'Write a Function to Count all Opened Browsers on desktop and close them
all
Function Close_Browsers()
Dim oBrowser, Browsers, TotBrowsers
1
Visit: www.gcreddy.com for QTP Examples
Set oBrowser=Description.Create
oBrowser("micclass").value="Browser"
Set Browsers=Desktop.ChildObjects(oBrowser)
TotBrowsers=Browsers.count
Function Count_Links()
Dim oLink, Links, TotLinks
Set oLink=Description.Create
oLink("micclass").value="Link"
Set Links=Browser("title:=.*").Page("title:=.*").ChildObjects(oLink)
TotLinks=Links.count
Next
End Function
Call Count_Links()
-----------------------------------------
Function Count_Buttons()
Dim oButton, Buttons,TotButtons, i, myButton
Set oButton=Description.Create
oButton("Class Name").Value="WinButton"
Set Buttons=Dialog("text:=Login").ChildObjects(oButton)
TotButtons=Buttons.count
Msgbox TotButtons
For i= 0 to TotButtons-1
myButton=Buttons(i).Getroproperty("text")
Msgbox myButton
Next
End Function
Call Count_Buttons()
------------------------------
Function Count_Objects(object)
Dim oobject, objects,Totobjects
Set oobject=Description.Create
oobject("Class Name").Value=object
Set objects=Window("text:=Flight Reservation").ChildObjects(oobject)
Totobjects=objects.count
Msgbox Totobjects
End Function
Call Count_objects("WinButton")
Call Count_objects("WinEdit")
Call Count_objects("WinRadioButton")
Call Count_objects("WinComboBox")
-----------------------------------------------------------------
2
Visit: www.gcreddy.com for QTP Examples
Function Launch_App()
SystemUtil.Run "C:\Program Files\Internet
Explorer\IEXPLORE.EXE","www.jjperfumes.com"
Wait (8)
Browser_Name = Browser("JJ Perfumes-Discount perfume").GetROProperty("title")
If Browser_Name= "JJ Perfumes-Discount perfume cheap brand name perfumes,
fragrance & cologne online" Then
Launch_App="Application Launched Sucessfully"
Msgbox Launch_App
Else
Launch_App="Application Not Launched"
Msgbox Launch_App
End If
End Function
-------------------------------
Function Registration(Email)
Set myBrowser=Browser("JJ Perfumes-Discount perfume").Page("JJ Perfumes-
Discount perfume_2")
Browser("JJ Perfumes-Discount perfume").Page("JJ Perfumes-Discount
perfume").Link("Register").Click
myBrowser.WebList("usertype").Select "Retailer"
myBrowser.WebEdit("firstname").Set "utyyruy"
myBrowser.WebEdit("lastname").Set "uyuu"
myBrowser.WebList("gender").Select "Male"
myBrowser.WebEdit("address1").Set "uyuytutuu"
myBrowser.WebEdit("phone").Set "9222222223"
myBrowser.WebEdit("email").Set Email
myBrowser.WebEdit("city").Set "hyderabad"
myBrowser.WebList("state").Select "MO - MISSOURI"
myBrowser.WebList("state").Select "MT - MONTANA"
myBrowser.WebEdit("zip").Set "34567"
myBrowser.WebEdit("password").SetSecure
"3c30b5521a58241cf85aea528e1f4148a78f"
myBrowser.WebEdit("conPass").SetSecure
"3c30b557ccf6dbd1b0951c84b2fefb951f55"
myBrowser.WebButton("Submit").Click
Confirm_Message = Browser("JJ Perfumes-Discount perfume").Page("JJ Perfumes-
Discount perfume_3").WebElement("Registered
successfully.").GetROProperty("innertext")
3
Visit: www.gcreddy.com for QTP Examples
Browser("JJ Perfumes-Discount perfume").Page("JJ Perfumes-Discount
perfume_3").WebEdit("username").Set Email
Browser("JJ Perfumes-Discount perfume").Page("JJ Perfumes-Discount
perfume_3").WebEdit("password").SetSecure Password
Browser("JJ Perfumes-Discount perfume").Page("JJ Perfumes-Discount
perfume_3").WebButton("Login").Click
Dim val
val="Hyderabad"
Msgbox Asc(val) '72
2) Chr Function
Dim val
val=72
Msgbox Chr(val) 'H
3) Abs Function
Dim val
val=100.49
Msgbox Abs(val)' 100.49
val=100.59
Msgbox Abs(val) '100.59
val=-100.49
4
Visit: www.gcreddy.com for QTP Examples
Msgbox Abs(val) '100.49
4) Round Function
Dim val
val=100.49
Msgbox Round(val)' 100
val=100.59
Msgbox Round(val) '101
val=-100.49
Msgbox Round(val) '-100
val=-100.59
Msgbox Round(val) '-101
val=100.5
Msgbox Round(val) '100
5) IsArray
6) IsNumeric
Dim val
val=100
Msgbox IsNumeric(val) 'True
val=100.456
Msgbox IsNumeric(val) 'True
val="abcd"
Msgbox IsNumeric(val) 'False
val="100"
Msgbox IsNumeric(val) 'True
val=#10/10/10#
Msgbox IsNumeric(val) 'False
7) IsDate
Dim val
val=#10-10-10#
Msgbox IsDate(val) 'True
val=#10-10-2010#
Msgbox IsDate(val) 'True
val=#10/10/2010#
Msgbox IsDate(val) 'True
val=#20/10/2010#
5
Visit: www.gcreddy.com for QTP Examples
Msgbox IsDate(val) 'True
'val=#20/20/10#
'Msgbox IsDate(val) 'Error
val=#10/40/10#
Msgbox IsDate(val) 'Error
val=#Sep/20/10#
Msgbox IsDate(val) 'True
val=100
Msgbox IsDate(val) 'False
val="asdd"
Msgbox IsDate(val) 'False
8) IsEmpty
Dim x,y
x=100
Msgbox IsEmpty(x) 'False
Msgbox IsEmpty(y) 'True
y="abcd"
Msgbox IsEmpty(y) 'False
y=0
Msgbox IsEmpty(y) 'False
y=Empty
Msgbox IsEmpty(y) 'True
9) Array
Dim x,y
10) Split
Dim x,y
x="VB Script Language"
Msgbox IsArray(y) 'False
y=Split(x," ")
Msgbox IsArray(y) 'True
11) DateDiff
6
Visit: www.gcreddy.com for QTP Examples
Dim Date1, Date2
Date1=#10-10-09#
Date2=#10-10-07#
Msgbox DateDiff("d",Date1,Date2)
Date1=#10-10-09#
Date2=#10-10-11#
Msgbox DateDiff("d",Date1,Date2) &" Days"
Date1=#10-10-09#
Date2=#10-10-11#
Msgbox DateDiff("m",Date1,Date2) &" Months"
Date1=#10-10-09#
Date2=#10-10-11#
Msgbox DateDiff("y",Date1,Date2) &" Days"
Date1=#10-10-09#
Date2=#10-10-11#
Msgbox DateDiff("yyyy",Date1,Date2) &" Years"
Date1=#10-10-09#
Date2=#10-10-11#
Msgbox DateDiff("w",Date1,Date2) &" Weeks"
Date1=#10-10-09#
Date2=#10-10-11#
Msgbox DateDiff("q",Date1,Date2) &" Quarters"
Date1=#10-10-09#
Date2=#10-10-11#
Msgbox DateDiff("h",Date1,Date2) &" Hours"
Date1=#10-10-09#
Date2=#10-10-11#
Msgbox DateDiff("n",Date1,Date2) &" Minutes"
Date1=#10-10-09#
Date2=#10-10-11#
Msgbox DateDiff("s",Date1,Date2) &" Seconds"
Dim val
val="Hyderabad"
Msgbox Len(val) '9
7
Visit: www.gcreddy.com for QTP Examples
Dim val
val="Hyderabad"
Msgbox Left(val,3) 'Hyd
Dim val
val="Hyderabad"
Msgbox Right(val,3) 'bad
Dim val
val="Hyderabad"
Msgbox Mid(val,3) 'bad
--------------------------
Dim x
x="Hyderabad"
Msgbox Left(x,3) 'Hyd
Msgbox Mid(x,1,3) 'Hyd
Result Criteria:
-----------------
8
Visit: www.gcreddy.com for QTP Examples
a) If st1>str2 then it returns 1
val1="QTP"
val2="qtp"
Msgbox StrComp(val1,val2,0) '-1
val1="qtp"
val2="QTP"
Msgbox StrComp(val1,val2,0) '1
val1="QTP"
val2="qtp"
Msgbox StrComp(val1,val2,1) '0
------------------------------------------
17) Cdbl Function
Dim val
val="100.45"
Msgbox VarType(val) '8 for String
val=Cdbl(val)
Msgbox VarType(val) '5 for Double
val=CInt (val)
Msgbox VarType(val) '2 for Integer
val="100.45"
Msgbox VarType(val) '8 for String
Msgbox val
val=CInt (val)
Msgbox VarType(val) '2 for Integer
Msgbox val
Dim val, x
val="Hyderabad"
Msgbox VarType(val) '8 for String
val="100"
9
Visit: www.gcreddy.com for QTP Examples
Msgbox VarType(val) '8 for String
val="100.456"
Msgbox VarType(val) '8 for String
val=100
Msgbox VarType(val) '2 for Integer
val=100.456
Msgbox VarType(val) '5 for Double
Set x=CreateObject("Scripting.FileSystemObject")
Msgbox VarType(x) '9 for Automation Object
val="HyderABAD"
Msgbox LCase(val) 'hyderabad
val="hyderabad"
val="100"
Msgbox LCase(val) '100
val=100
Msgbox LCase(val) '100
Dim val
val="HYDERABAD"
Msgbox UCase(val) 'HYDERABAD
val="HyderABAD"
Msgbox UCase(val) 'HYDERABAD
val="hyderabad"
Msgbox UCase(val) 'HYDERABAD
val="100"
Msgbox UCase(val) '100
val=100
Msgbox UCase(val) '100
10
Visit: www.gcreddy.com for QTP Examples
Dim val
val=" vb script "
Msgbox val
Msgbox Trim(val)
Dim val
val=Date
Msgbox val
val=Time
Msgbox val
val=Now
Msgbox val
Variable1=Timer
----------
Statements
----------
---------
Variable2=Timer
Variable3=Variable2-Variable1
Ex:
11
Visit: www.gcreddy.com for QTP Examples
Start_Transaction=Timer
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").Activate
Dialog("Login").WinEdit("Agent Name:").Set "dfgh"
Dialog("Login").WinEdit("Password:").SetSecure
"3c30b6b7c56ffcb6847571203c0da519da6af43e"
Dialog("Login").WinButton("OK").Click
Window("Flight Reservation").Close
End_Transaction=Timer
Transaction_Time=End_Transaction-Start_Transaction
Msgbox Transaction_Time
Dim objFso
'Creating an Automation Object in File System Object , that can be used to perform
Operations on Computer File System
Set objFso=CreateObject("Scripting.FileSystemObject")
Dim objExcel
'Creating an Automation Object in Excel Application , that can be used to perform
Operations on Excel Files
Set objExcel=CreateObject("Excel.Application")
Dim objWord
'Creating an Automation Object in Word Application , that can be used to perform
Operations on Word Documents
Set objWord=CreateObject("Word.Application")
Dim objDic
'Creating an Automation Object in Dictionary Object, that can be used to define key,
value pairs
Set objDic=CreateObject("Scripting.Dictionary")
Dim objCon
'Creating an Automation Object in Database Connection , that can be used to
connect to Databases
Set objCon=CreateObject("Adodb.Connection")
Dim objRs
'Creating an Automation Object in Database RecordSet , that can be used to perform
Operations on Database Tables
Set objRs=CreateObject("Adodb.RecordSet")
Dim objCom
'Creating an Automation Object in Database Command , that can be used to
Maniplate Databases
Set objCom=CreateObject("Adodb.Command")
--------------------------------------------------------
Examples:
----------
12
Visit: www.gcreddy.com for QTP Examples
'Create a Folder
Dim objFso
Set objFso=CreateObject("Scripting.FileSystemObject")
objFso.CreateFolder "C:\Documents and Settings\Administrator\Desktop\abc"
'Check the Existance of a Folder , if not exist then Create the Folder
Dim objFso, myFolder
myFolder="C:\Documents and Settings\Administrator\Desktop\abc"
Set objFso=CreateObject("Scripting.FileSystemObject")
If Not objFso.FolderExists(myFolder) Then
objFso.CreateFolder (myFolder)
End If
'Copy a folder
Dim objFso, myFolder
myFolder="C:\Documents and Settings\Administrator\Desktop\November"
Set objFso=CreateObject("Scripting.FileSystemObject")
objFso.CopyFolder myFolder, "C:\Documents and Settings\Administrator\My
Documents\December"
'Delete a folder
Dim objFso, myFolder
myFolder="C:\Documents and Settings\Administrator\Desktop\November"
Set objFso=CreateObject("Scripting.FileSystemObject")
objFso.DeleteFolder(myFolder)
'Delete a folder
Dim objFso, myFolder
myFolder="C:\Documents and Settings\Administrator\Desktop\November"
Set objFso=CreateObject("Scripting.FileSystemObject")
If objFso.FolderExists(myFolder) Then
objFso.DeleteFolder(myFolder)
End If
13
Visit: www.gcreddy.com for QTP Examples
Set objFso=Nothing
----------------
'Caputure Buttons Name fro Login Dialog box and Export to an external flat file
Dim objFso, myFile, oButton
Set objFso=CreateObject("Scripting.FileSystemObject")
Set myFile=objFso.OpenTextFile("C:\Documents and
Settings\Administrator\Desktop\abc.txt",2) '1-Read, 2-Write, 8- Append
myFile.WriteLine "Button Names"
myFile.WriteLine "---------------"
Set oButton=Description.Create
oButton("Class Name").value="WinButton"
Set Buttons=Dialog("text:=Login").ChildObjects(oButton)
TotButtons=Buttons.Count
For i= 0 to TotButtons-1
myButton=Buttons(i).GetRoProperty("text")
myFile.WriteLine myButton
Next
myFile.Close
Set objFso=Nothing
------------------
'Caputure Buttons Name fro Login Dialog box and Export to an external flat file
Dim objFso, myFile, oButton
Set objFso=CreateObject("Scripting.FileSystemObject")
Set myFile=objFso.OpenTextFile("C:\Documents and
Settings\Administrator\Desktop\abc.txt",2) '1-Read, 2-Write, 8- Append
myFile.WriteLine "Customer Names"
myFile.WriteLine "---------------"
If Not Window("Flight Reservation").Exist(3) Then
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").Activate
Dialog("Login").WinEdit("Agent Name:").Set "yetyyt"
Dialog("Login").WinEdit("Password:").SetSecure
"3c30cae3f35fe9752b50e9b98a5efed3e96b7fc7"
Dialog("Login").WinButton("OK").Click
End If
For i =1 to 10 Step 1
Window("Flight Reservation").Activate
Window("Flight Reservation").WinButton("Button").Click
Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set
"ON"
Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set i
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
wait (1)
Customer_Name = Window("Flight
Reservation").WinEdit("Name:").GetROProperty("text")
myFile.WriteLine Customer_Name
Next
myFile.Close
14
Visit: www.gcreddy.com for QTP Examples
Set objFso=Nothing
15