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.
-
1For 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– sameer joshi2016年01月19日 04:28:58 +00:00Commented Jan 19, 2016 at 4:28
-
Why exactly do you think using Thread.sleep() is a bad practice?IAmMilinPatel– IAmMilinPatel2016年01月19日 07:05:11 +00:00Commented Jan 19, 2016 at 7:05
2 Answers 2
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– seleniumappiumnewbie2016年01月21日 16:13:11 +00:00Commented Jan 21, 2016 at 16:13
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– IAmMilinPatel2016年01月19日 07:05:16 +00:00Commented Jan 19, 2016 at 7:05
-
Please refer blogs.msmvps.com/peterritchie/2007/04/26/… for the explanationMinion4– Minion42016年01月19日 11:44:12 +00:00Commented Jan 19, 2016 at 11:44
-
3Generally 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– demouser1232016年01月19日 11:49:57 +00:00Commented Jan 19, 2016 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– demouser1232016年01月19日 11:51:40 +00:00Commented Jan 19, 2016 at 11:51
-
also, best practices are to use explicit waits only.Peter M. - stands for Monica– Peter M. - stands for Monica2016年01月19日 15:28:34 +00:00Commented Jan 19, 2016 at 15:28