Skip to content

Instantly share code, notes, and snippets.

@droyad
Created September 17, 2015 04:55
Show Gist options
  • Select an option

  • Save droyad/906f6c4f1c1157548f03 to your computer and use it in GitHub Desktop.

Select an option

Save droyad/906f6c4f1c1157548f03 to your computer and use it in GitHub Desktop.
Selenium Wait
public static void WaitForCgBusyCompletion(this BasePage page)
{
HostEx.WaitUntil(() => page.FindElements(By.ClassName("cg-busy"), TimeSpan.FromMilliseconds(100)).None(e => e.Displayed), TimeSpan.FromMinutes(1));
Thread.Sleep(TimeSpan.FromMilliseconds(100));
}
public static void WaitUntil<T>(this T page, Func<T, bool> condition, TimeSpan? timeout = null) where T : Page
{
HostEx.WaitUntil(() => condition(page), timeout);
}
public static void WaitUntil(Func<bool> condition, TimeSpan? timeout = null)
{
// Always sleep for a little.
// Also there is a 200ms delay before cg-busy appears
Thread.Sleep(TimeSpan.FromMilliseconds(250));
var to = timeout ?? TimeSpan.FromSeconds(20);
var sw = new Stopwatch();
while (!condition())
{
if (sw.Elapsed > to)
throw new TimeoutException();
Thread.Sleep(TimeSpan.FromMilliseconds(100));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment