VBScript Tutorial 5

VBScript Tutorial 5(VBScript Functions)
 

I) VBScript User defined Functions

i) Create a Sub Procedure with no Arguments 


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 "5566695dab8b9f47c06f8ba443bdba84f0d5274e"
Dialog("Login").WinButton("OK").Click
End Sub
Call Login()
---------------------------------------------------
ii) Create a 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
wait 2
Dialog("Login").WinButton("OK").Click
End Sub
Call Login("abcd", "mercury")
---------------------------------------------------
iii) Sub Procedure with Arguments and Data Driven Testing
 

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
wait 2
Dialog("Login").WinButton("OK").Click
Window("Flight Reservation").Close
End Sub
Call Login(DataTable(1, 1), DataTable(2, 1))
---------------------------------------------------------
iv) Sub Procedure with Arguments and load Shared object repository file from Function library.
 

Sub Login (Agent, Password)
RepositoriesCollection.Add "C:\Users\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
wait 2
Dialog("Login").WinButton("OK").Click
End Sub
------------------------------------------------------
v) Sub Procedure with Arguments and Verification Points

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
wait 2
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("abc", "mercury")
-------------------------------------
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
wait 2
Dialog("Login").WinButton("OK").Click

If Window("Flight Reservation").Exist(12) Then
Window("Flight Reservation").Close
Result = "Login Successful - Passed"
Else
If Dialog("Login").Dialog("Flight Reservations").Exist(3) Then
Dialog("Login").Dialog("Flight Reservations").WinButton("OK").Click
Dialog("Login").WinButton("Cancel").Click
Result = "Login Unsuccessful - Failed"
End If
End If
Msgbox Result
End Sub
Call Login("abc", "mercury")
----------------------------------------------
'Function Procedure with returning 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
wait 2
Dialog("Login").WinButton("OK").Click

If Window("Flight Reservation").Exist(12) Then
Window("Flight Reservation").Close
Login = "Login Successful - Passed"
Else
If Dialog("Login").Dialog("Flight Reservations").Exist(3) Then
Dialog("Login").Dialog("Flight Reservations").WinButton("OK").Click
Dialog("Login").WinButton("Cancel").Click
Login = "Login Unsuccessful - Failed"
End If
End If
End Function
res = Login("abcd", "mercury")
Msgbox res
--------------------------------------------------
Note:
If we use local (Function level) variable then we can use the value within the Function only.

If we return (Function Name) the value then we can use the value within the Function and
we can pass the value to external functions and export the value to external files.
------------------------------------------------------------
'Function Procedure with returning multiple values
 

Function Verify_UpdateButton()
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
Verify_UpdateButton = Array(Verify1, verify2)
End Function
Result= Verify_UpdateButton()
Msgbox Result(0)
Msgbox Result(1)
-----------------------------------------------------
'Calling a Function within the Function
 

Function OpenOrder(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
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
End Function
Call OpenOrder(7)
-----------------------------------------
II) Built in Functions
 

1) Asc Function

Returns ANSI character code for first letter of a String or number.

'A to Z (65 to 90)

'a to z (97 to 122)

'0 to 9 (48 to 57)
--------------------
Ex:
Dim val
val = "ABCD"
Msgbox Asc(val) '65
Msgbox Asc("ABCD") '65
Msgbox Asc("Z") '90
Msgbox Asc("a") '97
Msgbox Asc("z") '122
Msgbox Asc(1) '49
Msgbox Asc("*") '42
---------------------------------------
2) Chr Function
Returns Character value for ANSI character code

Ex:
Msgbox Chr(65) 'A
Msgbox Chr(90) 'Z
Msgbox Chr(97) 'a
Msgbox Chr(122) 'z
Msgbox Chr(49) '1
Msgbox Chr(42) '*
------------------------------------
3) Abs Function

Returns absolute value (Positive value).

Msgbox Abs (100.34) '100.34
Msgbox Abs (100.74) '100.74
Msgbox Abs (-100.34) '100.34

-----------------------------------
4) Round Function

Rounds the value to nearest integer.

Ex:
Msgbox Round(100.34) '100
Msgbox Round(100.74) '101
Msgbox Round(-100.34) '-100
---------------------------------------
5) Array Function

