vb script pocedures
DESCRIPTION
VB Script Procedures/Functions; String functions, Array functions, Date & Time Functions and Math Functions.TRANSCRIPT
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").ActivateDialog("Login").WinEdit("Agent Name:").Set AgentDialog("Login").WinEdit("Password:").Set Passwordwait 1Dialog("Login").WinButton("OK").Click
If Window("Flight Reservation").Exist(10) ThenMsgbox "Login Operation Sucessful"Else
SystemUtil.CloseDescendentProcessesMsgbox "Login Failed"End If End SubCall 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").ActivateDialog("Login").WinEdit("Agent Name:").Set AgentDialog("Login").WinEdit("Password:").Set Passwordwait 1Dialog("Login").WinButton("OK").Click
If Window("Flight Reservation").Exist(10) ThenWindow("Flight Reservation").CloseLogin= "Login Operation Sucessful"Msgbox LoginElse
SystemUtil.CloseDescendentProcessesLogin= "Login Failed"
Msgbox LoginEnd If End FunctionCall Login("accd","mercury")-----------------------------------
'Write a Function to Count all Opened Browsers on desktop and close them all
Function Close_Browsers() Dim oBrowser, Browsers, TotBrowsers Set oBrowser=Description.Create oBrowser("micclass").value="Browser"
1
Visit: www.gcreddy.com for QTP Examples
Set Browsers=Desktop.ChildObjects(oBrowser)TotBrowsers=Browsers.count
For i= 0 to TotBrowsers-1 Step 1Browsers(i).closeNextEnd FunctionCall 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.countNextEnd FunctionCall 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 NextEnd FunctionCall 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 FunctionCall Count_objects("WinButton")Call Count_objects("WinEdit")Call Count_objects("WinRadioButton")Call Count_objects("WinComboBox")-----------------------------------------------------------------
Function Launch_App()SystemUtil.Run "C:\Program Files\Internet Explorer\IEXPLORE.EXE","www.jjperfumes.com"Wait (8)
2
Visit: www.gcreddy.com for QTP Examples
Browser_Name = Browser("JJ Perfumes-Discount perfume").GetROProperty("title")If Browser_Name= "JJ Perfumes-Discount perfume cheap brand name perfumes, fragrance & cologne online" ThenLaunch_App="Application Launched Sucessfully"Msgbox Launch_AppElseLaunch_App="Application Not Launched"Msgbox Launch_AppEnd IfEnd 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").ClickmyBrowser.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").ClickConfirm_Message = Browser("JJ Perfumes-Discount perfume").Page("JJ Perfumes-Discount perfume_3").WebElement("Registered successfully.").GetROProperty("innertext")
If Confirm_Message="Registered successfully. " ThenRegistration="Customer Registerd Sucessfully"Msgbox RegistrationElse
Registration="Registration Failed"Msgbox Registration
End IfEnd Function--------------------------------Function Login(Email, Password)Browser("JJ Perfumes-Discount perfume").Page("JJ Perfumes-Discount perfume_3").Link("Login").ClickBrowser("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 PasswordBrowser("JJ Perfumes-Discount perfume").Page("JJ Perfumes-Discount perfume_3").WebButton("Login").Click
3
Visit: www.gcreddy.com for QTP Examples
If Browser("JJ Perfumes-Discount perfume").Page("JJ Perfumes-Discount perfume_4").Link("Logout").Exist(5) Then
Login="Login Operation Sucessful"Msgbox LoginElse
Login="Login Operation Failed"Msgbox LoginEnd If
End Function-----------------------------------------------------
Built in Functions-------------------1) Asc Function
Dim valval="Hyderabad"Msgbox Asc(val) '72
Msgbox Asc("A") '65Msgbox Asc("Z") '90Msgbox Asc("a") '97Msgbox Asc("z") '122Msgbox Asc(1) '49Msgbox Asc("*") '42
2) Chr FunctionDim valval=72Msgbox Chr(val) 'H
Msgbox Chr(65) 'AMsgbox Chr(90) 'ZMsgbox Chr(97) 'aMsgbox Chr(122) 'zMsgbox Chr(49) '49 Msgbox Chr (42) '*
3) Abs FunctionDim valval=100.49Msgbox Abs(val)' 100.49
val=100.59Msgbox Abs(val) '100.59
val=-100.49Msgbox Abs(val) '100.49
4) Round FunctionDim valval=100.49Msgbox Round(val)' 100
val=100.59
4
Visit: www.gcreddy.com for QTP Examples
Msgbox Round(val) '101
val=-100.49Msgbox Round(val) '-100
val=-100.59Msgbox Round(val) '-101val=100.5Msgbox Round(val) '100
5) IsArray
Dim a, b(3), c(), d(4,5)Msgbox IsArray(a) 'FalseMsgbox IsArray(b) 'TrueMsgbox IsArray(c) 'TrueMsgbox IsArray(d) 'True
6) IsNumeric
Dim valval=100Msgbox IsNumeric(val) 'Trueval=100.456Msgbox IsNumeric(val) 'Trueval="abcd"Msgbox IsNumeric(val) 'Falseval="100"Msgbox IsNumeric(val) 'True
val=#10/10/10#Msgbox IsNumeric(val) 'False
7) IsDateDim valval=#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#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
5
Visit: www.gcreddy.com for QTP Examples
val=100Msgbox IsDate(val) 'False
val="asdd"Msgbox IsDate(val) 'False
8) IsEmpty
Dim x,yx=100Msgbox IsEmpty(x) 'FalseMsgbox IsEmpty(y) 'Truey="abcd"Msgbox IsEmpty(y) 'False
y=0Msgbox IsEmpty(y) 'False
y=EmptyMsgbox IsEmpty(y) 'True
9) Array
Dim x,y
Msgbox IsArray(x) 'Falsex=Array("abcd",100, #10-10-10#)Msgbox IsArray(x) 'True
Msgbox x(1) '100
10) Split Dim x,yx="VB Script Language"Msgbox IsArray(y) 'Falsey=Split(x," ")Msgbox IsArray(y) 'True
Msgbox y(1) 'Script
11) DateDiff
Dim Date1, Date2Date1=#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#
6
Visit: www.gcreddy.com for QTP Examples
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 valval="Hyderabad"Msgbox Len(val) '9
Msgbox Len ("asd1234") '7
Msgbox Len (1000) '4
Msgbox Len (#10-10-10#) '10
Msgbox Len ("#10-10-10#") '10
Msgbox Len (#Sep-10-10#) '9Msgbox Len (#Dec-10-10#) '10
13) Left Function
Dim valval="Hyderabad"Msgbox Left(val,3) 'Hyd
Msgbox Left(9247892478,1) '9
7
Visit: www.gcreddy.com for QTP Examples
Msgbox Left("%*&y",2) '%*
14) Right Function
Dim valval="Hyderabad"Msgbox Right(val,3) 'bad
Msgbox Right(9247892478,1) '8
Msgbox Right("%*&y",2) '&y
15) Mid Function
Dim valval="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 xx="Hyderabad"Msgbox Left(x,3) 'HydMsgbox Mid(x,1,3) 'Hyd
Msgbox Right (x,3) 'badMsgbox Mid (x,7)
Msgbox Mid(x,5,3) 'rab---------------------------16) StrComp Function
Result Criteria:-----------------a) If st1>str2 then it returns 1
a) If st1<str2 then it returns -1a) If st1=str2 then it returns 0Dim val1,val2val1=100val2=100Msgbox StrComp(val1,val2) '0
val1="QTP"val2="qtp"Msgbox StrComp(val1,val2,0) '-1
val1="qtp"val2="QTP"Msgbox StrComp(val1,val2,0) '1
8
Visit: www.gcreddy.com for QTP Examples
val1="QTP"val2="qtp"Msgbox StrComp(val1,val2,1) '0------------------------------------------17) Cdbl Function
Dim valval="100.45"Msgbox VarType(val) '8 for String
val=Cdbl(val)Msgbox VarType(val) '5 for Double
18) CInt FunctionDim valval="100"Msgbox VarType(val) '8 for String
val=CInt (val)Msgbox VarType(val) '2 for Integer
val="100.45"Msgbox VarType(val) '8 for StringMsgbox val
val=CInt (val)Msgbox VarType(val) '2 for IntegerMsgbox val
19) VarType Function
Dim val, xval="Hyderabad"Msgbox VarType(val) '8 for String
val="100"Msgbox VarType(val) '8 for String
val="100.456"Msgbox VarType(val) '8 for String
val=100Msgbox VarType(val) '2 for Integer
val=100.456Msgbox 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
9
Visit: www.gcreddy.com for QTP Examples
20) LCase FunctionDim valval="HYDERABAD"Msgbox LCase(val) 'hyderabad
val="HyderABAD"Msgbox LCase(val) 'hyderabad
val="hyderabad"
Msgbox LCase(val) 'hyderabad
val="100"Msgbox LCase(val) '100
val=100Msgbox LCase(val) '100
21) UCase Function
Dim valval="HYDERABAD"Msgbox UCase(val) 'HYDERABAD
val="HyderABAD"Msgbox UCase(val) 'HYDERABAD
val="hyderabad"Msgbox UCase(val) 'HYDERABAD
val="100"Msgbox UCase(val) '100
val=100Msgbox UCase(val) '100
22) Trim Function
23) LTrim Function
24) RTrim Function
Dim valval=" vb script "Msgbox valMsgbox Trim(val)
val=" vb script "Msgbox valMsgbox LTrim(val)
val=" vb script "Msgbox valMsgbox RTrim(val)
10
Visit: www.gcreddy.com for QTP Examples
25) Date Function
26) Time Function
27) Now Function
Dim valval=DateMsgbox val
val=TimeMsgbox val
val=Now Msgbox val
val= Date&" "&TimeMsgbox val
val= Time&" "&DateMsgbox val
28) Timer Function
Variable1=Timer----------Statements-------------------Variable2=TimerVariable3=Variable2-Variable1Ex:Start_Transaction=TimerSystemUtil.Run "C:\Program Files\HP\QuickTest Professional\samples\flight\app\flight4a.exe","","C:\Program Files\HP\QuickTest Professional\samples\flight\app\","open"Dialog("Login").ActivateDialog("Login").WinEdit("Agent Name:").Set "dfgh"Dialog("Login").WinEdit("Password:").SetSecure "3c30b6b7c56ffcb6847571203c0da519da6af43e"Dialog("Login").WinButton("OK").ClickWindow("Flight Reservation").CloseEnd_Transaction=TimerTransaction_Time=End_Transaction-Start_TransactionMsgbox 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 SystemSet objFso=CreateObject("Scripting.FileSystemObject")
Dim objExcel
11
Visit: www.gcreddy.com for QTP Examples
'Creating an Automation Object in Excel Application , that can be used to perform Operations on Excel FilesSet objExcel=CreateObject("Excel.Application")
Dim objWord'Creating an Automation Object in Word Application , that can be used to perform Operations on Word DocumentsSet 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 DatabasesSet objCon=CreateObject("Adodb.Connection")
Dim objRs'Creating an Automation Object in Database RecordSet , that can be used to perform Operations on Database TablesSet objRs=CreateObject("Adodb.RecordSet")
Dim objCom'Creating an Automation Object in Database Command , that can be used to Maniplate DatabasesSet objCom=CreateObject("Adodb.Command")--------------------------------------------------------Examples:----------'Create a Folder
Dim objFsoSet 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 FolderDim objFso, myFoldermyFolder="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 folderDim objFso, myFoldermyFolder="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 folderDim objFso, myFoldermyFolder="C:\Documents and Settings\Administrator\Desktop\November"
12
Visit: www.gcreddy.com for QTP Examples
Set objFso=CreateObject("Scripting.FileSystemObject")objFso.DeleteFolder(myFolder)
'Delete a folderDim objFso, myFoldermyFolder="C:\Documents and Settings\Administrator\Desktop\November"Set objFso=CreateObject("Scripting.FileSystemObject")If objFso.FolderExists(myFolder) ThenobjFso.DeleteFolder(myFolder)End If
''Create a Flat fileDim 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 fileDim 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- AppendResult=10+23myFile.WriteLine "Result is: " &ResultmyFile.CloseSet objFso=Nothing ----------------'Caputure Buttons Name fro Login Dialog box and Export to an external flat fileDim 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- AppendmyFile.WriteLine "Button Names"myFile.WriteLine "---------------"Set oButton=Description.CreateoButton("Class Name").value="WinButton"Set Buttons=Dialog("text:=Login").ChildObjects(oButton)TotButtons=Buttons.CountFor i= 0 to TotButtons-1myButton=Buttons(i).GetRoProperty("text")myFile.WriteLine myButtonNext
myFile.CloseSet objFso=Nothing ------------------'Caputure Buttons Name fro Login Dialog box and Export to an external flat fileDim 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
13
Visit: www.gcreddy.com for QTP Examples
myFile.WriteLine "Customer Names"myFile.WriteLine "---------------"If Not Window("Flight Reservation").Exist(3) ThenSystemUtil.Run "C:\Program Files\HP\QuickTest Professional\samples\flight\app\flight4a.exe","","C:\Program Files\HP\QuickTest Professional\samples\flight\app\","open"Dialog("Login").ActivateDialog("Login").WinEdit("Agent Name:").Set "yetyyt"Dialog("Login").WinEdit("Password:").SetSecure "3c30cae3f35fe9752b50e9b98a5efed3e96b7fc7"Dialog("Login").WinButton("OK").ClickEnd If
For i =1 to 10 Step 1Window("Flight Reservation").ActivateWindow("Flight Reservation").WinButton("Button").ClickWindow("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON"Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set iWindow("Flight Reservation").Dialog("Open Order").WinButton("OK").Clickwait (1)Customer_Name = Window("Flight Reservation").WinEdit("Name:").GetROProperty("text")
myFile.WriteLine Customer_Name NextmyFile.CloseSet objFso=Nothing
14