VBScript User Defined Functions


User Defined Functions in VBScript
 
VBScript Functions
 

> In Structured Programming (Ex: C Language) we use Functions (Built-in and user defined).

> In Object Oriented Programming (ex: Java) we use Methods (Built-in and user defined).

> In VBScript we use Functions and Methods (Object based language).

What is Function?
 

A reusable code.

When we choose Functions?
 

Whenever we want to perform any operation multiple times then we choose Functions.

Types of Functions in VBScript:
i) Built-in Functions
    

    Array Functions
    Date & Time Functions
    String Functions
    Conversion Functions
    Math Functions
    Miscellaneous Functions

ii) User defined Functions
    

    Sub Procedures (Public/Private, Internal/External)
    Function Procedures (Public/Private, Internal/External)

Public and Private - Access Modifiers

Internal Function - Defining and Calling within the Script

External Function - Calling from external Resource (Ex: Function Library)

a) Sub Procedures:
 

Set of Statements enclosed with Sub and End Sub statements to perform an operation.

Sub Procedures take Arguments but Arguments are optional.

Sub Procedures Won’t return any value.

Syntax:

Sub ProcedureName (Arguments)
Statements
----------
--------
--------
End Sub
-------------------------------
b) Function Procedures
 

Set of Statements enclosed with Function and End Function Statements to perform an operation.

Function Procedures take Arguments but Arguments are optional.

Function Procedures can return a value.

Note: Sub Procedures are faster in execution than Function procedures.

Syntax:

Function procedurename (Arguments)
Statements
-----------
-------------
-------------
------------
End Function
--------------------------------------------------
Calling Functions

Call FunctionName(Values)
--------------------------------
VBScript User defined Function Examples:
 

1) Sub / Function procedure with No Arguments

2) Sub / Function procedure with Arguments

3) Sub / Function procedure with Arguments and Loading Shared Object Repository at run-time.

4) Sub / Function procedure with Arguments and Verification point

5) Sub / Function procedure for Data driven Testing

6) Function with Returning a Value

7) Function with Returning multiple values

8) Calling Sub procedure/Function Procedure within a Function
-----------------------------------------------------------------
1) Sub Procedure with no Arguments
 

Call Login()
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 "asdf"
Dialog("Login").WinEdit("Password:").SetSecure "5681f0587ddaa0b908ab34fb23c8b330baf5bd74"
Dialog("Login").WinButton("OK").Click
End Sub
---------------------------------------
Sub Procedure with no Arguments
 

Sub adminLogin()
SystemUtil.Run "C:\Program Files\Internet Explorer\iexplore.exe", "http://www.gcrit.com/build3/admin/"
Browser("GCR Shop").Page("GCR Shop").WebEdit("username").Set "admin"
Browser("GCR Shop").Page("GCR Shop").WebEdit("password").Set "admin@123"
Browser("GCR Shop").Page("GCR Shop").WebButton("Login").Click
End Sub
Call adminLogin()
---------------------------------------------
2) Sub Procedure with Arguments
 

Sub adminLogin(userName, Password)
SystemUtil.Run "C:\Program Files\Internet Explorer\iexplore.exe", "http://www.gcrit.com/build3/admin/"
Browser("GCR Shop").Page("GCR Shop").WebEdit("username").Set userName
Browser("GCR Shop").Page("GCR Shop").WebEdit("password").Set Password
Browser("GCR Shop").Page("GCR Shop").WebButton("Login").Click
End Sub
Call adminLogin("admin", "admin@123")
-------------------------------------------------
3) Sub / Function procedure With Loading Shared Object Repository at run-time.
 

Sub adminLogin(userName, Password)
RepositoriesCollection.Add "C:\Users\G C REDDY\Desktop\webapp.tsr"
SystemUtil.Run "C:\Program Files\Internet Explorer\iexplore.exe", "http://www.gcrit.com/build3/admin/"
Browser("GCR Shop").Page("GCR Shop").WebEdit("username").Set userName
Browser("GCR Shop").Page("GCR Shop").WebEdit("password").Set Password
Browser("GCR Shop").Page("GCR Shop").WebButton("Login").Click
End Sub
Call adminLogin("admin", "admin@123")
---------------------------------------------------------
4) Sub / Function procedure with Arguments and Verification point/s
 

