WaitForExists increase search delay?

Ask general questions here.
mrt
Posts: 257
Joined: Mon Mar 16, 2020 11:31 am

WaitForExists increase search delay?

Post by mrt » Tue Aug 30, 2022 8:28 am

Dear all,

when I use the WaitForExists / WaitForNotExists method my desktop client AUT starts flashing heavily.

In this case, I know that it will take several minutes until the AUT is ready, so I would like to know if there is any possibility to tell WaitForExists that it should search for the UI element every few seconds instead of every few milliseconds.

I check the "Duration" property, but it seems not to do what I am looking for.

Any ideas?
Thank you, BR mrt

User avatar
doke
Posts: 112
Joined: Fri Mar 29, 2019 2:33 pm

Re: WaitForExists increase search delay?

Post by doke » Tue Aug 30, 2022 3:06 pm

Hi mrt,
I'm using this usercode to check status of a job in Sut, which needs an extra item to press to refresh my page ( you probably don't need this)
just added Thread.Sleep for your use case, so try and expirement wether it works:

public void RetryandWait(RepoItemInfo infoExists, RepoItemInfo atagRefresh, int secondstotry, int secondstowait )
{
var startTime = System.DateTime.UtcNow;
bool bfound = false;
while( (System.DateTime.UtcNow - startTime < TimeSpan.FromSeconds(secondstotry)) && (!bfound))
{
atagRefresh.FindAdapter<ATag>().Click();
bfound = infoExists.Exists();
Report.Log(ReportLevel.Info, "RetryandWait", "checking..." + bfound.ToString() );
Thread.Sleep (secondstowait);
}
if (!bfound) {throw new Ranorex.ElementNotFoundException("not found", null); }
}

mrt
Posts: 257
Joined: Mon Mar 16, 2020 11:31 am

Re: WaitForExists increase search delay?

Post by mrt » Tue Aug 30, 2022 4:11 pm

Thank you, I ended up with this:

Code: Select all

[UserCodeMethod]
public static void WaitForExistsDelayed(RepoItemInfo itemInfo, Duration checkEvery, Duration totalTimeout)
{
    System.Diagnostics.Stopwatch stopWatch = System.Diagnostics.Stopwatch.StartNew();

    while (!itemInfo.Exists(2000) && stopWatch.Elapsed.TotalSeconds <= totalTimeout.TimeSpan.TotalSeconds)
    {				
        Thread.Sleep(new TimeSpan(checkEvery));
    }
		
    if (stopWatch.Elapsed.TotalSeconds < totalTimeout.TimeSpan.TotalSeconds)
        Report.Success("Wait", "Element '" + itemInfo + "' exists after '" + totalTimeout.TimeSpan.TotalSeconds + "' seconds.");
    else
        Report.Failure("Wait", "Wait for not exists failed after '" + totalTimeout.TimeSpan.TotalSeconds + "' seconds.");
		
    stopWatch.Stop();
}