The event handler GeneralEvents_OnLogError is getting executed recursively.
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()
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.
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.
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
Thanks for your reply.
But I am not sure if this will work
The requirement is to run performTask method for every item of the list.
I believe in your suggestion,the script execution would stop once any error occurs for an item(i.e. once the code enters OnLogError method).
But instead of stopping the execution, I need to continue the process with the next item in the list.
I have been able to achieve this with my code(posted in the question) but the issue that I am facing is that the GeneralEvents_OnLogError is getting executed recursively even after completing the process for all the items.
Kindly guide me how can I handle this.
Correct. UI indentification issues will not raise as a Python exception. However, a best practice is to always verify an object exists before interacting with it, especially when using "Find" methods or if you are navigating to a new page, form, etc. So, in those checks for the object, you could raise an exception manually if the object does not exist which would then trigger the exception handling.
@karkadil: The issue here is that the error occurs in performTask method after which generalevents_onlogerror is invoked and the control goes back to the step where error occured (which again causes failure since the application is closed by the onlogerror method).
I need to close then reopen the application and start the same (performTask) for the next item in the list.
Any suggestions, how can I achieve this?