Exit an Action when a Timeout is Reached
Posted by admin - Dec 9, 2008 Articles, Meir Bar-Tal, QTips 0 0 Views : 881 Receive Updates For This Category
Article Tools
- Print this page
- Add Comment
- Send to Friend
- Last Updated on :
Jul 16, 2011
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:
-
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).
- In a vbs file define a global variable (another possibility is to have another environment variable) to store the actual elapsed time, e.g.:
Dim INT_ACTION_STARTTIME_MSEC - 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:
1: Public Function CheckIfActionReachedTimeout()
2: Dim intElapsedTimeMsec
3:4: intElapsedTimeMsec = Timer-INT_ACTION_STARTTIME_MSEC5: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: ExitAction12: Else
13: CheckIfActionReachedTimeout = False
14: End If
15: End Function
- At the beginning of each action, put the following statement:
INT_ACTION_STARTTIME_MSEC = Timer
- 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:
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.


