Find function with retries

Originally posted by pse on January 15, 2025

Hi,

I am trying to design a function for object recognition that allows for retries. Here is my code snippet below:

def wait_for_object_with_retries(base_object, find_properties, find_values, depth):
attempt = 1
while attempt <= retry:
try:
base_object.Refresh()
#base_object.RefreshMappingInfo()
reference_object = None
reference_object = base_object.Find(find_properties, find_values, depth)
if not reference_object.Exists:
raise Exception(“Object not detected.”)
return reference_object
except Exception as e:
Log.Message(str(e))
Delay(1000, “Unable to find object. Waiting for timeout.”)
attempt = attempt + 1
return None

For whatever reason, the Find function’s ability to find an object given parameters is inconsistent. When something is looked for right before it appears, the code attempts to retry two more times. However, even if the new object is demonstrably on screen, the retry has the same result of not finding the object. Any recommendations?

Originally posted by JDR2500 on January 15, 2025

Hmm, looks good to me. I gather you are confident in the validity of the parameters you’re passing the function. Maybe try FindChild?

Originally posted by Hassan_Ballan on January 15, 2025

Not sure if any of my suggestions will help.

If you are using NameMapping, TestComplete rely heavily on caching and you will need to use the RefreshMappingInfo for that specific object, it’s parent, or starting from the tree top Browser.

Maybe you can try to split to two functions, one that will loop and check if exist, and to call the other function that refresh and find.

Originally posted by rraghvani on January 16, 2025

What is the intended purpose of this function?

Originally posted by pse on January 22, 2025

The goal is to find an object again if it is not found immediately. For example, if an object didn’t exist when the function is called, but appears within 3 seconds, the function needs to be able to find the object now that it has appeared.

Originally posted by Marsha_R on January 22, 2025

There are built in functions for waiting for objects

Originally posted by scot1967 on January 22, 2025

Have you tried using FindEx or FindChildEx? This will build your delay into the method and simplify your code! It also has a ‘refreshtree’ option you can enable to ensure you can find objects that are dynamically created and cause a problem with the traditional find method.

https://support.smartbear.com/testcomplete/docs/reference/test-objects/members/common-for-all/findex-method.html?sbsearch=FindEx%20method

Originally posted by rraghvani on January 23, 2025

One of my replies is missing!

If you refer to https://support.smartbear.com/testcomplete/docs/app-objects/common-tasks/waiting-process-or-window-activation.html mainly Using WaitNNN Methods. There are methods to wait for an object to appear within a given time.