UFT Tutorial 26

UFT Class 26
(VBScript Loop Statements Part-2, VBScript Functions Part-1)
-------------------------------
VBScript Loop Statements Part-2
I) For...Next
II) While...Wend
------------------
III) Do While / Until...Loop

a) Using While keyword

It repeats a block of statements while condition is True

Syntax:

1)
Do While Condition
Statements
---------
----------
Increment/Decrement
Loop
-------------------
Example:
OrderNo = 1
Do While OrderNo <=5
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
OrderNo = OrderNo + 1
Loop
-------------------------
2)
Do
Statements
---------
----------
Increment/Decrement
Loop While Condition
-----------------
Example:
OrderNo = 1
Do
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
OrderNo = OrderNo + 1
Loop While OrderNo <=5
------------------------
b) Using Until Keyword

It repeats a block of statements until condition is True

a) Using While keyword

It repeats a block of statements while condition is True

Syntax:

3)
Do Until Condition
Statements
---------
----------
Increment/Decrement
Loop
---------------
Example:

OrderNo = 1
Do Until
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
OrderNo = OrderNo + 1
Loop
-------------------------
4)
Do
Statements
---------
----------
Increment/Decrement
Loop Until Condition
---------------
Example:
OrderNo = 1
Do
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
OrderNo = OrderNo + 1
Loop Until OrderNo > 5
------------------------
IV) For Each...Next loop
------------------------
It executes all elements in an Array.

Syntax:
For Each Element in Array
Statements
----------
----------
----------
Next

Example:
Dim a, b, res(3)
a = 10
b = 20

res(0) ="Addition of a, b is: "& a + b
res(1) ="Subtraction of a, b is: "& a - b
res(2) ="Multiplication of a, b is: "& a * b
res(3) ="Division of a, b is: "& a / b

For Each Element in res
Msgbox Element
Next
---------------------------------------
VBScript Functions

There are two types of Functions

a) Built in Functions

b) User defined Functions
-----------------------------
Categories of Built in Functions:

                1) Array Functions
                2) String Functions
                3) Date and Time Functions
                4) Conversion Functions
                5) I/O Functions
                6) Math Functions
                7) Miscellaneous Functions
-----------------------
What is Function?

It is a reusable code.

When we choose Functions?

When ever we want perform any operation several times then we choose functions.

Note: Using functions we can reduce the code size.
-------------------------------

User Defined 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:

1) Sub Procedure without 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()
2) 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")


3) 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

4) 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")


5) 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.

6) 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)
--------------------------------

0 comments:

Post a Comment