Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webdriver FluentWait not ignoring exceptions

I have automated some user flows which go via this page http://www.efinancialcareers.co.uk/search. When I narrow a search using the left hand-side refine rail, an overlay appears where a user has to wait until the search results are returned. The method waits for the overlay to appear and then waits for it to disappear.

public void waitForSRPOverlayToComplete() {

    Wait<WebDriver> wait = new FluentWait<WebDriver>(getDriver())
            .withTimeout(5, TimeUnit.SECONDS)
            .pollingEvery(1, TimeUnit.NANOSECONDS)
            .ignoring(NoSuchElementException.class)
            .ignoring(TimeoutException.class);

    **// Error occurs here**
    WebElement blockedOverlay = wait.until(new Function<WebDriver, WebElement>() {
        public WebElement apply(WebDriver driver) {
            return driver.findElement(By.className("blockOverlay"));
        }
    });

    Wait<WebDriver> wait2 = new FluentWait<WebDriver>(getDriver())
            .withTimeout(5, TimeUnit.SECONDS)
            .pollingEvery(1, TimeUnit.NANOSECONDS)
            .ignoring(NoSuchElementException.class)
            .ignoring(TimeoutException.class);

    wait2.until(ExpectedConditions.stalenessOf(blockedOverlay));
}

On occasion I get a Timeout exception as the element (blockOverlay) is not found. I have observed the page when this occurs and the overlay does appear but I think sometimes when the search is very fast the above method misses it. I don’t understand why I get a technical error as I have told the fluent wait to ignore them.

This is the code to make the overlay appear:

$('body').block({
            message: $('#loaderEl'),
            css: {
                backgroundColor: 'transparent',
                border: "none",
                color: '#333333',
                fontWeight: 'bolder',
                top: ($(window).height() - $('#loaderEl').height()) / 2 + $(window).scrollTop() + "px"
            },
            overlayCSS: {
                backgroundColor: '#f8f8f8'
            },
            centerY: false
        });

And to remove it

$('body').unblock();

This is the error I receive:

Caused by: org.openqa.selenium.TimeoutException: Timed out after 5 seconds waiting for     com.efinancialcareers.myefc.desktop.BasePage$6@548238e0
Build info: version: '2.35.0', revision: '8df0c6bedf70ff9f22c647788f9fe9c8d22210e2',     time: '2013-08-17 12:46:41'
System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.6.0_26'
Driver info: driver.version: unknown
    ... 33 more
Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"class name","selector":"blockOverlay"}

Any help or suggestions would be appreciated.

like image 557
Ilyas Patel Avatar asked Dec 09 '13 10:12

Ilyas Patel


1 Answers

You can't suppress TimeoutException when the FluentWait actually times out. That's simply the nature of the API. If this is an exception you truly want to ignore, you need to catch the TimeoutException.

Also, as a side note, attempting to poll for the condition every nanosecond is likely counterproductive. You should probably extend your polling interval to something like every 200 or 250 milliseconds.

like image 162
JimEvans Avatar answered Nov 14 '22 22:11

JimEvans