QTP Functions

 User Defined Functions
Basically, In Visual Basic Scripting Edition,  Functions / Procedures are two types;
1) Built-in Functions / Procedures
2) User defined Functions / Procedures

* Click on this below Image in order to get Big size and Clear Image

Examples for User Defined Procedures
I think all of you familiar with QTP Sample Application (Flight Reservations), based on that Application and www.jjperfumes.com Web Application I have written some Functions.
You upgrade your skills by Reading and Practice these GUI and Web User Defined Functions.


‘1) Login Sub Procedure with no Arguments
Sub Login()
SystemUtil.Run “C:Program FilesHPQuickTest Professionalsamplesflightappflight4a.exe”
Dialog(“Login”).Activate
Dialog(“Login”).WinEdit(“Agent Name:”).Set “asdf”
Dialog(“Login”).WinEdit(“Password:”).SetSecure “4dc0b8a8761bda26e679c0ba9ff9a4302a8eaada”
Dialog(“Login”).WinButton(“OK”).Click
End Sub
Call Login()

—————————
‘2) Login Sub Procedure with Arguments

 

Sub Login(Agent, Password)
SystemUtil.Run “C:Program FilesHPQuickTest Professionalsamplesflightappflight4a.exe”
Dialog(“Login”).Activate
Dialog(“Login”).WinEdit(“Agent Name:”).Set Agent
Dialog(“Login”).WinEdit(“Password:”).Set Password
Dialog(“Login”).WinButton(“OK”).Click
End Sub
Call Login(“abcd”,”mercury”)
———————————

‘3) Login Sub Procedure with Arguments and Data Driven Testing


Sub Login(Agent, Password)
SystemUtil.Run “C:Program FilesHPQuickTest Professionalsamplesflightappflight4a.exe”
Dialog(“Login”).Activate
Dialog(“Login”).WinEdit(“Agent Name:”).Set Agent
Dialog(“Login”).WinEdit(“Password:”).Set Password
Wait 1
Dialog(“Login”).WinButton(“OK”).Click
Window(“Flight Reservation”).Close
End Sub
Call Login(DataTable(“Agent”,1), DataTable(“Password”,1))
——————————————————-

‘4) Login Sub Procedure with Arguments , Verification Point and Error Handling


Sub Login(Agent, Password)
SystemUtil.Run “C:Program FilesHPQuickTest Professionalsamplesflightappflight4a.exe”
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(12)  Then
Window(“Flight Reservation”).Close
Msgbox “Login Operation Sucessful”
Else
SystemUtil.CloseDescendentProcesses
Msgbox “Login Failed”
End If
End Sub
Call Login(DataTable(“Agent”,1), DataTable(“Password”,1))
———————————————————–
5) ‘Login Object Repository file directly into Function
Sub Login(Agent, Password)
RepositoriesCollection.Add “Login.tsr”
SystemUtil.Run “C:Program FilesHPQuickTest Professionalsamplesflightappflight4a.exe”
Dialog(“Login”).Activate
Dialog(“Login”).WinEdit(“Agent Name:”).Set Agent
Dialog(“Login”).WinEdit(“Password:”).Set Password
Wait 1
Dialog(“Login”).WinButton(“OK”).Click
End Sub
————————————————-

ExecuteFile “abcd.vbs”
Call Login(“asdf”,”mercury”)
—————————————–

6) ‘Login Operation with returning the value


Function Login(Agent, Password)
RepositoriesCollection.Add “Login.tsr”
SystemUtil.Run “C:Program FilesHPQuickTest Professionalsamplesflightappflight4a.exe”
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(12)  Then
Window(“Flight Reservation”).Close
Login=”Login Operation Sucessful”

Else
SystemUtil.CloseDescendentProcesses
Login=”Login Failed”

End If
End Function
Res=Login(“asdf”,”mercury”)
Msgbox Res
—————————————————-

7) ‘Write a Function to Count all opened Browsers on desktop and Close them all
Function Close_Browsers()
Dim oBrowser, Browsers, TotBrowsers, i
‘Creating Collection Object
Set oBrowser=Description.Create
‘Entering Property info for Browser Object
oBrowser(“micclass”).Value=”Browser”
‘Storing Object Reference
Set Browsers=Desktop.ChildObjects(oBrowser)
TotBrowsers=Browsers.Count
Msgbox TotBrowsers

For i=0 to TotBrowsers-1 Step 1
Browsers(i).Close
Next
End Function

Call Close_Browsers()
———————————————-
8) Write a function to count and display button names from Login Dialog box

