Showing posts with label On Error resume next. Show all posts
Showing posts with label On Error resume next. Show all posts

Friday, September 9, 2011

Recovery Scenario Manager in QTP

Recovery scenario manager is used to handle unexpected events/errors during the execution of QTP script.
This is the general definition which we are seeing for a long time. But the question that arises is when should we use Recovery scenario manager and when not? And why is recovery scenario manager not used for all the events that gets raised while QTP execution?
Recovery scenarios are used only when the event occurrence is not predictable. That is if we are not sure at what place the event may get raised, then we go for recovery scenarios. If the event occurrence is predictable, then it is always recommended to use programmatic handling by using statements such as IF conditions or On error resume next. Because using Recovery scenario manager will slower down the performance of QTP. 

For example, assume that Ads are getting raised when Application is opened in a browser or while accessing the application. Assume that sometimes they get raised immediately as soon as you open a browser and sometimes they get raised only after you are accessing something on the page. And also assume that you are not able to predict how many ads get displayed. In such situations we go for Recovery Scenarios. 

One of the other best example and common example is Printer out of Paper example. It is not possible to predict at what time Network would return the printer error. Hence Recovery scenario is used in this example because by the time the printer error message gets raised QTP might have progressed several steps before the error is displayed.
Some of the examples where we don’t go for Recovery scenarios:

The dialog box that gets prompted for remembering the password. This can be handled using If statements.

The dialog box that gets raised to overwrite the existing file during the process of saving a file.
How to create a Recovery Scenario:
Select Recovery Scenario Manager from Resources>>Recovery Scenario Manager.
Click on New to create a new Recovery Scenario and it takes the user to ‘Recovery Scenario Wizard’.
Recovery Scenario involves following steps: 

1. Define the trigger event that interrupts the test run 

Define the trigger event that interrupts the test run such as Popup window, QTP error or Application error.
2. Specify the recovery operation(s) required to continue
Specify the recovery operations that need to be performed on the trigger event such as closing the Popup through keyboard operation/mouse operation, closing the application, Calling a function or restarting windows.





3. Choose a post-recovery test run operation
Instructing QTP what should be done after performing the recovery operations on Triggered event. For example like re-executing the same step in the test or moving to next step in the test or moving to the next test or close the execution of qtp script etc.,



4. Specify a name and description for the recovery scenario
Specify the name and description for the recover scenario.






5.Specify whether to associate the recovery scenario to the current test and/or to all new tests
Specify whether to add the recovery scenario to current test or make it a default to all the new tests that are created here after.



Reference: Quick Test User guide - Defining Recovery Scenarios

Thursday, July 21, 2011

QTP - Generating Customized Logs during the execution of script

How would you generate your own customized Logs during the execution of script for Exceptions/Information cases? 

This post would provide some information and scripts to generate customized logs in QTP. First let us discuss what is the advantage in having customized logs for our scripts. 
  • Assume that you have 1000 scripts to be executed.  And you don't want your script to be stopped in between in case of any error(s) by using On Error Resume Next, but you would like to catch these exceptions and store it at some place so that you can have a look at those errors later, you can use these customized logging functions.
  • Also if you wish to add series of logging statements about the way your script is being executed based on certain inputs, you can add these logging statements where ever required in your script.
  • Once the script execution is complete, you can have a look at these log files and see if there are any errors during your script execution and see how the script ran.
For adding Logs to your script, please follow the steps given here:

1. Create a VBS file(say for example Logs.Vbs) with a Class for Log. This class needs to have a series of Sub statements for starting the Log, Ending the Log, Adding Exception and for adding Inline statements. Please have a look at the following code:

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 

Class ClsLogger

    Public v_StepName, objLogger

    'Description : Function used to initiate the log file
    'Arguments  :  sStepName(String) -- Name of the step(can be any text)

    Public Sub LogStart(ByVal sStepName)
   
        Dim objFSO
           
        v_StepName=sStepName
       
        Set objFSO = CreateObject("Scripting.FileSystemObject")
         
        Set objLogger=objFSO.OpenTextFile("C:\QTP\Scripts\Logs.txt", 8, True)
       
        objLogger.WriteLine(vbcrlf)

        objLogger.WriteLine(Now() & " '" & sStepName & "' execution has been Started" )

    End Sub


     'Description : Function used to end the log file
     'Arguments  :  N/A 


    Public Sub LogEnd()

        objLogger.WriteLine(Now() & " '" & v_StepName & "' execution has been Completed" )

        objLogger.WriteLine(vbcrlf)
       
        objLogger.Close

        SET objLogger = NOTHING

        SET objFSO = NOTHING

    End Sub

   
    'Description: Function used to capture any exception in log file that is occurred during script execution
    'Arguments 1:  sStepName(String)-- Name of the step(can be any text) 
    'Arguments 2: sException(String)-- Exception that needs to be logged 
  
    Public Sub LogException(ByVal sStepName, ByVal sException)

        objLogger.WriteLine(Now() & " Exception has been raised on step '" & sStepName & "': " & sException & Err.Description)

        objLogger.WriteLine(vbcrlf)

        Err.Clear

    End Sub
   
    'Description: Function used to write any statement in log file
    'Arguments: sLine(String)-- Statement to be written in log(can be any text)  

    Public Sub LogWrite(sLine)

        objLogger.WriteLine(Now() & " " & sLine)

        objLogger.WriteLine(vbcrlf)

    End Sub   

End Class 


''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

2. Once the logging class creation is completed, create a separate Vbs file(say for example variables.vbs) to declare the class as a variable. You Can add all your variables related to your scripts to the same Vbs file.

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Dim objScriptLogger

Set objScriptLogger=New ClsLogger

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Why do we need to write 2 separate statements as above when we can write a single statement as Dim objScriptLogger As New ClsLogger?

This is because if it is declared in a single statement, VB would treat that variable as an auto-instantiated variable and as a result where ever it is used, VB would check whether it should be instantiated which would degrade the speed by adding some additional CPU Cycles


3.Now in your Script for each Action, at the top add the following line:

objScriptLogger.LogStart "Action <Action Name> " 

4. In the same way add the following line at the end of each Action:


objScriptLogger.LogEnd

5. If you need to capture any statement in your log file for any action, use the following statement in those actions:


objScriptLogger.LogWrite "Step number 5 has been executed successfully"

6. For capturing Exceptions in Log files, use "On Error Resume Next" at the top of your action. And then use the following set of statements before LogEnd statement:

IF Err.Number<>0 Then
        objScriptLogger.LogException "Error occurred while executing <Action Name>",""
End If

Note: Make sure that you attach both these Vbs files to your actions before executing them.