User defined functions in UFT


VBScript User defined Functions for UFT

What is Function?

It is a reusable code.

When we choose Functions?

When ever we want perform any task or tasks multiple times then we choose functions.

Using functions we can reduce the code size.

In VBScript two types of Functions are there.

i)    Built-in Functions
ii)    User defined Functions

Note: Built-in Functions are limited and read only, but User defined functions are unlimited, we can create N number of functions.

VBScript User Defined Functions are two types

i)    Sub procedures
ii)    Function Procedures

i) Sub procedures

Set of statements enclosed with Sub and End Sub statements to perform a task or tasks

Sub procedures Take Arguments but Arguments are optional

Sub procedures Won't return any value

Syntax:

Sub ProcedureName (Arguments)
Statements
-----------
----------
-----------
End Sub

Call ProcedureName(Values for Arguments)

ii) Function Procedures

Set of statements enclosed with Function and End Function statements to perform a task or tasks.

Function Procedures take Arguments but Arguments are optional, if you don’t want arguments then use ()

Function Procedures can return a value, but returning value is not mandatory.

Syntax:

Function ProcedureName (Arguments)
Statements
-----------
----------
-----------
End Function

Call ProcedureName(Values for Arguments)

Note: Sub procedures are faster in execution than function procedures


Examples:

'Sub Procedure with no arguments (Sub Procedure for Login Functionality in Flight Reservation Application)

Sub Login()
SystemUtil.Run "C:\Program Files\HP\Unified Functional Testing\samples\flight\app\flight4a.exe"
Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set "abcd"
Dialog("Login").WinEdit("Password:").SetSecure "534fd925bc3c30f905dd8e72a312396a6431713c"
Dialog("Login").WinButton("OK").Click
Window("Flight Reservation").Close
End Sub
----------------------
Call Login() 


'Sub Procedure with Arguments

Sub Login(Agent, Password)
SystemUtil.Run "C:\Program Files\HP\Unified Functional Testing\samples\flight\app\flight4a.exe"
Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set Agent
Dialog("Login").WinEdit("Password:").Set Password
Dialog("Login").WinButton("OK").Click
Window("Flight Reservation").Close
End Sub
-----------------------
Call Login("Hyderabad", "mercury")

'Sub Procedure with Arguments and loding Shared Object Repository at Run-time

Sub Login(Agent, Password)
RepositoriesCollection.Add "C:\Documents and Settings\gcreddy\Desktop\Login.tsr"
SystemUtil.Run "C:\Program Files\HP\Unified Functional Testing\samples\flight\app\flight4a.exe"
Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set Agent
Dialog("Login").WinEdit("Password:").Set Password
Dialog("Login").WinButton("OK").Click
Window("Flight Reservation").Close
End Sub
 

'Sub Procedure and With Parameterization using Data Table

Sub Login(Agent, Password)
SystemUtil.Run "C:\Program Files\HP\Unified Functional Testing\samples\flight\app\flight4a.exe"
Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set Agent
Dialog("Login").WinEdit("Password:").Set Password
Dialog("Login").WinButton("OK").Click
Window("Flight Reservation").Close
End Sub
-------------------------
Call Login(DataTable(1, 1), DataTable(2, 1))
 

'Sub Procedure with Arguments and Verification Point
Sub Login(Agent, Password)
SystemUtil.Run "C:\Program Files\HP\Unified Functional Testing\samples\flight\app\flight4a.exe"
Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set Agent
Dialog("Login").WinEdit("Password:").Set Password
Dialog("Login").WinButton("OK").Click

If Window("Flight Reservation").Exist(12) Then
    Window("Flight Reservation").Close
    Result = "Login Successful -Passed"
    Else
    SystemUtil.CloseDescendentProcesses
    Result = "Login Unsuccessful -Failed"
End If
Msgbox Result
End Sub
----------------------------
Call Login("abcd", "mercury")

'Function Procedure with returning a value
Function Login(Agent, Password)
SystemUtil.Run "C:\Program Files\HP\Unified Functional Testing\samples\flight\app\flight4a.exe"
Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set Agent
Dialog("Login").WinEdit("Password:").Set Password
Dialog("Login").WinButton("OK").Click

