Forum Discussion

levi_bryant's avatar
levi_bryant
Contributor
12 years ago

Better way to deal with a list of WaitObjects

Sometimes I have a large list of Objects that are required to get to the object that I really need to use. Sometimes one of the objects int the list will not exist. If one of those objects does not exist then I want to handle it rather then get a missing object error. The best way so far of dealing with this is using an if statement for each one. It seems like there should be a better way of dealing with this.



Var MyObject = WPFObject("object1").WPFObject("object2").WPFObject("object3)...



If the first object is missing then all objects in list will be missing and I will get an error.

So I do a check for the first objects.



if(WaitWPFObject("object1").Exists)

      Var MyObject = WPFObject("object1").WPFObject("object2").WPFObject("object3)...



If I am lucky then if the first object exists then all objects after it will exist as well.



However, sometimes I am unlucky and the first object will exist but not the second. In that case I can do something like this.





if(WaitWPFObject("object1").Exists)

    if(WaitWPFObject("object1").WaitWPFObject("object2").Exists)

          Var MyObject = WPFObject("object1").WPFObject("object2").WPFObject("object3)...



I can keep doing this for each object that may not exist. Eventually I have a huge section of if statements. 



Is there an easier or better way to do this?


  • Hi Levi,


     


    The Wait methods require using the timeout parameter. Otherwise, they don't differ from the WPFObject methods.


    For example:




    if(WaitWPFObject("object1", 5000).Exists)


          Var MyObject = WPFObject("object1").WPFObject("object2").WPFObject("object3)...



  • simon_glet's avatar
    simon_glet
    Regular Contributor
    Hi Levi,



    Would something like this work for you ?



    function WaitForTheLastWPF()

    {

      var index = 1;

      var currentObject = WaitWPFObject("object1");


      while(currentObject.Exists)

     
    {

       
    index++;

        currentObject = eval(currentObject + ".WaitWPFObject(\"object" + index + "\");");



      }



      // now you know how deep you had to go

     
    // ...

    }





    Sincerely


  • simon_glet's avatar
    simon_glet
    Regular Contributor
    Hi Levi,



    Glad I could help !



    Would FindAllChildren (here) be a good fit to your requirement ?

    If you try it out please read the note about JScript in the "Result Value" paragraph.



    Sincerely



  • rgratis's avatar
    rgratis
    Frequent Contributor
    I often have this same issue.  It seems like Simon's suggestion in comment 3 could be altered to accept an array of object names to find.  This would be very useful in situations where you do need to wait for each child to become available or when FindAllChildren takes too long to search the object tree.
  • TanyaYatskovska's avatar
    TanyaYatskovska
    SmartBear Alumni (Retired)

    Hi Levi,


     


    The Wait methods require using the timeout parameter. Otherwise, they don't differ from the WPFObject methods.


    For example:




    if(WaitWPFObject("object1", 5000).Exists)


          Var MyObject = WPFObject("object1").WPFObject("object2").WPFObject("object3)...



  • Thank you both for your response.



    Hi Tanya,

    I am using the wait period in my calls. WaitObject("ObjectName",1000).Exists

    I forgot to add that to my original post.



    Hi Simon,

    Thanks for your algorithm. I see what your doing there and if my workflow could benefit from that it would be pretty cool. Each of the names in my object list is unique though so I wouldn't be able to just iterate through index's. I see what your doing though and that is very clever. At some point in the future that may be useful.



    I think for my situation the best option is to use a stack of If statements.



    //This is just an example. The numbers in the name are not indexs but are just there to make the names unique.

    //Typically I will just need to check that the first object in the list exists and then I can use the full path to the object I really need. Sometimes though I need to use a stacked set of if statements to verify that each object along the list is valid before I can use the object I need.



    If(WaitObject("Objectname1",1000).Exists)

        If(WaitObject("Objectname1",1000).WaitObject("Objectname2",1000).Exists)

            If(WaitObject("Objectname1",1000).WaitObject("Objectname2",1000)..Exists)

                var ObjectINeed = Object("Objectname1").Object("Objectname2").Object("Objectname3");



    ObjectINeed.Click();





    Thanks so much for your help.



    ~Levi
  • Hi Simon,



    I don't think FindAllChildren will help me in this case. The reason being that I have no idea what the property names of that object are.



    I do know the value that I am looking for though. Without success, I tried plugging in educated guesses on property names for the particular value I was looking for. Perhaps I just need to try a few more possible property names until I find the one that works. It seems there should be a better way to do it.



    If I knew the property name that contains the value I am looking for I am positive FindAllChildren would work. Also, the code that is being used to get the value of that object, uses a method call GetText() which is not even a property. I'm not sure if this particular object even contains a property with the value I'm looking for.



    I even tried putting in a wild card for the property name in hopes that it would just search all properties for the value I was looking for.







    Thanks for the tip on reading the  "Result Value" paragraph.

  • Hi Simon,



    I was going through my posts and I realized that my last reponse to you was incorrect.

    I have several posts to the forum and I confused your response as an answer to another question I posted. Please forgive me.



    That being said. Think you are correct. i think FindAllChildren may actually be a good solution to my problem here.



    I have been dealing with Find and FindAllChildren quite a bit recently as it appears to be the solution to all my problems.



    Thanks so much Simon,



    Levi