Forum Discussion

hrothrock's avatar
hrothrock
Contributor
5 years ago
Solved

Using FindChildEx with regexp

I am writing a function to find a child object based on contentText. The items that I have are named similarly, and in my case I have one called "Dispatch 2" and one called "Dispatch 24". If I use text only to find the child and use "Dispatch 2" as my text, it chooses the Dispatch 24 object, every time. I am trying to use regexp to force it to only match exactly what I type, but I am running into several problems there. For some of the ways I wrote the code, it still finds Dispatch 24 instead of Dispatch 2. For others, it finds no object at all. For yet others, it finds the Dispatch 2 line but in order to find it, must go too many levels deep and then I can't click on the checkbox because it is on the same level as the child object found.  

 

The contentText is: 

Dispatch 2
No Jobs Assigned

 

In some cases, the "No Jobs Assigned" section could be different so I would like to wildcard that part without wildcarding the name of the applicator (Dispatch 2). 

I have tried many different combinations in this code with no luck. Here are some that I have tried:
FindChildEx("contentText", "regexp:Dispatch 2", 0)
- finds Dispatch 24

FindChildEx("contentText", "regexp:Dispatch 2*", 0)

- finds Dispatch 24

FindChildEx("contentText", "regexp:Dispatch 2$", 0)

- object does not exist

FindChildEx("contentText", "regexp:Dispatch 2$", 1)

- finds Dispatch 2, at the text node level, 1 level too deep for me to be able to click the checkbox

FindChildEx("contentText", "regexp:^Dispatch 2", 0)

- finds Dispatch 24

FindChildEx("contentText", "regexp:^Dispatch 2$", 0)

 - object does not exist unless I change the 0 at the end to a 1, and then I am 1 level too deep

FindChildEx("contentText", "regexp:^Dispatch 2*?", 0)

- object does not exist

  • How's your relationship with the coders?  This is, honestly, what happens when coders write code to just get it done as "elegantly" as possible without considering things further down the pipeline.  They, many times, don't think about what some of their "dynamic" object creation does to automation efforts.

     

    See if they can add an attribute to the objects that is a unique identifier.  You can then use that attribute as the property for your search.

     

    On those lines, that's something else you can look into.  There are properties of onscreen objects that are, themselves, objects with additional properties.  css, attributes, etc., are all accessible via TestComplete and can be utilized for object identification.

9 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    Note that in all of these, you're not actually giving a timeout.  It could be that one of them works but the object is not immediately available at the time you execute FindChildEx.  There are 2 parameters after the depth parameter that include a "refresh tree" (default to false) and a "timeout" (defaults to 0).  You might want to look at this.

     

    Also... contentText might not be the best choice of property to check, at least not alone.  Is there an ObjectType property that you can add for additional searching?

     

    Also, in this case, the regex systax is the non-native (see https://support.smartbear.com/testcomplete/docs/scripting/regular-expressions.html) so double check what syntax you're using.

    • hrothrock's avatar
      hrothrock
      Contributor

      I will try adding a timeout. In all the documents I have looked at (including the one you attached) I found the explanations to be very confusing but what I got out of them was that when using the Find methods, I should be using the non-native objects.

       

      There are object types on our objects but they are almost all 'Panel'...

      • tristaanogre's avatar
        tristaanogre
        Esteemed Contributor

        You can include the "className" property as well.  

         

        Basically, just like NameMapping, the various Find functions are more efficient, more reliable, if you expand the list of properties you're using.  Searching via one property, especially contentText, can bring back more than you want (contentText includes text from the current DIV as well as all DIVs it contains so that's why you're getting some of what you're getting).  So, adding other properties (like className, ObjectType, etc), can help narrow the search parameters.

         

        The first and second parameters of FindChildEx can either be single strings (like you have) or arrays of strings... I'd look into creating arrays and searching that way rather than messing with RegEx, ESPECIALLY since contentText has the problem I've mentioned above.