Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a script below that refreshes the page automatically if an element is not visible using automation script, and this works but I want it to stop refreshing after a period of time e.g 20 seconds or a number of times if the element is not visible so the test will fail and so it doesn’t end up infinitely refreshing. The code below works if the element is visible but if the element is not visible, it keeps refreshing infinitely. Any idea how to resolve it please ? Thanks

C#
do {
await Page.ReloadAsync(new PageReloadOptions() { Timeout = 5000 }); } 
while (!await Page.IsVisibleAsync("input[name='elementname']"))



What I have tried:

I have tried the code below but it refreshes infinitely if the element is not visible:

C#
do {
await Page.ReloadAsync(new PageReloadOptions() { Timeout = 5000 }); } 
while (!await Page.IsVisibleAsync("input[name='elementname']"))
Posted

1 solution

Firstly if this is happening a lot, you could consider increasing the timeout to give the reload more time to complete per try.
In answer to your question, add an incrementing count and use that to trigger exit after a certain number of loops, something like this:
C#
int refreshCount = 0;
do {
    await Page.ReloadAsync(new PageReloadOptions() { Timeout = 5000 });
    refreshCount++;
} 
while (!await Page.IsVisibleAsync("input[name='elementname']") && refreshCount < 5)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900