You are on page 1of 15

Visit: www.gcreddy.

com for QTP Examples


-------------------------------------------------------------

VB Script Procedures/Functions
---------------------------------

Sub Login(Agent, Password)


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 Agent
Dialog("Login").WinEdit("Password:").Set Password
wait 1
Dialog("Login").WinButton("OK").Click

If Window("Flight Reservation").Exist(10) Then


Msgbox "Login Operation Sucessful"
Else
SystemUtil.CloseDescendentProcesses
Msgbox "Login Failed"
End If
End Sub
Call Login("acd","mercury")
-----------------------------

Function Login(Agent, Password)


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 Agent
Dialog("Login").WinEdit("Password:").Set Password
wait 1
Dialog("Login").WinButton("OK").Click

If Window("Flight Reservation").Exist(10) Then


Window("Flight Reservation").Close
Login= "Login Operation Sucessful"
Msgbox Login
Else
SystemUtil.CloseDescendentProcesses
Login= "Login Failed"
Msgbox Login
End If
End Function
Call Login("accd","mercury")
-----------------------------------

'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

For i= 0 to TotBrowsers-1 Step 1


Browsers(i).close
Next
End Function
Call Close_Browsers()
------------------------------------------------------

'Write a Function to Count Links in any opened webpage

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")

If Confirm_Message="Registered successfully. " Then


Registration="Customer Registerd Sucessfully"
Msgbox Registration
Else
Registration="Registration Failed"
Msgbox Registration
End If
End Function
--------------------------------
Function Login(Email, Password)
Browser("JJ Perfumes-Discount perfume").Page("JJ Perfumes-Discount
perfume_3").Link("Login").Click

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

If Browser("JJ Perfumes-Discount perfume").Page("JJ Perfumes-Discount


perfume_4").Link("Logout").Exist(5) Then
Login="Login Operation Sucessful"
Msgbox Login
Else
Login="Login Operation Failed"
Msgbox Login
End If
End Function
-----------------------------------------------------
Built in Functions
-------------------
1) Asc Function

Dim val
val="Hyderabad"
Msgbox Asc(val) '72

Msgbox Asc("A") '65


Msgbox Asc("Z") '90
Msgbox Asc("a") '97
Msgbox Asc("z") '122
Msgbox Asc(1) '49
Msgbox Asc("*") '42

2) Chr Function
Dim val
val=72
Msgbox Chr(val) 'H

Msgbox Chr(65) 'A


Msgbox Chr(90) 'Z
Msgbox Chr(97) 'a
Msgbox Chr(122) 'z
Msgbox Chr(49) '49
Msgbox Chr (42) '*

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

Dim a, b(3), c(), d(4,5)


Msgbox IsArray(a) 'False
Msgbox IsArray(b) 'True
Msgbox IsArray(c) 'True
Msgbox IsArray(d) 'True

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

Msgbox IsArray(x) 'False


x=Array("abcd",100, #10-10-10#)
Msgbox IsArray(x) 'True

Msgbox x(1) '100

10) Split
Dim x,y
x="VB Script Language"
Msgbox IsArray(y) 'False
y=Split(x," ")
Msgbox IsArray(y) 'True

Msgbox y(1) 'Script

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"

12) Len Function

Dim val
val="Hyderabad"
Msgbox Len(val) '9

Msgbox Len ("asd1234") '7

Msgbox Len (1000) '4

Msgbox Len (#10-10-10#) '10

7
Visit: www.gcreddy.com for QTP Examples

Msgbox Len ("#10-10-10#") '10

Msgbox Len (#Sep-10-10#) '9


Msgbox Len (#Dec-10-10#) '10

13) Left Function

Dim val
val="Hyderabad"
Msgbox Left(val,3) 'Hyd

Msgbox Left(9247892478,1) '9

Msgbox Left("%*&y",2) '%*

14) Right Function

Dim val
val="Hyderabad"
Msgbox Right(val,3) 'bad

Msgbox Right(9247892478,1) '8

Msgbox Right("%*&y",2) '&y

15) Mid Function

Dim val
val="Hyderabad"
Msgbox Mid(val,3) 'bad

Msgbox Mid(9247892478,5,2) '89

Msgbox Mid("%*&y",2) '*&y

Msgbox Mid(#10-10-10#,4,5) '10/20

--------------------------
Dim x
x="Hyderabad"
Msgbox Left(x,3) 'Hyd
Msgbox Mid(x,1,3) 'Hyd

Msgbox Right (x,3) 'bad


Msgbox Mid (x,7)

Msgbox Mid(x,5,3) 'rab


---------------------------
16) StrComp Function

Result Criteria:
-----------------

8
Visit: www.gcreddy.com for QTP Examples
a) If st1>str2 then it returns 1

a) If st1<str2 then it returns -1


a) If st1=str2 then it returns 0
Dim val1,val2
val1=100
val2=100
Msgbox StrComp(val1,val2) '0

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

18) CInt Function


Dim val
val="100"
Msgbox VarType(val) '8 for String

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

19) VarType Function

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

Msgbox VarType(x) '0 for Uninitialized

Msgbox VarType(#10/10/2010#) '7 for Date format

Set x=CreateObject("Scripting.FileSystemObject")
Msgbox VarType(x) '9 for Automation Object

20) LCase Function


Dim val
val="HYDERABAD"
Msgbox LCase(val) 'hyderabad

val="HyderABAD"
Msgbox LCase(val) 'hyderabad

val="hyderabad"

Msgbox LCase(val) 'hyderabad

val="100"
Msgbox LCase(val) '100

val=100
Msgbox LCase(val) '100

21) UCase Function

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

22) Trim Function

23) LTrim Function

24) RTrim Function

Dim val
val=" vb script "
Msgbox val
Msgbox Trim(val)

val=" vb script "


Msgbox val
Msgbox LTrim(val)

val=" vb script "


Msgbox val
Msgbox RTrim(val)

25) Date Function

26) Time Function

27) Now Function

Dim val
val=Date
Msgbox val

val=Time
Msgbox val

val=Now
Msgbox val

val= Date&" "&Time


Msgbox val

val= Time&" "&Date


Msgbox val

28) Timer Function

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

29) CreateObject Function

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

''Create a Flat file


Dim objFso
Set objFso=CreateObject("Scripting.FileSystemObject")
objFso.CreateTextFile("C:\Documents and Settings\Administrator\Desktop\abc.txt")

objFso.CreateTextFile("C:\Documents and Settings\Administrator\Desktop\abc.doc")


objFso.CreateTextFile("C:\Documents and Settings\Administrator\Desktop\abc.xls")
objFso.CreateTextFile("C:\Documents and Settings\Administrator\Desktop\abc.pdf")

'Write Data to a Text file


''Create a Flat file
Dim objFso, myFile
Set objFso=CreateObject("Scripting.FileSystemObject")
Set myFile=objFso.OpenTextFile("C:\Documents and
Settings\Administrator\Desktop\abc.txt",2) '1-Read, 2-Write, 8- Append
Result=10+23
myFile.WriteLine "Result is: " &Result
myFile.Close

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

You might also like