Introduction to Error Handling using vbscript in QTP:
Unexpected events during a test run disrupt a test or may give invalid test results. For example, during a test run, an application error may occur. This error does not permit the automated test to navigate to the feature, screen, or module that needs to be tested. These unexpected errors and events are called exceptions. It becomes important
to handle these exceptions so that we are able to continue with automated
testing even in unattended mode. Handling of exception in a manner so that test
execution is uninterrupted is known as error handling.
Following are the ways in which error handling can be implemented in QTP.
• VB Script Error Handling
o Using Test Settings
o Using On Error Statement
o Using Err Object Properties
o Using Exit Statement
• Recovery Scenarios
Unexpected events during a test run disrupt a test or may give invalid test results. For example, during a test run, an application error may occur. This error does not permit the automated test to navigate to the feature, screen, or module that needs to be tested. These unexpected errors and events are called exceptions. It becomes important
to handle these exceptions so that we are able to continue with automated
testing even in unattended mode. Handling of exception in a manner so that test
execution is uninterrupted is known as error handling.
Following are the ways in which error handling can be implemented in QTP.
• VB Script Error Handling
• Recovery Scenarios
Test Settings : Error Handling
Error Handling can be defined in Test Settings through File>Settings>Run as shown below:
It defines the possible actions in case an error is encountered during run session
Test Settings for error handling |
Using On Error Statement
• On Error Statement
On Error statement, which informs the VBScript engine of intention to handle errors by self, rather than to allow the VBScript engine to display a typically uninformative error message and halt the program. This is done by inserting a statement like the following at the start of a procedure:
• On Error Resume Next:
On Error Resume Next tells the VBScript engine that, should an error occur, we want it to continue executing the program starting with the line of code that directly follows the line in which the error occurred.
On Error statement, which informs the VBScript engine of intention to handle errors by self, rather than to allow the VBScript engine to display a typically uninformative error message and halt the program. This is done by inserting a statement like the following at the start of a procedure:
• On Error Resume Next:
On Error Resume Next tells the VBScript engine that, should an error occur, we want it to continue executing the program starting with the line of code that directly follows the line in which the error occurred.
• On Error Goto 0:
This turns off the error handling
Err Object Properties and Methods
The Err object is part of the VBScript language and contains information about the last error to occur. Some of the most useful properties and method of Err object are as follows:
Err.Number Property: The Number property returns or sets a numeric value specifying an error. Number is the Err object's default property.If the value of Err.Number is 0, no error has occurred.
Err.Description Property: The Description property returns or sets a descriptive string associated with an error.
Err.Clear Method: The Clear method clears all property settings of the Err object.
Following is a code snippet how error handling can be implemented programmatically.
Public Function FunctER()
On error resume next
Lines of code
If (err.number<>0) then
Reporter.ReportEvent micFail, “Func1", err.description
Else
Reporter.ReportEvent micFail, “Func1", “Function executed”
End If
Err.clear
End Function
Using Exit Statement:
Exit Statement can be used in conjunction with err object to exit from a test/action/iteration in case of an error encountered. Following are the exit statements that can be used in QTP to exit from a particular state:
ExitTest: Exits the entire QTP test or Quality Center business process test, regardless of the run-time iteration settings.
ExitAction: Exits the current action.
ExitActionIteration: Exits the current iteration of the action.
ExitTestIteration: Exits the current iteration of the QTP test or Quality Center business process test and proceeds to the next iteration.
Following is a code snippet how error handling can be implemented programmatically.
Nice post but if you provide some examples for each, it will be very easy to understand for everyone..anyway thank you so much
ReplyDeleteThanks for liking the post. I am planning to explain everything in detail, once I get complete with Selenium articles.
ReplyDeleteregarding for query
ReplyDeletekey features of QTP at a glance
various types of properties when using Object Identification in
QTP?
http://www.bestqtptraining.com/