Skip to content

Commit

Permalink
When asserting a URL, allow for some time for any redirect to complete.
Browse files Browse the repository at this point in the history
  • Loading branch information
ahus1 authored and hmlnarik committed Jun 14, 2022
1 parent fb18b69 commit c2043da
Showing 1 changed file with 27 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.awaitility.core.ThrowingRunnable;
import org.junit.Assert;
import org.keycloak.testsuite.auth.page.login.PageWithLoginUrl;
import org.keycloak.testsuite.page.AbstractPage;
Expand All @@ -36,7 +37,9 @@
import java.io.StringWriter;
import java.net.URI;
import java.nio.charset.Charset;
import java.util.concurrent.TimeUnit;

import static org.awaitility.Awaitility.await;
import static org.junit.Assert.assertTrue;
import static org.keycloak.testsuite.util.URLUtils.currentUrlDoesntStartWith;
import static org.keycloak.testsuite.util.URLUtils.currentUrlEquals;
Expand Down Expand Up @@ -68,8 +71,10 @@ public static void assertCurrentUrlEquals(final URI url) {
}

public static void assertCurrentUrlEquals(final String url) {
assertTrue("Expected URL: " + url + " ; actual: " + DroneUtils.getCurrentDriver().getCurrentUrl(),
currentUrlEquals(url));
awaitUntilAsserted(() -> {
assertTrue("Expected URL: " + url + " ; actual: " + DroneUtils.getCurrentDriver().getCurrentUrl(),
currentUrlEquals(url));
});
}

public static void assertCurrentUrlStartsWith(final AbstractPage page, WebDriver driver) {
Expand All @@ -87,12 +92,10 @@ public static void assertCurrentUrlStartsWith(final AbstractPage page) {
}

public static void assertCurrentUrlStartsWith(final String url){
assertTrue("URL expected to begin with: " + removeDefaultPorts(url) + " ; actual URL: " + DroneUtils.getCurrentDriver().getCurrentUrl(),
currentUrlStartsWith(removeDefaultPorts(url)));
}

public static void waitUntilUrlStartsWith(String url, int timeOutInSeconds) {
new WebDriverWait(DroneUtils.getCurrentDriver(), timeOutInSeconds).until(ExpectedConditions.urlMatches("^" + url));
awaitUntilAsserted(() -> {
assertTrue("URL expected to begin with: " + removeDefaultPorts(url) + " ; actual URL: " + DroneUtils.getCurrentDriver().getCurrentUrl(),
currentUrlStartsWith(removeDefaultPorts(url)));
});
}

public static void assertCurrentUrlDoesntStartWith(final AbstractPage page, WebDriver driver) {
Expand All @@ -101,7 +104,9 @@ public static void assertCurrentUrlDoesntStartWith(final AbstractPage page, WebD

public static void assertCurrentUrlDoesntStartWith(final String url, WebDriver driver) {
DroneUtils.addWebDriver(driver);
assertCurrentUrlDoesntStartWith(url);
awaitUntilAsserted(() -> {
assertCurrentUrlDoesntStartWith(url);
});
DroneUtils.removeWebDriver();
}

Expand All @@ -110,8 +115,10 @@ public static void assertCurrentUrlDoesntStartWith(AbstractPage page) {
}

public static void assertCurrentUrlDoesntStartWith(final String url) {
assertTrue("URL expected NOT to begin with: " + url + " ; actual URL: " + DroneUtils.getCurrentDriver().getCurrentUrl(),
currentUrlDoesntStartWith(url));
awaitUntilAsserted(() -> {
assertTrue("URL expected NOT to begin with: " + url + " ; actual URL: " + DroneUtils.getCurrentDriver().getCurrentUrl(),
currentUrlDoesntStartWith(url));
});
}


Expand Down Expand Up @@ -193,4 +200,13 @@ public void assertResponse(CloseableHttpResponse response) throws IOException {

protected abstract void assertResponseBody(String body) throws IOException;
}

private static void awaitUntilAsserted(ThrowingRunnable r) {
await()
.pollInterval(100, TimeUnit.MILLISECONDS)
.pollInSameThread() // to ensure that drones are accessible
.atMost(10, TimeUnit.SECONDS)
.untilAsserted(r);
}

}

0 comments on commit c2043da

Please sign in to comment.