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.
Comments
Post a Comment