Wednesday, March 10, 2010

QTP - Where and how to use "On Error Resume Next" statement.

QTP - Where and how to use "On Error Resume Next" statement.

"On Error Resume Next" causes execution to continue with the statement immediately following the statement that caused the run-time error, or with the statement immediately following the most recent call out of the procedure containing the "On Error Resume Next" statement. This allows execution to continue despite a run-time error. You can then build the error-handling routine inline within the procedure.

An "On Error Resume Next" statement becomes inactive when another procedure is called, so you should execute an "On Error Resume Next" statement in each called routine if you want inline error handling within that routine. When a procedure is exited, the error-handling capability reverts to whatever error-handling was in place before entering the exited procedure.

Use "On Error GoTo 0" to disable error handling if you have previously enabled it using "On Error Resume Next".

It is not a good practice to use "On Error Resume Next" for entire test or project, this will suppress actual errors during execution and complicate debugging process. Just using "On Error Resume Next" will not help us, need to write proper error-handling routine.

In QTP, .Sync is the only method for which "On Error Resume Next" to be used, it is not so useful for other methods, you can use .Exist or check specific object property instead of "On Error Resume Next". Your code would get complicated if there are more error-handling routines. Enable error handling by using "On Error Resume Next" for necessary statements and disable error handling by using "On Error GoTo 0".

Err object is part of VB Script, created and updated automatically. It contain information about the last error occurred in the script. Script engine will overwrite the error information when it encounters next error, information is not preserved. Always clear the object information once the error check is completed else same error information will cause issue in the next error check.

Different methods associated with Err object
Err.number - Contain value that contain error code, Zero no error occurred.
Err.description - Descriptive message associated with the error.
Err.source - Set name of the object or application where error generated.
Err.helpcontext - Return value containing context id of the appropriate topic in the help file.
Err.helpfile - Path of help file.
Err.rise - Generate run-time error.



Attaching sample code from my Dual-Function framework

On Error Resume Next
oObject.Sync

'Error-handling routine 
If err.number = 0 Then
Else
        err.clear
        gsExecutionStatusTestCase = conF
        gsStatusMessageTestCase = gsStatusMessageTestCase & "Page " & oObject.GetTOProperty("title") & " don't exist OR Using Incorrect Keyword " & " # "
End If
On Error GoTo 0







----

No comments:

Post a Comment