Pages

Custom Search

Error Handling in QTP


Error Handling in QTP

Error Handling:
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.

Error Preventing:

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.

Error Handling Methods in QTP and VB Script:

a)    Synchronization
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.

Parts:

GoToline

    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.

GoTo 0

    Disables enabled error handler in the current procedure and resets it to Nothing.

GoTo -1

    Disables enabled exception in the current procedure and resets it to Nothing.

Resume Next

    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 

Example1:
Dim a
a = 1
b-2
MsgBox a + b ' displays result without showing error

Example2:

Function Sum(Num1, Num2)
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."
Exit Function
End If
Sum = Num1 +Num2

End Function
 
Call Sum("gcreddy","QTP") 'Comes out without showing error
Call Sum(100,200) ' returns sum of 100,200 as 300
e)    Error Object

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.
 
The default property of the Err object is Number.

 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.
 
Example:

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

Properties

Description Property
 
HelpContext Property
 
HelpFile Property
 
Number Property
 
Source Property

Methods

Clear Method

Raise Method

f)    Exit Statement

Open 30 to 40 Orders in Flight Reservation Window, if some records not available handle the situation

Option Explicit
Dim Order_Number
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").Activate
Dialog("Login").WinEdit("Agent Name:").Set "gcreddy"
Dialog("Login").WinEdit("Password:").SetSecure "4c2e1e65bf29943393b6940f116d35231ce5fb7e"
Dialog("Login").WinButton("OK").Click
End If

For Order_Number= 30 to 40 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_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"
Exit For
End If
Next


1 comment:

Anonymous said...

Good! All would be well written:)