UFT Tutorial 25

UFT Class 25 
(VBScript Conditional Statements Part-2, VBScript Loop Statements)

Usage of VBScript conditional statements
1) Execute a statement when condition is True/Simple if.

2) Execute a block of statements when condition is True.

3) Execute a block of statements when condition is True, otherwise execute another block of statements
------------------------
4) Decide among several alternates (Elseif structure)

Syntax:

If Condition Then
Statements
----------
----------
Elseif Condition Then
Statements
----------
----------
Elseif Condition Then
Statements
----------
----------
Else
Statements
----------
----------
End if
--------------------------
Example:

' Read a number and verify the range
'If the value is in between 1 and 100 then display "Value is a Small Number"
'If the value is in between 101 and 1000 then display "Value is a Medium Number"
'If the value is in between 1001 and 10000 then display "Value is a Big Number"
'If the value is more than 10000 then display Value is High Number
'Otherwise display value is either Zero or negative value
'-------------------------------------------
Dim val
val = InputBox("Enter a Value")

If IsNumeric(val) = True Then
If val >= 1 And val <=100 Then
    Msgbox "Val is a Small Number"
   
    ElseIf val > 100 And val <= 1000 Then
    Msgbox "Val is a Medium Number"
   
    ElseIf val > 1000 And val <= 10000 Then
    Msgbox "Val is a Big Number"
   
    ElseIf val > 10000 Then
    Msgbox "Val is High Number"
   
    Else
    Msgbox "Val is either Zero or Negative value"
End If
Else
Msgbox "Invalid Input"
End If
----------------------------------------------
5) Execute a block of statements when more than one condition is True (Nested if structure).

Syntax:

If Condition Then
 If Condition Then
  If Condition Then
    Statements
    ---------
    -----------
   Else
    Statements
    -----------
    -----------
  End If
 End If
End If
----------------------------
Example:
'Read a Value and verify whether the value is valid mobile number or not?
' Value should be Numeric.
' value must contain 10 digits
' Value shout start with either 9 or 8 then display Value is a valid mobile Number
'-------------------------------------------------------------
Dim val
val = InputBox ("Enter a Value")

If IsNumeric(val) = True Then
    If Len(val) = 10 Then
        If Left(val, 1) = 9 Or Left(val, 1) = 8 Then
            Msgbox "Val is a valid Mobile Number"
            Else
            Msgbox "Val is an Invalid Mobile Number"
        End If
        Else
        Msgbox "Val is not a 10 digit value"
    End If
    Else
    Msgbox "val is not a Numeric value"
End If
'Assignment
'Handle . Symbol
--------------------------------------
6) Decide among several alternates (using Select Case)

Syntax:

Select Case (Expression)

case "case1Name"
Statements
----------
----------
case "case2Name"
Statements
----------
----------
case "case3Name"
Statements
----------
----------
Case Else
Statements
----------
----------
End select
-------------------------------------
Example:
Dim num1, num2, operation
num1 = 10
num2 = 20

operation = LCase (InputBox("Enter an Operation"))

Select Case operation
    Case "add"
    Msgbox "Addition of num1, num2 is: " & num1 + num2
   
    Case "sub"
    Msgbox "Subtraction of num1, num2 is: " & num1 - num2
   
    Case "mul"
    Msgbox "Multiplication of num1, num2 is: " & num1 * num2
   
    Case "div"
    Msgbox "Division of num1, num2 is: " & num1 / num2
   
    Case Else
    Msgbox "Invalid Operation"
   
End Select
--------------------------------------------
VBScript Loop Statements

Whenever we want to execute a block of statements several times then we use Loop statements.

a) For...Next

b) While...Wend

c) Do While Or Until...Loop

d) For Each...Next
----------------------------
a) For...Next

It repeats a block of statements for a specified number of times.

Syntax:

For Counter (Variable) = Start to End Step Increment/Decrement
Statements
----------
-----------
---------
Next

Examples:
--------------
'Display "Hello UFT" message five times
For i = 1 To 5 Step 1
    Msgbox i & " Hello UFT"
Next
----------------------
'Open first 5 orders
For Order_Numer = 1 To 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 Order_Numer
wait 2
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
Next
----------------
'Open first 5 orders in reverse order
For Order_Numer = 5 To 1 Step -1
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 Order_Numer
wait 2
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
Next
----------------------------------
'Open first 5 orders except 4th order
For Order_Number = 1 To 5 Step 1
If Order_Number <> 4 Then
Window("Flight Reservation").Activate @@ hightlight id_;_460232_;_script infofile_;_ZIP::ssf1.xml_;_
Window("Flight Reservation").WinButton("Button").Click @@ hightlight id_;_67076_;_script infofile_;_ZIP::ssf2.xml_;_
Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON" @@ hightlight id_;_132634_;_script infofile_;_ZIP::ssf3.xml_;_
Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set Order_Number @@ hightlight id_;_132644_;_script infofile_;_ZIP::ssf4.xml_;_
wait 2
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click @@ hightlight id_;_132646_;_script infofile_;_ZIP::ssf5.xml_;_
End If
Next
-----------------------------------
'Open first 10 orders except 4th and 7th order
For Order_Number = 1 To 10 Step 1
If Order_Number <> 4 And Order_Number <> 7 Then
Window("Flight Reservation").Activate @@ hightlight id_;_460232_;_script infofile_;_ZIP::ssf1.xml_;_
Window("Flight Reservation").WinButton("Button").Click @@ hightlight id_;_67076_;_script infofile_;_ZIP::ssf2.xml_;_
Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON" @@ hightlight id_;_132634_;_script infofile_;_ZIP::ssf3.xml_;_
Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set Order_Number @@ hightlight id_;_132644_;_script infofile_;_ZIP::ssf4.xml_;_
wait 2
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click @@ hightlight id_;_132646_;_script infofile_;_ZIP::ssf5.xml_;_
End If
Next
----------------------------------
Loop Termination
----------------------
'Terminating For loop based on Condition
For Order_Number = 20 To 30 Step 1
Window("Flight Reservation").Activate @@ hightlight id_;_460232_;_script infofile_;_ZIP::ssf1.xml_;_
Window("Flight Reservation").WinButton("Button").Click @@ hightlight id_;_67076_;_script infofile_;_ZIP::ssf2.xml_;_
Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON" @@ hightlight id_;_132634_;_script infofile_;_ZIP::ssf3.xml_;_
Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set Order_Number @@ hightlight id_;_132644_;_script infofile_;_ZIP::ssf4.xml_;_
wait 2
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click @@ hightlight id_;_132646_;_script infofile_;_ZIP::ssf5.xml_;_

If Window("Flight Reservation").Dialog("Open Order").Dialog("Flight Reservations").Exist(3) Then @@ hightlight id_;_67196_;_script infofile_;_ZIP::ssf6.xml_;_
Window("Flight Reservation").Dialog("Open Order").Dialog("Flight Reservations").WinButton("OK").Click @@ hightlight id_;_67198_;_script infofile_;_ZIP::ssf7.xml_;_
Window("Flight Reservation").Dialog("Open Order").WinButton("Cancel").Click @@ hightlight id_;_1050220_;_script infofile_;_ZIP::ssf8.xml_;_
Window("Flight Reservation").WinButton("Button_2").Click @@ hightlight id_;_67078_;_script infofile_;_ZIP::ssf9.xml_;_
Reporter.ReportEvent 3, "Res", "Up to " & Order_Number-1 & " Orders only exist"
Exit For
End If
Next
----------------------------
b) While...wend

It repeats a block of statements while condition is True.

Note: If we are in true position then it will continue, once we meet false position
then it will come out from the loop.

Syntax:

Initialization
While Condition
Statements
---------
----------
Increment / Decrement
Wend
-----------------------------
Examples:

'Open 1 to 5 orders using While loop
OrderNo=1
While OrderNo <= 5
Window("Flight Reservation").Activate @@ hightlight id_;_460232_;_script infofile_;_ZIP::ssf1.xml_;_
Window("Flight Reservation").WinButton("Button").Click @@ hightlight id_;_67076_;_script infofile_;_ZIP::ssf2.xml_;_
Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON" @@ hightlight id_;_786986_;_script infofile_;_ZIP::ssf3.xml_;_
Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set OrderNo @@ hightlight id_;_788124_;_script infofile_;_ZIP::ssf4.xml_;_
wait 2
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click @@ hightlight id_;_394994_;_script infofile_;_ZIP::ssf5.xml_;_
OrderNo = OrderNo + 1
Wend
----------------------------------------
'Open first 5 orders in reverse order using While loop
OrderNo=5
While OrderNo >= 1
Window("Flight Reservation").Activate @@ hightlight id_;_460232_;_script infofile_;_ZIP::ssf1.xml_;_
Window("Flight Reservation").WinButton("Button").Click @@ hightlight id_;_67076_;_script infofile_;_ZIP::ssf2.xml_;_
Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON" @@ hightlight id_;_786986_;_script infofile_;_ZIP::ssf3.xml_;_
Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set OrderNo @@ hightlight id_;_788124_;_script infofile_;_ZIP::ssf4.xml_;_
wait 2
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click @@ hightlight id_;_394994_;_script infofile_;_ZIP::ssf5.xml_;_
OrderNo = OrderNo - 1
Wend
----------------------------------------------
c) Do while /Until...Loop

i) Using While Keyword

1)

Do While Condition
Statements
----------
----------
Increment/decrement
Loop

2)

Do
Statements
----------
----------
Increment/decrement
Loop While Condition

ii) Using Until Keyword

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

4)

Do
Statements
-----------
----------
---------
Increment/Decrement
Loop Until Condition
-----------------------------
Examples:

'Open first 5 orders using Do while loop
OrderNo=1
Do While OrderNo <= 5
Window("Flight Reservation").Activate @@ hightlight id_;_460232_;_script infofile_;_ZIP::ssf1.xml_;_
Window("Flight Reservation").WinButton("Button").Click @@ hightlight id_;_67076_;_script infofile_;_ZIP::ssf2.xml_;_
Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON" @@ hightlight id_;_786986_;_script infofile_;_ZIP::ssf3.xml_;_
Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set OrderNo @@ hightlight id_;_788124_;_script infofile_;_ZIP::ssf4.xml_;_
wait 2
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click @@ hightlight id_;_394994_;_script infofile_;_ZIP::ssf5.xml_;_
OrderNo = OrderNo + 1
Loop
--------------------------------
'Open first 5 orders using Do while loop
OrderNo=10
Do 
Window("Flight Reservation").Activate @@ hightlight id_;_460232_;_script infofile_;_ZIP::ssf1.xml_;_
Window("Flight Reservation").WinButton("Button").Click @@ hightlight id_;_67076_;_script infofile_;_ZIP::ssf2.xml_;_
Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON" @@ hightlight id_;_786986_;_script infofile_;_ZIP::ssf3.xml_;_
Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set OrderNo @@ hightlight id_;_788124_;_script infofile_;_ZIP::ssf4.xml_;_
wait 2
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click @@ hightlight id_;_394994_;_script infofile_;_ZIP::ssf5.xml_;_
OrderNo = OrderNo + 1
Loop While OrderNo <= 5
----------------------------------
'Terminating Do while loop
OrderNo=20
Do While OrderNo <= 30
Window("Flight Reservation").Activate @@ hightlight id_;_460232_;_script infofile_;_ZIP::ssf1.xml_;_
Window("Flight Reservation").WinButton("Button").Click @@ hightlight id_;_67076_;_script infofile_;_ZIP::ssf2.xml_;_
Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON" @@ hightlight id_;_786986_;_script infofile_;_ZIP::ssf3.xml_;_
Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set OrderNo @@ hightlight id_;_788124_;_script infofile_;_ZIP::ssf4.xml_;_
wait 2
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click @@ hightlight id_;_394994_;_script infofile_;_ZIP::ssf5.xml_;_

If Window("Flight Reservation").Dialog("Open Order").Dialog("Flight Reservations").Exist(3) Then @@ hightlight id_;_657082_;_script infofile_;_ZIP::ssf6.xml_;_
Window("Flight Reservation").Dialog("Open Order").Dialog("Flight Reservations").WinButton("OK").Click @@ hightlight id_;_198332_;_script infofile_;_ZIP::ssf7.xml_;_
Window("Flight Reservation").Dialog("Open Order").WinButton("Cancel").Click @@ hightlight id_;_1640222_;_script infofile_;_ZIP::ssf8.xml_;_
Window("Flight Reservation").WinButton("Button_2").Click @@ hightlight id_;_67078_;_script infofile_;_ZIP::ssf9.xml_;_
Reporter.ReportEvent micWarning, "Res", "Up to " & OrderNo - 1 & " Orders only Exist"
Exit Do
End if
OrderNo = OrderNo + 1
Loop
--------------------------------

0 comments:

Post a Comment