Forum Discussion
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.
- SushantNfl6 years agoOccasional 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.
- karkadil6 years agoValued 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
- SushantNfl6 years agoOccasional Contributor
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.
- tristaanogre6 years agoEsteemed Contributor
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.
Related Content
- 13 years agogreendarkness