Even the best Selenium test will fall victim to timing issues which will cause tests to fail some what randomly.
This class attempts to isolate the test from timing issues by performing an automatic retry.
public static class SeleniumRetry
{
public static void Invoke<TArg>(Action<TArg> action, TArg arg)
{
Invoke(() => action.Invoke(arg));
}
public static void Invoke<TArg1, TArg2>(Action<TArg1, TArg2> action2, TArg1 arg1, TArg2 arg2)
{
Invoke(() => action2.Invoke(arg1, arg2));
}
public static void Invoke(Action action)
{
var attempt = 0;
const int maxAttempts = 5;
while (true)
{
try
{
action.Invoke();
break;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
if (++attempt > maxAttempts)
throw;
Thread.Sleep(500);
}
}
}
}
Using the class is simple:
This scenario shows how to automate a dynamically loaded login form that is created when the PopupLogon link is clicked.
SeleniumRetry.Invoke(Browser.Click, "PopupLogon");
SeleniumRetry.Invoke(Browser.Type, "input_email", "fake@email.com");
SeleniumRetry.Invoke(Browser.Type, "password", "12345");
//First click selects the button, second performs action.
SeleniumRetry.Invoke(Browser.Click, "submitBtn");
SeleniumRetry.Invoke(Browser.Click, "submitBtn");
No comments:
Post a Comment