Forum Discussion

SushantNfl's avatar
SushantNfl
Occasional Contributor
6 years ago

GeneralEvents_OnLogError Running Infinitely

The event handler GeneralEvents_OnLogError is getting executed recursively.

Scenario:
Have a collection of items for which 1 same process has to be performed.
But if there is any error thrown during the execution for any item, TestComplete should close and restart the application and continue the same process with the next item in the collection.

But the GeneralEvents_OnLogError  gets executed infinitely if an error occurs.
Ideally script execution should end once the main is completely executed.

Kindly let me know what could be the possible root cause of the issue?

Thanks in advance!

 

Please find the sample code below.

indexValue = 0
list = [1,2,3,5,10,8]
def main(): launchApplication() while indexValue<len(list): performTask(list(indexValue)) indexValue = indexValue + 1 Log.Message("Process Completed") def GeneralEvents_OnLogError(Sender, LogParams): global indexValue indexValue= indexValue+ 1 CloseOpenApplcaition() main()

7 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    It's generally not a good idea to do a recursive call like that in an event handler.  Basically, you have a function that is getting an error, that error triggers the event, that event then goes back and calls the same function that generates the same error which calls the event... etc.

     

    Instead of using the OnLogError event, what I would do is utilize exception handling within def main.  I'm not familiar with python but I'm certain it has that kind of handling (see https://wiki.python.org/moin/HandlingExceptions).  Wrap the code that potentially could have the error in a "try" block, handle the exception in the "exception" block.  The exception block would have the call to "CloseApplication".  Here's what I THINK it should look like:

     

    indexValue = 0
    list = [1,2,3,5,10,8]
    
    def main():
    	launchApplication()
    	
    	while indexValue<len(list):
                    try:
    		     performTask(list(indexValue))
                    except Error:
                        CloseOpenApplication()
    		indexValue = indexValue + 1
    	
    	Log.Message("Process Completed")
    	

    Basically, in the loop, if an error happens performing the task, it throws the exception.  The except block handles it and closes the application. the index is then increased as necessary and the loop continues.

    • SushantNfl's avatar
      SushantNfl
      Occasional Contributor

      Thanks a lot Robert for your detailed reply. :)

      I have a further query for the solution that you have suggested.

       

      In performTask method, if the error occurs due to some unexpected window, or any other issue where UI objects are involved, will the exception handling of python handle it?

      I believe the Python exception handling can only handle python errors and not the UI window errors.

       

      I may be completely wrong as I am new to Test Complete.

      Kindly post your thoughts on this.

      • karkadil's avatar
        karkadil
        Valued Contributor

        You are right, try...except will not handle UI errors. I'd suggest you another solution:

         

        def main():
        	while indexValue<len(list):
        		launchApplication()
        		performTask(list(indexValue))
        		indexValue = indexValue + 1
        	
        	Log.Message("Process Completed")
        	
        def GeneralEvents_OnLogError(Sender, LogParams):
            CloseOpenApplcaition()
        
        def launchApplication():
        	if(not isAppRunning()):
        		startApp()

        The main idea is that you call launchApp() function in every iteration of the while loop and start the app only if it is not started yet. In the OnLogError event handler you just close your app