Sub adminLogin(userName, Password)
RepositoriesCollection.Add "C:\Users\G C REDDY\Desktop\webapp.tsr"
SystemUtil.Run "C:\Program Files\Internet Explorer\iexplore.exe", "http://www.gcrit.com/build3/admin/"
Browser("GCR Shop").Page("GCR Shop").WebEdit("username").Set userName
Browser("GCR Shop").Page("GCR Shop").WebEdit("password").Set Password
Browser("GCR Shop").Page("GCR Shop").WebButton("Login").Click

If Browser("GCR Shop").Page("GCR Shop").Link("Logoff").Exist(5) Then
Reporter.ReportEvent micPass, "Res", "Login Successful"
Else
Reporter.ReportEvent micFail, "Res", "Login Failed"
End if

End Sub

Call adminLogin("admin1", "admin@123")
----------------------------------------------------------
5) Sub / Function procedure for Data driven Testing
 

Sub adminLogin(userName, Password)
RepositoriesCollection.Add "C:\Users\G C REDDY\Desktop\webapp.tsr"
SystemUtil.Run "C:\Program Files\Internet Explorer\iexplore.exe", "http://www.gcrit.com/build3/admin/"
Browser("GCR Shop").Page("GCR Shop").WebEdit("username").Set userName
Browser("GCR Shop").Page("GCR Shop").WebEdit("password").Set Password
Browser("GCR Shop").Page("GCR Shop").WebButton("Login").Click

If Browser("GCR Shop").Page("GCR Shop").Link("Logoff").Exist(5) Then
Reporter.ReportEvent micPass, "Res", "Login Successful"
Else
Reporter.ReportEvent micFail, "Res", "Login Failed"
End if
Browser("GCR Shop").Close

End Sub
Call adminLogin(DataTable(1, 1), DataTable(2, 1))
----------------------------------------------------------------
6) Function 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(10) Then
Window("Flight Reservation").Close
Login = "Login Successful"
Else
SystemUtil.CloseDescendentProcesses
Login = "Login Failed"
End if
End Function

x = Login("abcd", "mercur")
Msgbox x
----------------------------------------------
'Function with Returning a Value
 

Function adminLogin(userName, password)
SystemUtil.Run "C:\Program Files\Internet Explorer\iexplore.exe", "http://www.gcrit.com/build3/admin/"
Browser("GCR Shop").Page("GCR Shop").WebEdit("username").Set userName
Browser("GCR Shop").Page("GCR Shop").WebEdit("password").Set password
Browser("GCR Shop").Page("GCR Shop").WebButton("Login").Click

If Browser("GCR Shop").Page("GCR Shop_2").Link("Logoff").exist(5) Then
adminLogin = "Login Successful"
Else
adminLogin = "Login Failed"
End If
Browser("GCR Shop").CloseAllTabs
End Function
Res = adminLogin("admin", "admin@123")
msgbox Res
----------------------------------------------------
7) Function with Returning multiple values.
 

'Verify Update Order Button in FR window
Function UpdateOrder()
Dim Before_Open, After_Open, Verify1, Verify2
Window("Flight Reservation").Activate
Before_Open = Window("Flight Reservation").WinButton("Update Order").GetROProperty("enabled")

If Before_Open = False Then
    Verify1 ="Update Button Disabled - Passed"
    Else
    Verify1 ="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 ="Update Button Enabled - Passed"
    Else
    Verify2 ="Update Button Disabled - Failed"
End If
UpdateOrder = Array(Verify1, Verify2)
End Function
Res= UpdateOrder()
Msgbox Res(0)
msgbox Res(1)
-----------------------------------------------
8) Calling Sub procedure/Function Procedure within a Function.
 

Function Open_Order(OrderNo)
Call Login("abcd", "mercury")
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 OrderNo
wait 2
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
End Function
Call Open_Order(7)
----------------------------------------------

0 comments:

Post a Comment