Login   /   Register

Exit an Action when a Timeout is Reached

Rate this article
     4 votes, average: 4.5 out of 54 votes, average: 4.5 out of 54 votes, average: 4.5 out of 54 votes, average: 4.5 out of 54 votes, average: 4.5 out of 5
Loading ... Loading ...
December 9th, 2008 by Meir Bar-Tal

This QTip is based on an answer I have given recently in the AdvancedQTP Forums.

It is not rare to find QTP users and developers feeling helpless while contemplating a test run session that goes on and on, seemingly till eternity, without any sign of progress. Such a frustrating experience would lead in most cases to killing the QTPro.exe process from the Task Manager (if possible), or in extreme cases (which are all too frequent) even to restarting the machine. Of course, the report data gathered so far would end-up corrupted, and precious time thus would be lost. Another aspect of this problem is that there is no guarantee that taking such drastic measures and re-running the test would yield different, more profitable, results.

It is for these reasons that QTP developers should carry in their toolbox a device or method to avoid such an awkward situation. At first this seems to be a quite complex challenge to conceive such a method, and as will be shown below it indeed requires combining deep QTP-specific knowledge with a blend of general architectural design considerations.

The problem stems from the fact that VB Scripts are processed by WSH (Windows Script Host) line-by-line. However, in order to break the operation of a QTP action arbitrarily (that is, without an error being detected, for which a recovery scenario could be used), you’d need to monitor the time taken by the action and compare it, let’s say, at each step with the timeout you define.

For example, let us suppose that an Environment variable MAX_ACTION_TIMEOUT is defined. Now, at each and every step you would like to see if the time has already reached the timeout, but this is absurd, since your scripts would be filled with tons of if statements that have nothing to do with your automated test proper.

Hence that we need to find a way to signal QTP to ExitAction, without having to recur to the aforementioned absurd (and tedious) possibility. Having said all that, I will show in what follows how is it possible to implement such a method quite (surprisingly) easily.

The method is the following:

  1. Set the environment variable MAX_ACTION_TIMEOUT with the value of X milliseconds (so if you have 40 minutes then it would be equal to 40*60*1000=2400000 msecs).

  2. In a vbs file define a global variable (another possibility is to have another environment variable) to store the actual elapsed time, e.g.:

  3. Dim INT_ACTION_STARTTIME_MSEC 
     
  4. In the same vbs as afore, write a simple function that just checks if the actual elapsed time did not exceed the timeout, as follows:

  5.    1: Public Function CheckIfActionReachedTimeout()
       2:     Dim intElapsedTimeMsec
       3:     
       4:     intElapsedTimeMsec = Timer-INT_ACTION_STARTTIME_MSEC
       5:     
       6:     If intElapsedTimeMsec > Environment("MAX_ACTION_TIMEOUT") Then
       7:         Reporter.ReportEvent, micFail, Environment("ActionName"), _
       8:         "Error: Timeout exceeded. Action aborted because it had executed for " & _
       9:         intElapsedTimeMsec & " msec (timeout: " & Environment("MAX_ACTION_TIMEOUT") & "."
      10:         CheckIfActionReachedTimeout = True
      11:         ExitAction
      12:     Else
      13:         CheckIfActionReachedTimeout = False
      14:     End If
      15: End Function

  6. At the beginning of each action, put the following statement:
     
  7. INT_ACTION_STARTTIME_MSEC = Timer

  8. Now comes the point of the whole idea. In order to avoid writing If…Then statements within your actions, you need to override the basic QTP methods you use in your project and register these new versions using RegisterUserFunc (see in the QTP reference). All your new versions of the functions would look pretty much the same. At the top of each overridden function the following statement will appear:

  9. If CheckIfActionReachedTimeout() Then Exit Function
    


And hence if the answer is positive, both the action and the function will be exited. As promised, the method is cost effective, requiring a minimal effort to implement.

Posted in Error Handling, Meir Bar-Tal's Blog

18 Responses to “Exit an Action when a Timeout is Reached”

  1. Ramesh Says:

    [+]

    Nice article Meir! Your content is absolutely helpful to us QTP users. Font size Suggestion: I kindly request you/webma... ...

  2. Meir Bar-Tal Says:

    [+]

    Thank you, Ramesh. As to the font size, it is not really a problem, since you can increase it by zooming in your browser! :) We'... ...

  3. mdavydov Says:

    [+]

    There exists an alternative way: if one is using Your call-chain implementation: to check the variable within 'Class_Terminate' ha... ...

  4. Meir Bar-Tal Says:

    [+]

    Thank you for your comment. The article only shows the basic idea, which was based on a reply I've given in the General Q&A forum... ...

  5. mdavydov Says:

    [+]

    Thanks for an answer. Actually, I did'nt have in mind to contradict the proposed solution. I just wished to share some other base ... ...

  6. Meir Bar-Tal Says:

    [+]

    1) Call-Chain: Though not the subject of this article, I will try my best to give a proper reply to your arguments. Regardless of ... ...

  7. heqingbluesky Says:

    [+]

    Another good way to exit current action. On the other side, you should know exactly how much the action need to run in order to de... ...

  8. Meir Bar-Tal Says:

    [+]

    That's true, but there's no bread without sweat. It is obvious that a timeout can be set only based upon expectations based on exp... ...

  9. mdavydov Says:

    [+]

    Thanks for a detailed reply, Meir! As for call chain and excessive re-implementation of QTP built-in methods - I think that it's ... ...

  10. eyaly Says:

    [-]

    Nice. Why not using Recovery Senario and call this function every step?

    Eyal

  11. Meir Bar-Tal Says:

    [+]

    Hi Eyal, A recovery scenario is used for exception handling like when an unexpected pop-up dialog opens or a VBScript runtime err... ...

  12. Mikhail Says:

    [-]

    How do you subtract milliseconds from seconds (Timer)?

    intElapsedTimeMsec = Timer-INT_ACTION_STARTTIME_MSEC

  13. Suresh Rao Says:

    [-]

    Hi, this is good, but why dont we use it as the recovery scenario? Did you find any difference in it?

  14. Meir Bar-Tal Says:

    [-]

    See my reply to Eyal above to the same question.

  15. Meir Bar-Tal Says:

    [+]

    Hi Mikhail, Quote: "How do you subtract milliseconds from seconds (Timer)? intElapsedTimeMsec = Timer-INT_ACTION_STARTTIME_MSEC" ... ...

  16. Mikhail Says:

    [+]

    Thanks Meir. Being a nasty validation engineer from hell ;-), I'd like to note also yet another gotcha: Timer "returns the numbe... ...

  17. Meir Bar-Tal Says:

    [+]

    Thank you Mikhail, for your comment. Being a system architect from hell ;-), I tend to miss some trivial issues related to code im... ...

  18. Meir Bar-Tal Says:

    [+]

    Hi Mikhail, thank for your comment. Being a nasty system architect from hell ;-), I tend to miss some trivial issues related to c... ...

Leave a Reply

You must be logged in to post a comment.

This article was viewed 962 times