Function Count_Buttons()
Dim oButton, Buttons,  i, myButton
Set oButton=Description.Create
oButton(“Class Name”).Value=”WinButton”
Set Buttons=Dialog(“text:=Login”).ChildObjects(oButton)
msgbox Buttons.Count
For i=0 to Buttons.Count-1 Step 1
myButton=Buttons(i).GetRoproperty(“text”)
Msgbox myButton
Next
End Function
Call Count_Buttons()
————————————-
9) ‘Write a function to count specified class of objects in FR Window
Function Count_Objects(obj)
Dim oObject, Objects
Set oObject=Description.Create
oObject(“Class Name”).Value=obj
Set Objects=Window(“text:=Flight Reservation”).ChildObjects(oObject)
Msgbox Objects.Count
End Function
Call Count_Objects(“WinButton”)
Call Count_Objects(“WinEdit”)
Call Count_Objects(“WinRadioButton”)

10) ‘Write a function for Open Order Operation in QTP Sample Application (Flight Reservation)

Function Open_Order(ord)
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 ord
Window(“Flight Reservation”).Dialog(“Open Order”).WinButton(“OK”).Click
OrderNum = Window(“Flight Reservation”).WinEdit(“Order No:”).GetROProperty(“text”)
OrderNum=Cint (OrderNum)
If ord=OrderNum  Then
Open_Order=ord& ” Order Opened Sucessfully”
Msgbox Open_Order
Else
If Window(“Flight Reservation”).Dialog(“Open Order”).Dialog(“Flight Reservations”).Exist(3) Then
Window(“Flight Reservation”).Dialog(“Open Order”).Dialog(“Flight Reservations”).WinButton(“OK”).Click
Window(“Flight Reservation”).Dialog(“Open Order”).WinButton(“Cancel”).Click
End If
Open_Order=ord& ” Order Not Opened”
Msgbox Open_Order
End If
End Function
Call Open_Order(40)
—————————————

11) ‘Write a function for Update Order Operation in QTP Sample Application (Flight Reservation)


Function Update_Order (Tickets)
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 “4”
Window(“Flight Reservation”).Dialog(“Open Order”).WinButton(“OK”).Click
Window(“Flight Reservation”).WinEdit(“Tickets:”).Set Tickets
Window(“Flight Reservation”).WinButton(“Update Order”).Click
Wait (10)
Message = Window(“Flight Reservation”).ActiveX(“Threed Panel Control”).GetROProperty(“text”)

If Message=”Update Done…”  Then
Update_Order=”Order Updated Sucessfully”
Msgbox Update_Order
Else

If Window(“Flight Reservation”).Dialog(“Flight Reservations”).Exist(3) Then
Window(“Flight Reservation”).Dialog(“Flight Reservations”).WinButton(“OK”).Click
Window(“Flight Reservation”).WinButton(“Button_2″).Click
Window(“Flight Reservation”).Dialog(“Flight Reservations”).WinButton(“No”).Click
End if
Update_Order=”Order Not Updated”
Msgbox Update_Order
End If
End Function
Call Update_Order(6)——————————————-
12) ‘Write a function to Launch the Application (jjperfumes.com)


Function App_Launch()
SystemUtil.Run “C:Program FilesInternet ExplorerIEXPLORE.EXE”, “http://jjperfumes.com/index.php
Wait 10
Browser_Name = Browser(“JJ Perfumes-Discount perfume”).GetROProperty(“title”)

If  Browser_Name=”JJ Perfumes-Discount perfume cheap brand name perfumes, fragrance & cologne online” Then
App_Launch=”Application Launched Sucessfully”
Msgbox App_Launch
Else
App_Launch=”Application Not Launched”
Msgbox App_Launch
End If
End Function
Call App_Launch()
——————————————————-
13) ‘Write a function for Customer Registration Operation (jjperfumes.com)