It assigns Series of values at a time to a variable

ex:
Dim a
Msgbox IsArray(a) 'False
a = Array(1, 2, 3, "abcd", 2.34)
Msgbox IsArray(a) 'True
msgbox a(1) '2
Msgbox UBound (a) '4
---------------------------------
6) IsArray Function

Checks weather the Variable is Array variable or not?
Return Boolean (Logical) Result.

Ex:
Dim a, b(3), c(), d(4, 5)
Msgbox IsArray(a) 'False
Msgbox IsArray(b) 'True
Msgbox IsArray(c) 'True
Msgbox IsArray(d) 'True
----------------------------------------
7) IsNumeric

Checks weather the value is Numeric value or not?

Msgbox isNumeric(100) 'True
Msgbox isNumeric("100") 'True
Msgbox isNumeric("abcd") 'False
Msgbox isNumeric(1.345) 'True
Msgbox isNumeric(#10/10/2010#) 'false
------------------------------------
8) IsDate
Checks weather the value is Data type data or not?

Ex:
Msgbox IsDate(100) 'False
Msgbox IsDate("abcd") 'False
Msgbox IsDate(1.345) 'false
Msgbox IsDate(#10/10/2010#) 'True
Msgbox IsDate(#10/10/10#) 'True
Msgbox IsDate(#Sep/10/2010#) 'True
Msgbox IsDate(#December/10/2010#) 'True
-------------------------------
9) IsEmpty
Checks weather the variable is empty variable or not?

Dim val
Msgbox IsEmpty(val) 'True
val = 0
Msgbox IsEmpty(val) 'False
val = Empty
Msgbox IsEmpty(val) 'True
---------------------------------------
10) Date Function

Returns current System Date.

11) Time Function

Returns current System Time.

12) Now Function
Returns current System Date & Time.
----------------------------
Example:
Dim a
a = Date
Msgbox a

Msgbox Date

Msgbox Time

Msgbox Now

msgbox Date &" "& Time

Msgbox Time &" "& Date
-----------------------------------
13) Datediff Function
Returns Date difference between two dates based on interval.

It deducts first date from second date.

Ex:
Dim date1, date2
date1 = #10/10/2010#
date2 = #10/10/2012#

Msgbox Datediff("yyyy", date1, date2) & " Years"

Msgbox Datediff("q", date1, date2) & " Quarters"

Msgbox Datediff("m", date1, date2) & " Months"

Msgbox Datediff("w", date1, date2) & " Weeks"

Msgbox Datediff("d", date1, date2) & " Days"

Msgbox Datediff("h", date1, date2) & " Hours"

Msgbox Datediff("n", date1, date2) & " Minutes"

Msgbox Datediff("s", date1, date2) & " Seconds"
-------------------------------------------------------
14) Left Function
Returns a specified number of charactors from left side of a string or number.

ex:

Dim val
val = 9249012345
msgbox Left(val, 5) '92490

Msgbox Left("Hyderabad", 3) 'Hyd
Msgbox Left(123.456, 4) '123.
Msgbox Left(#10/10/2010#, 5) '10/10
--------------------------------------------------
15) Right Function

Returns a specified number of characters from right side of a string or number.

Ex:
Dim val
val = 9249012345
msgbox Right(val, 5) '12345

Msgbox Right("Hyderabad", 3) 'bad
Msgbox Right(123.456, 4) '.456
Msgbox Right(#10/10/2010#, 5) '/2010
------------------------------------------
16) Mid Function

Returns a specified number of characters from a string or number.

Ex:
Msgbox Left("Hyderabad", 3) 'Hyd
Msgbox Mid("Hyderabad", 1, 3) 'Hyd

Msgbox Right("Hyderabad", 3) 'bad
Msgbox Mid("Hyderabad", 7) 'bad

Msgbox Mid("Hyderabad", 4, 3) 'era
------------------------------------------------
17) Trim

18) LTrim

19) RTrim


Ex:
Dim val
val ="           VBScript              "
msgbox val

Msgbox Trim(val)
Msgbox LTrim(val)
Msgbox RTrim(val)
---------------------------------------------------

0 comments:

Post a Comment