Blog to understand automation concepts in QTP, Selenium Webdriver and Manual Testing concepts

Showing posts with label Error handling. Show all posts
Showing posts with label Error handling. Show all posts

Different Ways to exit from a QTP test: Using ExitTest, AOM and LoadandRunAction

In QTP, Framework should ensure that test is exited as soon test is failed, since there is no point in going forward with execution.
To ensure proper exit from QTP test, we can either use error handling using recovery scenario or vb script. In case of error handling using vbscript, we can exit a test at runtime in following ways.


Using ExitTest Statement

ExitTest statement exits a test once executed in script.But in scripts with functions called from other functions in different libraries and in case if there is multiple nesting of functions within different libraries, ExitTest statement on execution does not stop the test execution.
To understand this, suppose there is Test A and there are 4 different libraries(vbs files) f1,f2,f,3,f4  in which functions are stored and the action in Test A calls function from library f1 which further calls function from library f2 ans so on. In such cases, sometimes exitTest does not get executed and moves to next test without exiting the test.
To exit from test in scenario where ExitTest is not working properly, we can exit from test in below ways:


 Using QTP AOM

 Use following code to exit test in case exitstatement is not working.
Dim qtAppObj
Set qtAppObj = CreateObject("quicktest.application")
qtAppObj.Test.Stop

Using LoadandRunAction

Create a test with Action  Action1. In Action , only write ExitTest. Save the test.
Now in the test, where exitTest is not working properly, instead of ExitTest, write below code:

LoadandRunAction "c:\Test1","Action1"

Once this line of code is executed, QTP will exit the test.

Please go through the below posts for understanding of :

Error Handling using Recovery scenario

When we run a test or a batch of test in QTP, tests may start failing in case application crashes or a pop up window appear during test execution. QTP needs to recover from such error and continue with execution of test script. QTP provides creating recovery scenario through recovery scenario manager so that the tests are executed uninterrupted. In this article we will discuss on recovery scenarios.

QTP enables user to create recovery scenarios and associate them with specific tests. Recovery scenarios activate specific recovery operations when trigger events occur. Recovery scenarios are intended for use only with events that cannot be predicted in advance, or for events that cannot otherwise be synchronized with a specific step in the test. 

The Recovery Scenario Manager provides a wizard that guides through the process of defining a recovery scenario, which includes a definition of an unexpected event and the operations necessary to recover the run session.

A recovery scenario consists of the following:


•       Trigger Event. The event that interrupts run session. This can be application error or a pop up window appearing

•       Recovery Operations. The operations to perform to enable QTP to continue running the test after the trigger event interrupts the run session.

•       Post-Recovery Test Run Option. The instructions on how QTP should proceed after the recovery operations have been performed

Following are the steps in creating recovery scenario using recovery scenario manager:


  • Create a new scenario
  • Define the trigger events
  • Define recovery operation
  • Define post recovery operation
  • Naming the scenario and associating with test.

 Step 1: Creating a new Scenario


Select Resources > Recovery Scenario Manager.


Step 2: Define Trigger Event

Select the required trigger Event and click on next button
A Trigger event can be one of the following:
•       Pop-up Window
•       Object State
•       Test run error
•       Application Crash

Step 3: Recovery Operation:

This page enables  to manage the collection of recovery operations in the recovery scenario. A recovery Operation can be one of the following:


•       Keyboard or mouse operation
•       Close application process
•       Function call
•       Restart Microsoft Windows




Step 4: Post-Recovery Test Run Options Page

This page enables  to define post-recovery test run options, which specify how to continue the run session after QTP has identified the event and performed all of the specified recovery operations.

Following are the options available post recovery:

•       Repeat current step and continue
•       Proceed to next step
•       Proceed to next action or component iteration
•       Proceed to next test iteration
•       Restart current test run
•       Stop the test run


Step 5:  Completing the Recovery Scenario Wizard Page

This page enables  to review a summary of the scenario settings  defined and provide option to add scenario to current test. 




Useful Info:  We have an existing recovery scenario that covers common errors and issue for Recovery . The file is available in "C:\Program Files\HP\QuickTest Professional\recovery\WebRecovery.qrs". Do have a look, this will be very useful to use existing recovey scenario for most of common error

Error Handling using vbscript in QTP

Introduction to Error Handling using  vbscript in QTP:


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.


       VB Script Error Handling

o   Using Test Settings
o   Using On Error Statement
o   Using Err Object Properties
o   Using Exit Statement

       Recovery Scenarios


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


Test Settings for error handling

Using On Error Statement


       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: 

This turns off the error handling


Err Object Properties and Methods


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 MethodThe 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:

ExitTest: Exits the entire QTP test or Quality Center business process test, regardless of the run-time iteration settings.

ExitAction: Exits the current action.

ExitActionIteration: Exits the current iteration of the action.   

ExitTestIteration:  Exits the current iteration of the QTP test or Quality Center business process test and proceeds to the next iteration.