cancel
Showing results for 
Search instead for 
Did you mean: 

Simple do-while question

SOLVED
royd
Regular Contributor

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

1 ACCEPTED SOLUTION

Accepted Solutions
tristaanogre
Community Hero

Re: Simple do-while question

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.


Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available

View solution in original post

4 REPLIES 4
tristaanogre
Community Hero

Re: Simple do-while question

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.


Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available

View solution in original post

royd
Regular Contributor

Re: Simple do-while question

Thank you Robert! Smiley Very Happy

 

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

 

Thanks.

 

Dave

AlexKaras
Community Hero

Re: Simple do-while question

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.

 

Regards,
  /Alex [Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
================================
royd
Regular Contributor

Re: Simple do-while question

Hi Alex

 

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

 

Dave

New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors