VBScript Tutorial 10

VBScript Tutorial 10

(Error Handling, Debugging Scripts)

Error Handling using VBScript:

Handling expected and unexpected Errors

> Expected Errors:

Whenever we use Invalid input then we can expect Errors.

Note: We use Invalid input for Negative Testing.

> Unexpected Errors

i) Resource missing

ii) Insufficient Resource

iii) Resource Response

How to Handle Errors:

i) Using Conditional Statements

ii) Using some built in Functions

iii) Using Exit Statement

iv) Using Option Explicit Statement

v) On Error Statement
Etc…

i) Using Conditional Statements

If Not Dialog(“Login”).Exist(3) Then
SystemUtil.Run “C:\Program Files\HP\Unified Functional Testing\samples\flight\app\flight4a.exe”
End If
Dialog(“Login”).Activate
Dialog(“Login”).WinEdit(“Agent Name:”).Set “asdf”
Dialog(“Login”).WinEdit(“Password:”).SetSecure “556fa5806c0b76ea4259c8e27e7c9af850b72f13”
Dialog(“Login”).WinButton(“OK”).Click
————————————————–
‘Working with multiple instance of Application

SystemUtil.Run “C:\Program Files\HP\Unified Functional Testing\samples\flight\app\flight4a.exe”
Dialog(“text:=Login”, “index:=0”).Activate
Dialog(“text:=Login”, “index:=0”).WinEdit(“attached text:=Agent Name:”).Set “asdf”
Dialog(“text:=Login”, “index:=0”).WinEdit(“attached text:=Password:”).SetSecure “556fa5806c0b76ea4259c8e27e7c9af850b72f13”
Dialog(“text:=Login”, “index:=0”).WinButton(“text:=OK”).Click
——————————————–
‘Working with multiple instantce of Application
Dialog(“text:=Login”, “index:=0”).Activate
Dialog(“text:=Login”, “index:=0”).WinEdit(“attached text:=Agent Name:”).Set “Hyderabad”
———————————
Note: Latest Application instance index is 0, oldest one index is n-1.

Browser(“CreationTime:=1”).close

We can work with multiple browsers using “CreationTime” property, latest browser creation time is n-1.

ii) Using VBScript Built in Functions

Examples:
Dim num1, num2
num1 = InputBox(“Enter Num1 value”)
num2 = InputBox(“Enter Num2 value”)

Msgbox “Addition of num1, num2 is: “& Cint (num1) + Cint (num2)
————————————-
Dim num1, num2
num1 = Cint (InputBox(“Enter Num1 value”))
num2 = Cint (InputBox(“Enter Num2 value”))

Msgbox “Addition of num1, num2 is: “& num1 + num2
—————————————————
Dim a, b
a = “abc”
b = “xyz”

If a > b Then
Msgbox “A is a Big Number”
Else
Msgbox “B is a Big Number”
———————————–
Dim a, b

a = InputBox(“Enter a Value”)
b = InputBox(“Enter b Value”)

If IsNumeric(a) = True And IsNumeric(b) Then
If Cint (a) > Cint (b) Then
Msgbox “A is a Big Number”
Else
Msgbox “B is a Big Number”
End if
Else
Msgbox “Invalid Input”
End If
————————————
‘Verify Data size and Data Type, display only 10 digit numeric value.
Dim MobileNumber

MobileNumber = InputBox(“Enter Mobile Number”)

If Len(MobileNumber) = 10 And IsNumeric(MobileNumber) = True Then
Msgbox MobileNumber
Else
Msgbox “Invalid Input”
End If
——————————————–
‘Check Data Range and accept the value is in between 21 to 35 only
Dim age
age = InputBox(“Enter Age”)

If IsNumeric (age) = True Then
If Cint(age) >= 21 And Cint(age) <= 35 Then
Msgbox “Accepted”
Else
Msgbox “Not Accepted”
End If
Else
Msgbox “Invalid Type of Data”
End If