Function Register(Email)
Browser(“JJ Perfumes-Discount perfume”).Page(“JJ Perfumes-Discount perfume”).Link(“Register”).Click
Browser(“JJ Perfumes-Discount perfume”).Page(“JJ Perfumes-Discount perfume_2″).WebList(“usertype”).Select “Retailer”
Browser(“JJ Perfumes-Discount perfume”).Page(“JJ Perfumes-Discount perfume_2″).WebEdit(“firstname”).Set “dfgdfgdg”
Browser(“JJ Perfumes-Discount perfume”).Page(“JJ Perfumes-Discount perfume_2″).WebEdit(“lastname”).Set “gfgggdg”
Browser(“JJ Perfumes-Discount perfume”).Page(“JJ Perfumes-Discount perfume_2″).WebList(“gender”).Select “Male”
Browser(“JJ Perfumes-Discount perfume”).Page(“JJ Perfumes-Discount perfume_2″).WebEdit(“address1″).Set “gdfgfdgfd”
Browser(“JJ Perfumes-Discount perfume”).Page(“JJ Perfumes-Discount perfume_2″).WebEdit(“phone”).Set “96767675679676767567
Browser(“JJ Perfumes-Discount perfume”).Page(“JJ Perfumes-Discount perfume_2″).WebEdit(“phone”).Set “96767675699676767569
Browser(“JJ Perfumes-Discount perfume”).Page(“JJ Perfumes-Discount perfume_2″).WebEdit(“email”).Set Email
Browser(“JJ Perfumes-Discount perfume”).Page(“JJ Perfumes-Discount perfume_2″).WebEdit(“city”).Set “hyderabad”
Browser(“JJ Perfumes-Discount perfume”).Page(“JJ Perfumes-Discount perfume_2″).WebList(“state”).Select “MO – MISSOURI”
Browser(“JJ Perfumes-Discount perfume”).Page(“JJ Perfumes-Discount perfume_2″).WebEdit(“zip”).Set “23456”
Browser(“JJ Perfumes-Discount perfume”).Page(“JJ Perfumes-Discount perfume_2″).WebEdit(“password”).SetSecure “4dc2093da356d966eb0f24343955″
Browser(“JJ Perfumes-Discount perfume”).Page(“JJ Perfumes-Discount perfume_2″).WebEdit(“conPass”).SetSecure “4dc20943f973bc27512088318958″
Browser(“JJ Perfumes-Discount perfume”).Page(“JJ Perfumes-Discount perfume_2″).WebButton(“Submit”).Click
Message = Browser(“JJ Perfumes-Discount perfume”).Page(“JJ Perfumes-Discount perfume_3″).WebElement(“Registered successfully.”).GetROProperty(“innertext”)

If Message=”Registered successfully. ”  Then
Register=”Customer Registered Sucessfully”
Msgbox Register
Else
Register=”Customer Registration Failed”
Msgbox Register
End If
End Function
Call Register(“[email protected]“)
—————————————————
14) ‘Write a function for Customer Registration Operation
Function Register(Email)
Set myPage=Browser(“JJ Perfumes-Discount perfume”).Page(“JJ Perfumes-Discount perfume_2″)
Browser(“JJ Perfumes-Discount perfume”).Page(“JJ Perfumes-Discount perfume”).Link(“Register”).Click
myPage.WebList(“usertype”).Select “Retailer”
myPage.WebEdit(“firstname”).Set “dfgdfgdg”
myPage.WebEdit(“lastname”).Set “gfgggdg”
myPage.WebList(“gender”).Select “Male”
myPage.WebEdit(“address1″).Set “gdfgfdgfd”
myPage.WebEdit(“phone”).Set “96767675679676767567
myPage.WebEdit(“phone”).Set “96767675699676767569
myPage.WebEdit(“email”).Set Email
myPage.WebEdit(“city”).Set “hyderabad”
myPage.WebList(“state”).Select “MO – MISSOURI”
myPage.WebEdit(“zip”).Set “23456”
myPage.WebEdit(“password”).SetSecure “4dc2093da356d966eb0f24343955″
myPage.WebEdit(“conPass”).SetSecure “4dc20943f973bc27512088318958″
myPage.WebButton(“Submit”).Click
Message = Browser(“JJ Perfumes-Discount perfume”).Page(“JJ Perfumes-Discount perfume_3″).WebElement(“Registered successfully.”).GetROProperty(“innertext”)

If Message=”Registered successfully. ”  Then
Register=”Customer Registered Sucessfully”
Msgbox Register
Else
Register=”Customer Registration Failed”
Msgbox Register
End If
End Function
Call Register(“[email protected]“)———————————————————–
15) ‘Write a function for Login Operation ((in jjperfumes.com)


Function Login(Email, Password)
Browser(“JJ Perfumes-Discount perfume”).Page(“JJ Perfumes-Discount perfume_4″).Link(“Login”).Click
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”).Set 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_5″).Link(“Logout”).Exist(10) Then

Login=”Login Sucessful”
Msgbox Login
Else
Login=”Login Failed”
Msgbox Login
End If
End Function
Call Login(“[email protected]“,”abc”)
———————————————-

For more Info Visit:

 ————————————————————


VB Script launched in 1997VB Script Derived from Visual Basic Programming Language

QTP has VB Script Engine for Scripting and SQL engine for Database Operations.

 

Comments