Error handling refers to the anticipation, detection, and resolution of programming, application, and communications errors.
Within every Script we have to think about possible exceptions and how to handle them. Especially in the uppermost layers of the script, it is important to handle all exceptions.
Error Handling in QuickTest Professional:
QTP and VBScript give the Test Engineer some tools to handle errors and Exceptions.
A good method for using error handling is to try to prevent them.
When an error occurred, Report it in detail. When working with GUI objects, use the Window.Exist property. Every If…Then..End If statement has the Else part, the same for
Select Case. Use Case Else.
b) Exist Property
c) Recover Scenarios
d) On Error Statement
Without an On Error statement, any run-time error that occurs is fatal: an error message is displayed, and execution stops.
Whenever possible, you use structured exception handling in your code, rather than resorting to unstructured exception handling and the On Error statement.
Enables the error-handling routine that starts at the line specified in the required line argument. The line argument is any line label or line number. If a run-time error occurs, control branches to the specified line, making the error handler active. The specified line must be in the same procedure as the On Error statement, or a compile-time error will occur.
Disables enabled error handler in the current procedure and resets it to Nothing.
Disables enabled exception in the current procedure and resets it to Nothing.
Specifies that when a run-time error occurs, control goes to the statement immediately following the statement where the error occurred, and execution continues from that point. Use this form rather than On Error GoTo when accessing objects.
on Error Resume Next
a = 1
MsgBox a + b ' displays result without showing error
If IsNumeric(Num1) = False Or IsNumeric(Num2) = False Then
On Error Resume Next
Err.Raise vbObjectError + 100, "Sum Function", _
"One or more parameters are invalid."
Sum = Num1 +Num2
The Err object is an intrinsic object with global scope — there is no need to create an instance of it in your code.
The properties of the Err object are set by the generator of an error — Visual Basic, an Automation object, or the VBScript programmer.
Err.Number contains an integer and can be used by an Automation object to return an SCODE.
When a run-time error occurs, the properties of the Err object are filled with information that uniquely identifies the error and information that can be used to handle it. To generate a run-time error in your code, use the Raise method.
The Err object's properties are reset to zero or zero-length strings ("") after an On Error Resume Next statement. The Clear method can be used to explicitly reset Err.
On Error Resume Next
Err.Raise 6 ' Raise an overflow error.
MsgBox ("Error # " & CStr(Err.Number) & " " & Err.Description)
Err.Clear ' Clear the error.
Err Object Properties and Methods
If Not Window("Flight Reservation").Exist(3) Then
SystemUtil.Run "C:\Program Files\HP\QuickTest Professional\samples\flight\app\flight4a.exe","","C:\Program Files\HP\QuickTest Professional\samples\flight\app\","open"
Dialog("Login").WinEdit("Agent Name:").Set "gcreddy"
For Order_Number= 30 to 40 step 1
Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON"
Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set Order_Number
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
Reporter.ReportEvent micWarning,"Res", "Up to "& Order_Number-1 &" Order only available"