Forum Discussion

pse's avatar
pse
Occasional Contributor
27 days ago

Find function with retries

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? 

  • JDR2500's avatar
    JDR2500
    Frequent Contributor

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

  • Hassan_Ballan's avatar
    Hassan_Ballan
    Regular Contributor

    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.

    • pse's avatar
      pse
      Occasional Contributor

      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.

      • Marsha_R's avatar
        Marsha_R
        Moderator

        There are built in functions for waiting for objects

        https://support.smartbear.com/testcomplete/docs/app-objects/common-tasks/waiting-process-or-window-activation.html