Error Handling in UFT
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.
A.Error handling thru VB SCRIPTING
1.Using Test Settings
2.Using On Error Statement
3.Using Err Object Properties
4.Using Exit Statement
B.Recovery Scenario
A.Introduction to Error Handling using VBscript in QTP:
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
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.
A.Error handling thru VB SCRIPTING
1.Using Test Settings
2.Using On Error Statement
3.Using Err Object Properties
4.Using Exit Statement
B.Recovery Scenario
A.Introduction to Error Handling using VBscript in QTP:
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
Using 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 Goto 0: 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:
ExitAction: Exits the current action. ExitActionIteration: Exits the current iteration of the action.
ExitActionIteration: Exits the current iteration of the action.
ExitTest: Exits the entire QTP test or Quality Center business process test, regardless of the run-time iteration Settings.
ExitTestIteration: Exits the current iteration of the QTP test or Quality Center business process test and proceeds to the next iteration.
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: 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:
ExitAction: Exits the current action. ExitActionIteration: Exits the current iteration of the action.
ExitActionIteration: Exits the current iteration of the action.
ExitTest: Exits the entire QTP test or Quality Center business process test, regardless of the run-time iteration Settings.
ExitTestIteration: Exits the current iteration of the QTP test or Quality Center business process test and proceeds to the next iteration.
Recovery Scenario
in QTP At times when you are trying to login to your Gmail account, assume a pop-up window comes up and you will be asked to confirm your security information. This does not happen every time you login. If your test is to login to Gmail account and as soon as you enter the user ID, password, hit the Sign In button and if your QTP test is expecting to arrive at your inbox your test is going to fail if the security information screen comes up randomly.
To handle cases like this we use the ‘Recovery Scenarios”.
Steps to create Recovery Scenario in QTP:
Step #1) Go to “Resources -> Recovery scenario manager” , click on the “New Scenario” icon.
in QTP At times when you are trying to login to your Gmail account, assume a pop-up window comes up and you will be asked to confirm your security information. This does not happen every time you login. If your test is to login to Gmail account and as soon as you enter the user ID, password, hit the Sign In button and if your QTP test is expecting to arrive at your inbox your test is going to fail if the security information screen comes up randomly.
To handle cases like this we use the ‘Recovery Scenarios”.
Steps to create Recovery Scenario in QTP:
Step #1) Go to “Resources -> Recovery scenario manager” , click on the “New Scenario” icon.
Step #2) Click Next
Step #3) The trigger for this to start could be one of the following options. Choose according to your scenario. In our case I will choose, Pop-up window. The other options are self explanatory.
Step #4) Using the “Pointed hand” option, choose the window that you would want to add.
Step #5) Define the recovery option by clicking on the “Next” icon below:
Step #6) Choose one from the list. I am going to choose “Keyword or mouse operation”. The options in this screen are really easy to understand. So choose accordingly.
Step #7) I am going to go with the default settings and click Next. The recovery operation gets added to the list. If you need to add more than one recovery operation you can keep the corresponding checkbox checked and click Next. It will take you back to the screen in Step number: 5. Or if you are done, you can simply uncheck the checkbox and click on “Next”. That is what I am going to do.
Step #8) Now you will have to define the post recovery operations. All the options are as their names indicate. I am going to choose “Proceed to next step”. Click Next
Step #9) Enter the scenario name, description and click Next
Step #10) It provides a gist of your scenario. As you can see, there are 3 parts to a recovery scenario. The Trigger, Recovery operation and post recovery operations. You can choose to add this scenario to the current test or to all tests by choosing the relevant checkboxes. I am going to keep them unchecked at this point because I want to show how a tester can associate them to a test explicitly. Click “Finish”.
Step #11) The scenario we just created will appear in the list. Save and close.
Step #12) Associating the recovery scenario. Open a test, in the “Resources” pane, right click on “Associated Recovery scenarios”, right click and choose “Associate recovery scenario”. Browse for the scenario and click “Add Scenario”. The chosen scenario will appear in the list in the Resources pane.
Step #13) Also, you can go to “File->Settings->Recovery” and add the scenarios you would like. Here you can also choose the options as to how often you would like it to run. You can choose to run it, On Error, On Every Step or Never.
Step #14) The extension for a recovery scenario file is “.qrs”
This concludes our discussion on Virtual Objects and Recovery scenarios. I would recommend the tester to use various combinations of Trigger, Recovery and post recovery operations when practicing the recovery scenarios.
Step #14) The extension for a recovery scenario file is “.qrs”
This concludes our discussion on Virtual Objects and Recovery scenarios. I would recommend the tester to use various combinations of Trigger, Recovery and post recovery operations when practicing the recovery scenarios.