1

My selenium test suite always has a few tests failing intermittently on each run on the firefox browser. On the next run the same tests will pass and new tests will fail. It is usually times out after 3 minutes of webdriverwait looking for an element. I also increase the wait time after clicks with thread.sleep. Any advice on how to fix it?

This is the intermittently failing line.

WebElement listing = wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Check the Listings"))); 

listing.click(); 

Thread.sleep(5000);

Thanks.

  • 1
    For best practice don't use Thread.sleep(), better to use Wait commands like Implicit Wait, Explicit Wait, Fluent Wait. depends on your needs. – sameer joshi Jan 19 '16 at 4:28
  • Why exactly do you think using Thread.sleep() is a bad practice? – IAmMilinPatel Jan 19 '16 at 7:05
1

Here's an example of a fluent wait method that I've integrated with my testing that will poll to see if an element is present every 500 milliseconds for 30 seconds before failing, you can call it and treat it like a regular WebElement so it may be helpful in this case.

public static WebElement fluentWait(WebDriver driver, final By locator)
{
    Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
            .withTimeout(30, TimeUnit.SECONDS)
            .pollingEvery(500, TimeUnit.MILLISECONDS)
            .ignoring(NoSuchElementException.class);

    WebElement element = wait.until(new Function<WebDriver, WebElement>() {
        @Override
        public WebElement apply(WebDriver driver) {
            return driver.findElement(locator);
        }
    });
    return element;
}

So calling it would look something like:

WebElement listing = fluentWait(driver, By.linkText("Check the Listings"));

listing.click();

This should help you bypass the infamous Thread.sleep() dilemma

  • Thank you Anthony, I think I will use fluent wait. I haven't tried fluent wait yet and was confused on how to code it. Thanks for showing me how to code it. – seleniumappiumnewbie Jan 21 '16 at 16:13
2

Sameer is right in his comment. Using thread.sleep is a considered a bad practice. I would suggest you to use Explicit wait.

Explicit waits would allow you to define your own wait conditions , based on conditions like presence of element located, alert is displayed or not.

Here is a good tutorial on how to use Explicit wait.

However, as a first approach, please debug whether this failure is due to waiting condition or there is some other reason behind it.

I would suggest you to post the errors/exceptions that you are getting when the test fails. This would help the people here in determining better, what the real cause it.

  • Why exactly do you think using Thread.sleep() is a bad practice? – IAmMilinPatel Jan 19 '16 at 7:05
  • Please refer blogs.msmvps.com/peterritchie/2007/04/26/… for the explanation – Minion4 Jan 19 '16 at 11:44
  • 3
    Generally it is because Thread.sleep() will introduce a fixed period of inactivity no matter what happens which will slow down your test, and if you are using CI generally result in much longer test run times if used all over the place. Also, more often than not people will use it because they don’t understand a problem so it’s quick and easy to just throw in an arbitrary wait rather than trying to understand the problem and fix it. – demouser123 Jan 19 '16 at 11:49
  • Also, I am a person who hates, something being hard coded, and Selenium gives you the flexibility to have waits implemented - so why not use that. – demouser123 Jan 19 '16 at 11:51
  • also, best practices are to use explicit waits only. – Peter M. Jan 19 '16 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.