Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,11 @@ public void submit() {
}
}

page.submitButton.submit();
// A rejected submit re-renders the same page, so wait until the submitted document is replaced and the
// new one has loaded; otherwise a following assertion or submit might act on the old or a partial page.
WebElement submittedButton = page.driver.findElement(By.cssSelector("[type=\"submit\"]"));
submittedButton.submit();
page.driver.waiting().waitUntilStale(submittedButton).waitForDocumentComplete();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
import org.keycloak.testframework.ui.page.AbstractPage;

import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.WebDriverWait;

public class WaitUtils {
Expand All @@ -37,10 +39,18 @@ public WaitUtils waitForPage(AbstractPage page) {
}

public WaitUtils waitForOAuthCallback() {
waitForOAuthCallback(webdriver1 -> webdriver1.getCurrentUrl().contains(OAuth2Constants.CODE + "=") || webdriver1.getCurrentUrl().contains(OAuth2Constants.ERROR + "="));
waitForOAuthCallback(webdriver1 -> {
// match whole parameter names, as the login page URL contains "session_code="
String url = webdriver1.getCurrentUrl();
return hasUrlParameter(url, OAuth2Constants.CODE) || hasUrlParameter(url, OAuth2Constants.ERROR);
});
return this;
}

private static boolean hasUrlParameter(String url, String name) {
return url.contains("?" + name + "=") || url.contains("&" + name + "=") || url.contains("#" + name + "=");
}

public WaitUtils waitForOAuthCallback(Function<? super WebDriver, Boolean> oAuthResponseIsPresent) {
try {
createDefaultWait().until(oAuthResponseIsPresent);
Expand All @@ -55,6 +65,39 @@ public WaitUtils waitForTitle(String title) {
return this;
}

/**
* Waits until the element is no longer attached to the current document, for example after a form submit
* replaced the page. Firefox reports this as a {@link StaleElementReferenceException}, while Chrome (CDP)
* throws a plain {@link WebDriverException} saying the node does not belong to the document.
*/
public WaitUtils waitUntilStale(WebElement element) {
createDefaultWait().until(d -> {
try {
element.isEnabled();
return false;
} catch (StaleElementReferenceException e) {
return true;
} catch (WebDriverException e) {
if (e.getMessage() != null && e.getMessage().contains("does not belong to the document")) {
return true;
}
throw e;
}
});
return this;
}

/**
* Waits until the current document has finished loading, so that its page source and elements are complete.
* WebDriver errors are ignored as the browser might still be switching documents (see {@link #waitForPage}).
*/
public WaitUtils waitForDocumentComplete() {
createDefaultWait()
.ignoring(WebDriverException.class)
.until(d -> "complete".equals(((JavascriptExecutor) d).executeScript("return document.readyState")));
return this;
}

public <V> V until(Function<WebDriver, V> isTrue) {
return createDefaultWait().until(isTrue);
}
Expand Down
Loading