VBScript Error Handlers
In Part-1 we have discussed about Recovery Scenario and now we know that Recovery Scenarios are useful to continue the script execution whenever an error/exception/unexpected event interrupts the script execution. But Recovery Scenarios will not handle VBScript Errors. It can only handle QTP Errors.
What is an error in VBScript?
When working with VBScript you get two types of errors.
- Syntax Errors
- Runtime Errors
Syntax Errors:
Syntax error is a grammatical mistake. In that case VBScript compiler doesn’t understand your statement and throws an error. It can be found in compilation stage before the script has begun to be executed. Ex: calling a sub with parenthesis “(“.
You can find the list of syntax errors in QTP Help File (Press F1 to get) –> VBScript Reference –> VBScript –> VBScript Language Reference –> Errors (VBScript) –> VBScript Syntax Errors
Run Time Errors:
Runtime Errors shows when VBScript attempts an action that the system cannot execute. Run-time errors occur in script execution time when variable expressions are being evaluated, and memory is being dynamic allocated. Ex: When calling a function which is not there/loaded, When assigning values to an array more than allocated size.
You can find the list of syntax errors in QTP Help File (Press F1 to get) –> VBScript Reference –> VBScript –> VBScript Language Reference –> Errors (VBScript) –> VBScript Runtime Errors
How to handle VBScript Errors?
You cannot skip from syntax errors. You must fix them in order to start execution. There is no specific recovery mechanism for VBScript runtime errors. But you can suppress them.
On Error Resume Next: This statement will simply suppress the errors and executes entire script. Suppressing the error means when error occur VBScript shows popup error message, pauses execution and waits for user action. When using this statement, the error popup will not be displayed on failure and goes to the next statement for execution.
On Error Go to 0: This statement is used to disable “On Error Resume Next”.
In VBScript there is no facility to call a statement/function on error. But you can use err object to perform condition based error handling. On Error Resume Next, Go to 0 and Err objects will work for VBScript as well as for QTP Objects.
Err Object: This object captures the information about runtime errors. Using this you can always get the last error details. To access error details you can use err.number or err.description statements. We can clear and raise the runtime errors using this object.
Condition Based Error Handling
You can use this when not using recovery scenarios.
'Clear the error
err.clear
'Execute a statement
Browser("Google").Page("Google").WebButton("SignIn").click
'Verify error. If error exist, err.number contains a value. Otherwise the value is empty.
if err.number<>"" then
Reporter.Reportevent micpass,"Button Click", "Clicked on Sign In Button"
Else
'Call a recovery function here if needed
'Send error details to result
Reporter.Reportevent micfail,"Button Click", "Clicked on Sign In Button Failed"&vbnewline&"Error Details: "&err.description
End If
'Clear the error
err.clear
'Execute a statement
Browser("Google").Page("Google").WebButton("SignIn").click
'Verify error. If error exist, err.number contains a value. Otherwise the value is empty.
if err.number<>"" then
Reporter.Reportevent micpass,"Button Click", "Clicked on Sign In Button"
Else
'Call a recovery function here if needed
'Send error details to result
Reporter.Reportevent micfail,"Button Click", "Clicked on Sign In Button Failed"&vbnewline&"Error Details: "&err.description
'Clear the error from cache
err.clear
End If
'Clear the error
err.clear
'Execute a statement
Browser("Google").Page("Google").WebButton("SignIn").click
'Verify error. If error exist, err.number contains a value. Otherwise the value is empty.
if err.number<>"" then
'Call a recovery function here if needed
'Send error details to result
Reporter.Reportevent micfail,"Button Click", "Clicked on Sign In Button Failed"&vbnewline&"Error Details: "&err.description
Else
Reporter.Reportevent micpass,"Button Click", "Clicked on Sign In Button"
End If
Why VBScript Errors cannot be handled by Recovery Scenario Manager?
QTP is having a large set of Objects, Methods, Properties, statements and Keywords that are not available in VBScript. But to access QTP objects/methods/properties/statements/keywords we must use the standards of VBScript. QTP Recovery Scenario Manager is developed to handle the issues that are occurred when working with QTP Test objects. So Recovery Scenario Manager works only with the errors that are related Test Objects and It cannot handle any other errors.
QTP activate Recovery Scenarios only on below errors. Whether it is Popup/Object State/TestRun Error/Application crash QTP gets any of the below error and activates the Recovery Scenario Mechanism.
- Object Not Found
- Item in list or menu is not unique
- Item in list or menu not found
- More than one object responds to the physical description
- Object is disabled
- Object not found
- Object not visible
When to use Recovery Scenario Manager and VBScript Error Handlers?
Recovery Scenario Manager useful to handle the test object related errors and VBScript error handlers useful to handle VBScript runtime errors. We must use both in order to complete the execution.
< Error Handling and Recovery Scenarios in QTP - Part-3 will be updated soon>
Hi sudhakar, good article, however when err.number<>"" then, you should report an error. Please update your code.
ReplyDeleteGood catch. Code updated.
DeleteHi Sudhakar,
ReplyDeleteYou are doing great work. I always refer your blog.
Currentlly I am facing issue. My application takes time to load. Application comes up for seconds and then again goes down for some seconds. Once fully loaded then application works fine. I have .exist check but it is not working. Because when application comes up then .exist check passes and on next statement it fails if application goes down. I receive run error when application goes down and qtp is unable to identify object.
I have used recovery scenario for run time errors. This recovery scenario calls to function which wait for 10 min ( dynamic wait). Recovery scenario working fine. Now issue is in result script status is failed due to run error. I am able to run script using recovery scenario but result is shown as failed.
I have used on error resume next to supress run error but still result shows as failed.
Please let me know how can I make result as passed.
Please make it uregent.
Thanks in advance.
It should not give run error. If recovery scenario worked in that scenario it should give only warning. Verify your code again. Recovery scenario works only for object related error but not vbscript run erros.
Delete