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
Dim a
a = 1
b 2
MsgBox a + b
Function Sum( nNum1, nNum2)
If IsNumeric(nNum1) = False Or IsNumeric(nNum2) =
False Then
On Error Resume Next
Err.Raise vbObjectError + 100, "Sum
Function", _
"One or more parameters are invalid."
Exit Function
End If
Sum = nNum1 + nNum2
End Function
Call Sum(gcr,bannu)
e) Error Object
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
No comments:
Post a Comment