0

This is a helper method we use in many places in our Selenium Java tests. The intent is to click a button and then detect that the browser has navigated away from the page by detecting the staleness of the HTML element:

protected static WebDriverWait wdWait;

public static void clickAndWaitForBrowserToLeavePage(WebElement elementToClick) {

    WebElement htmlTag = wdWait.until(ExpectedConditions.presenceOfElementLocated(By.tagName("html")));
    // click on the WebElement
    elementToClick.click();     
    // wait until the <html> tag becomes stale
    wdWait.until(ExpectedConditions.stalenessOf(htmlTag));
}

we also have this generalized version:

public static void clickAndWaitForElementToBecomeStale(WebElement elementToClick, WebElement elementToBecomeStale) {
    // click on the WebElement to click
    elementToClick.click();

    // wait until the old WebElement becomes stale
    wdWait.until(ExpectedConditions.stalenessOf(elementToBecomeStale));
}

The problem is that sometimes this script appears to not detect the staleness of the element it's waiting to become stale, probably because it's already stale before wdwait.until starts, and at that point it detects the same element on the next page, and obviously that's not going to go stale because we don't do anything on the new page before we finished this check.

Is there a way to avoid this "we're waiting an element on the next page instead of an element on the page we just navigated away from" race condition while still retaining the "check we've actually refreshed the page"?

1
  • have you looked into refresh functionality for this element instead of stalenessOf?wdWait.until(ExpectedConditions.refreshed(elementToBecomeStale));
    – BernardV
    Jul 5 at 13:59

1 Answer 1

0

The issue you're encountering is likely due to the race condition between the element becoming stale and the navigation to the next page in your automation strategy. As a software QA company, it is important to handle such scenarios effectively. The script needs to be modified to differentiate between the staleness of the element on the current page and the presence of a similar element on the next page. By implementing proper handling of the race condition, you can ensure that the staleness check is performed correctly and avoid any errors or inconsistencies.

public static void clickAndWaitForBrowserToLeavePage(WebElement elementToClick) {
    // Get the current URL before clicking the element
    String currentUrl = driver.getCurrentUrl();
    
    // click on the WebElement
    elementToClick.click();     
    
    // Wait until the URL changes
    wdWait.until(ExpectedConditions.not(ExpectedConditions.urlToBe(currentUrl)));
}

In this improved approach, we address the race condition by capturing the current URL before clicking the element. After the click, we wait for the URL to change, indicating a successful navigation to a new page. By relying on the URL change instead of the staleness of a specific element.

1
  • Interesting... do you think a similar race condition handling can be done for AJAX actions where we want to make sure a dialog is closed?
    – Nzall
    Jul 14 at 19:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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