If Window("Flight Reservation").Exist(12) Then
    Window("Flight Reservation").Close
    Login = "Login Successful -Passed"
    Else
    SystemUtil.CloseDescendentProcesses
    Login = "Login Unsuccessful -Failed"
End If
End Function
---------------------------------------------
Result= Login("abcd", "mercury")
Msgbox Result

Note: Result is Script level variable, it can be exported.

'Function Procedure with multiple verification Points (Returning multiple Results)

'Function Procedure to Verify the status of Update Order Button in Flight Reservation Application.
Function VerifyUpdateButton()
Dim Before_Open, After_Open, Verify1, Verify2
Window("Flight Reservation").Activate
Window("Flight Reservation").WinButton("Button_2").Click
Before_Open = Window("Flight Reservation").WinButton("Update Order").GetROProperty("enabled")

If Before_Open = False Then
    Verify1 = "1st Verification: " & "Update Button Disabled - Passed"
    Else
    Verify1 = "1st Verification: " & "Update Button Enabled - Failed"
End If

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 "1"
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
After_Open = Window("Flight Reservation").WinButton("Update Order").GetROProperty("enabled")

If After_Open = True Then
    Verify2 = "2nd Verification: " & "Update Button Enabled - Passed"
    Else
    Verify2 = "2nd Verification: " & "Update Button Disabled - Failed"
End If
VerifyUpdateButton = Array(Verify1, Verify2)
End Function

Result=VerifyUpdateButton()
Msgbox Result(0)
Msgbox Result(1)

'Write a Function For Open Order Functionality in 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
OrderNo = Window("Flight Reservation").WinEdit("Order No:").GetROProperty("text")

If Cint (OrderNo)  = ord Then
    Open_Order = ord & " Order Opened"
    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
Window("Flight Reservation").WinButton("Button_2").Click
End If
    Open_Order = ord & " Order Not Opened"
End If
Msgbox Open_Order
End Function

Call Open_Order(26)

'Write a Function for Update Order Functionality in Flight Reservation
Function Update_Order(Tickets)
Window("Flight Reservation").Activate
Window("Flight Reservation").WinButton("Button_2").Click
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 "5"
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
Window("Flight Reservation").WinEdit("Tickets:").Set Tickets
If Tickets > 10 Then
    Update_Order = "Enter up to 10 Tickets Only"
Window("Flight Reservation").WinButton("Button_2").Click
Window("Flight Reservation").Dialog("Flight Reservations").WinButton("No").Click
Else
Window("Flight Reservation").WinButton("Update Order").Click
Wait 9
Conformation_Message = Window("Flight Reservation").ActiveX("Threed Panel Control").GetROProperty("text")

If Conformation_Message = "Update Done..." Then
    Update_Order = "Order Updated -Passed"
    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 -Failed"
End If
End If
Msgbox Update_Order
End Function

Call Update_Order(1)

'Write a Function to Count and capture Button names From Login Dialog Box in Flight Reservation Application

Function Buttons_Count()
Dim oButton, Buttons, i
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
    Msgbox Buttons(i).GetRoproperty("text")
Next
End Function
Call Buttons_Count

'Write a Function to Count Links in Google Homepage

Function CountLinks()
    Dim oLink, Links, i
    Set oLink = Description.Create
    oLink("micclass").value="Link"
   
    Set Links = Browser("Google").Page("Google").ChildObjects(oLink)
    msgbox Links.Count
   
End Function

Call CountLinks () 


Function Procedure for Login Functionality with Coding Standards

'*****************************************************
'Function Name: Login
'
'Author: abcd

' Date Of Creation: 6th May 2014

'Date of Modification: NA

'Input: Agent Name and password

'Output: Flight Reserervation window

'Purpose: Login to Flight Reserervation Application
''*****************************************************
Function Login (Agent, Password)
SystemUtil.Run "C:\Program Files\HP\Unified Functional Testing\samples\flight\app\flight4a.exe"
Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set Agent
Dialog("Login").WinEdit("Password:").Set Password
wait 2
Dialog("Login").WinButton("OK").Click

If Window("Flight Reservation").Exist(12) Then
'Window("Flight Reservation").Close
Login = "Login Successful - Passed"
Else
SystemUtil.CloseDescendentProcesses
Login = "Login Unsuccessful - Failed"
End If
End Function
'******************************************************
Call Login ("abcd", "mercury")

0 comments:

Post a Comment