Forum Discussion

royd's avatar
royd
Regular Contributor
6 years ago
Solved

Simple do-while question

I am using do-while to wait for an object till becomes visible/available, like so -

 

let btnSave = page.FindChild("idStr", "submit", 15);

 do
   {
     aqUtils.Delay(100);
     btnSave.Refresh();
   }
 while (!btnSave.Exists);

is the following same as above?

 while (!btnSave.Exists)
 {
   Delay(100);
   btnSave.Refresh();
 }

I would really appreciate experts correcting me.

 

Dave

  • It doesn't matter...  You can't call "Refresh" on an object that doesn't exist.  So, no matter what you do, that won't work.  Here's what I would suggest.

    let btnSave, count; 
    count = 0'
     do
       {
    count++ btnSave = page.FindChildEx("idStr", "submit", 15, true, 100); } while ((!btnSave.Exists) || (count < 100));

    This will repeat the search for the object each time through the loop.  This is necessary in order to make sure that the object is properly found so that the "Exists" check can happen.  Note that I'm using "FindChildEx" as this has the timeout built in so that we don't need the delay call.

     

    Also, note that I'm adding an additional break out.  The loop will continue infinitely if the object never resolves so I added a max iteration of 100 (10 seconds) to allow for the loop to exit rather than hang your automation.

4 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    It doesn't matter...  You can't call "Refresh" on an object that doesn't exist.  So, no matter what you do, that won't work.  Here's what I would suggest.

    let btnSave, count; 
    count = 0'
     do
       {
    count++ btnSave = page.FindChildEx("idStr", "submit", 15, true, 100); } while ((!btnSave.Exists) || (count < 100));

    This will repeat the search for the object each time through the loop.  This is necessary in order to make sure that the object is properly found so that the "Exists" check can happen.  Note that I'm using "FindChildEx" as this has the timeout built in so that we don't need the delay call.

     

    Also, note that I'm adding an additional break out.  The loop will continue infinitely if the object never resolves so I added a max iteration of 100 (10 seconds) to allow for the loop to exit rather than hang your automation.

    • royd's avatar
      royd
      Regular Contributor

      Thank you Robert! :smileyvery-happy:

       

      You are always among the first to the rescue! Good to hear from you.

       

      Thanks.

       

      Dave

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi Dave,

     

    is the following same as above?

    Logically - yes, but in terms of runtime, suffixed 'while' means that the body of the loop will be executed at least once (because the check is done at the end), while with prefixed 'while' the body of the loop may be not executed at all.

    For your given code snippet, assuming that btnSave contains a reference to the UI object (that might be disposed of at the moment of execution), the first version of code will always delay execution for 100ms and refresh the reference to the UI object. The second version of code might skip these actions depending on the result of the btnSave.Exists check.

     

    P.S. Small comment to Robert's code:

    Technically, this should be identical:

    btnSave = page.FindChildEx("idStr", "submit", 15, true, 10000); // this will wait for the button to appear but not more than 10 seconds

    if (!btnSave.Exists)

      ...

     

    But the advantage of Robert's code is that it can be interrupted almost immediately by pressing Pause button to enter debug mode during runtime while in my case TestComplete will wait for the call to FindChildEx() to complete and only then the execution will be paused.

     

    So, for short delays, both code variants can be used, while for long delays (15+ sec) Robert's approach is more preferable.

     

    • royd's avatar
      royd
      Regular Contributor

      Hi Alex

       

      Thank you for the detailed explanation. It really helped me understand how it works.  Thank you. :)

       

      Dave