tags:

views:

35

answers:

3

There are few scenarios which i can expect during my execution, I would like to understand the best practices of handles such run time errors.

One more question, is it worth to load all the recovery scenarios during the load of the functions or handle the exception with in the functions itself.

Kindly suggest the best practices and why you suggest we need to go for that.

A: 

The general rule is, handle exceptions where you can do something useful to handle them. If you can recover immediately, handle the exception in the function itself; otherwise, let the exception bubble up to the caller (or wrap it in a domain-specific exception that you then throw back to the caller).

This lets the function do what it can, but also lets it stop trying to make sense of things when it can't. The alternative is either to pretend there's no error (which is usually something you want to know about), or cause every little exception to bring everything to a screeching halt.

cHao
+2  A: 

In my experience with QTP, it's best to handle known exception scenarios directly in your functions, and leave the exception handling mechanism for unknown or unexpected exceptions. Recovery scenarios use additional machine resources and can slow down your tests, so only use them in a limited fashion for catching truly exceptional scenarios.

Tom E
+1  A: 

Due to naming established in QTP, beginners often confuse exceptions and GUI events.
Truly, pop-up window is not an "exception" even if it "crashes" script execution.

For catching and recovery from failed operations, I recommend using "On Error Resume Next...On Error GoTo 0" statements.

For catching GUI events, that may or may not happen, you can use QTP Recovery Scenarios, BUT, as Tom E mentioned, each activated recovery handler uses extra resources and affects QTP performance.
Best way to use only those of them you would need, and keep the rest deactivated.

A few examples.

1.Catching exceptions

This way, if you have RegEx syntax errors, execution won't stop.

Set objRegEx = New RegExp   
objRegEx.Pattern = strRegEx
On Error Resume Next
boolRC = objRegEx.Test(strSrc) 
intRC = Err.Number
On Error GoTo 0
If intRC <> 0 Then boolRC = False
Set objRegEx = Nothing

2.Dynamically operating Recovery handlers

intPos = Recovery.GetScenarioPosition("API\Exceptions\AppExceptions.qrs", "Recovery_on_Error1")  
''# You can store intPos (position in QTP's qrs file) for all handlers during initialization

Recovery.SetScenarioStatus intPos, boolState ''# Parameterize boolState as True or False  
''# Enable or disable handlers this way. Disabled handler does not consume QTP resources.

Thank you,
Albert Gareev
http://automation-beyond.com/

Albert Gareev