iii) Using Exit Statement

It is used to Terminate Loops.

Ex:

Exit For

Exit Do
——————————————
‘ Terminate For loop
For OrderNumer = 20 To 30 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 OrderNumer
wait 2
Window(“Flight Reservation”).Dialog(“Open Order”).WinButton(“OK”).Click

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
Reporter.ReportEvent micWarning, “Res”, “Up to “& OrderNumer-1 &” Orders only Exist”
Exit For
End If
Next
———————————–
‘ Terminate Do loop
OrderNumer = 20
Do Until OrderNumer > 30
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 OrderNumer
wait 2
Window(“Flight Reservation”).Dialog(“Open Order”).WinButton(“OK”).Click

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
Reporter.ReportEvent micWarning, “Res”, “Up to “& OrderNumer-1 &” Orders only Exist”
Exit Do
End If
OrderNumer= OrderNumer + 1
Loop

iv) Using Option Explicit Statement

Option Explicit – It forces declaration of all variables in a Script, so that we avoid miss-spell problem.
Example:

Option Explicit
Dim Tickets, Price, Total
Tickets = 8
Price = 100
Total = Tickets * Priee
Msgbox Total

V) Using “ON Error Resume Next” statement

If any Error is there, don’t stop the Script execution, skip the error and continue.

> When to Choose:

If there is no impact on final output then we can use it.

Ex:

On Error Resume Next
VbWindow(“Form1”).Activate
VbWindow(“Form1”).VbComboBox(“Combo1”).Select “Chennai”
VbWindow(“Form1”).VbComboBox(“Combo1”).Select “Goa”
VbWindow(“Form1”).VbComboBox(“Combo1”).Select “Hyderabad”
VbWindow(“Form1”).VbComboBox(“Combo1”).Select “New Delhi”
VbWindow(“Form1”).VbComboBox(“Combo1”).Select “Mumbai”

> When Not to Choose:

If any impact on final output then don’t use it.

‘On Error Resume Next
Dim a, b
a = 100
b – 50
Msgbox a+b

Debugging Scripts

i) What is Debugging?

Locating and isolating Errors through Step by Step execution is called Debugging.

Note: Debugging Scripts is Optional.

ii) When Debugging is Required?

Scenario 1:

Script is not showing errors and providing correct Output – Debugging is not required.

Scenario 2:

Script is showing errors- Debugging is optional

Note: First correct the errors then most of time we can get correct output, otherwise
we can debug the script.

Scenario 3:
Script is not showing errors and not providing correct Output – Debugging is required.
—————————–
Whenever Script is not showing any errors and not providing correct output then
Debugging is required.

iii) How to Debug Scripts?

Using VBScript debug commands and breakpoints we can debug scripts.

Important Debug Commands:

a) Step Into (F11)
1) It starts the Script Execution.
2) It executes one statement at a time.
3) If is function call, opens the function and executes one function statement at a time.

b) Step Over (F10)
1) It executes one statement at a time.
2) If it is function call, it executes all function statements at a time.

Note: Once we open the function using “Step Into” command then “Step Over” executes
one statement(function) at a time only.

c) Step Out (Shift + F11)
1) It executes all remaining function statements at a Time.

——————
Breakpoints (F9)

To pause the Script execution.

It is for Hybrid execution.

Script Execution:

a) At a Time execution (UFT Run Command)

b) Step by Execution (VBScript debug commands)

c) Hybrid Execution (Using UFT Run command, VBScript Debug commands and Breakpoins)

Note: Debug commands and Breakpoint available in “Run” menu (UFT tool).
——————————-
Example:

Dim a, b, c
a = 10
b = 20
c = a+b
a = 5
b = 7
c = a*b
Call Login(“gcreddy”, “mercury”)
a = 20
b = 3
c = a^b
a = 5
b = 7
c = a-b
a =100
———————————–
Debugging scripts,

Follow me on social media: