Working around Selenium

After you’ve used selenium for a while, you’ll be familiar with some of the problems.

Yep, WaitForElement doesn’t exactly cover itself in glory. It is possible to work round this a bit using a static helper for retries. Something like this;

    public static void MyNavigateMethod(this IWebDriver driver, int retryCounter = 0)
    {
        try
        {
            driver.DoSomething();

            // select the "Return to third party checkbox
            driver.WaitForWebElement(By.Id("myElement")).Click();
        }
        catch (Exception ex)
        {
            if (retryCounter <= 5)
            {
                // retry this again
                retryCounter++;
                Trace.WriteLine(string.Format(" - exception occurred: '{1}', retrying: {0}", retryCounter, ex.GetType().ToString()));
                Thread.Sleep(retryCounter * retrySleep);
                driver.MyNavigateMethod(retryCounter: retryCounter);
            }
            else
            {
                Trace.Write(ex);
                throw;
            }
        }
    }

Here we have passed an optional retryCounter into the method, and then within the exception iterated this counter and get the method to call itself.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

A WordPress.com Website.

Up ↑

%d bloggers like this: