Skip to content

Commit 03da425

Browse files
committed
Use WebDriverWait on some flaky tests
1 parent 0abf21e commit 03da425

4 files changed

Lines changed: 33 additions & 18 deletions

File tree

uaa/src/test/java/org/cloudfoundry/identity/uaa/integration/SessionControllerIntegrationTests.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,11 @@ void sessionPageHasTheFunction() {
4343
"/session?clientId=admin&messageOrigin=http://localhost:8080");
4444

4545
WebDriverWait wait = webDriver.createWebDriverWait();
46-
Object type = wait.until(driver -> webDriver.getJavascriptExecutor().executeScript(
47-
"return typeof(handleMessage);"));
46+
Object type = wait.until(driver -> {
47+
Object t = webDriver.getJavascriptExecutor().executeScript(
48+
"return typeof(handleMessage);");
49+
return "function".equals(String.valueOf(t)) ? t : null;
50+
});
4851

4952
assertThat(type).hasToString("function");
5053
}

uaa/src/test/java/org/cloudfoundry/identity/uaa/integration/feature/HomeIT.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ void profilePage() {
8989
} catch (TimeoutException e) {
9090
webDriver.get(baseUrl + "/profile");
9191
}
92+
WebDriverWait wait = webDriver.createWebDriverWait();
93+
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("h1")));
9294
assertThat(webDriver.findElement(By.cssSelector("h1")).getText()).contains("Account Settings");
9395
}
9496

uaa/src/test/java/org/cloudfoundry/identity/uaa/integration/feature/LoginIT.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import org.openqa.selenium.By;
3131
import org.openqa.selenium.JavascriptExecutor;
3232
import org.openqa.selenium.WebElement;
33+
import org.openqa.selenium.support.ui.ExpectedConditions;
34+
import org.openqa.selenium.support.ui.WebDriverWait;
3335
import org.springframework.beans.factory.annotation.Autowired;
3436
import org.springframework.beans.factory.annotation.Value;
3537
import org.springframework.http.HttpEntity;
@@ -252,14 +254,15 @@ void passcodeRedirect() {
252254

253255
attemptLogin(testAccounts.getUserName(), testAccounts.getPassword());
254256

257+
WebDriverWait wait = webDriver.createWebDriverWait();
258+
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("h1")));
255259
assertThat(webDriver.findElement(By.cssSelector("h1")).getText()).contains("Temporary Authentication Code");
256260

257-
// Verify that the CopyToClipboard function can be executed
261+
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("passcode")));
258262
String passcode = webDriver.findElement(By.id("passcode")).getText();
259263
(webDriver.getJavascriptExecutor()).executeScript("CopyToClipboard",
260264
passcode);
261-
// Verify that the copybutton can be clicked
262-
webDriver.findElement(By.id("copybutton")).click();
265+
wait.until(ExpectedConditions.elementToBeClickable(By.id("copybutton"))).click();
263266
}
264267

265268
@Test
@@ -335,6 +338,8 @@ void redirectAfterUnsuccessfulLogin() {
335338
@Test
336339
void loginPageReloadBasedOnCsrf() {
337340
webDriver.get(baseUrl + "/login");
341+
webDriver.createWebDriverWait()
342+
.until(driver -> driver.getPageSource().contains("http-equiv=\"refresh\""));
338343
assertThat(webDriver.getPageSource()).contains("http-equiv=\"refresh\"");
339344
}
340345

uaa/src/test/java/org/cloudfoundry/identity/uaa/integration/feature/RateLimitingIT.java

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,28 +68,33 @@ void logout_and_clear_cookies() {
6868
@Test
6969
void infoEndpointRateLimited() throws InterruptedException {
7070
RestOperations restTemplate = serverRunning.getRestTemplate();
71-
//One Request should pass
71+
72+
// Wait for a fresh rate-limit window to avoid interference from prior requests
73+
TimeUnit.SECONDS.sleep(2);
74+
7275
ResponseEntity<String> response = restTemplate.getForEntity(baseUrl + "/info", String.class);
7376
assertThat(response.getStatusCode()).isNotEqualTo(HttpStatus.TOO_MANY_REQUESTS);
74-
boolean rateLimited = false;
77+
7578
int infoLimit = 20;
7679
int requestCount = 50;
77-
//Limit on /info is set to 20
80+
int tolerance = 5;
81+
7882
List<ResponseEntity> responses = new ArrayList<>(requestCount);
79-
//Many Requests should hit the RL
8083
IntStream.range(0, requestCount).forEach(x -> responses.add(restTemplate.getForEntity(baseUrl + "/info", String.class)));
81-
//Check numbers
84+
8285
long limits = responses.stream().filter(s -> HttpStatus.TOO_MANY_REQUESTS.equals(s.getStatusCode())).count();
8386
long oKs = responses.stream().filter(s -> HttpStatus.OK.equals(s.getStatusCode())).count();
8487
assertThat(limits + oKs).isEqualTo(requestCount);
85-
//Expect limited count around expected ones, more limited then with OK and check with tolerance of 2 that only expected limits are done
86-
if (limits > oKs && limits > (infoLimit - 2) && limits < (requestCount - infoLimit + 2)) {
87-
rateLimited = true;
88-
}
89-
assertThat(rateLimited).as("Rate limit counters are not as expected. Request: " + requestCount + ", Limit: " + infoLimit + ", blocked: " + limits
90-
+ ", allowed: " + oKs).isTrue();
91-
//After 1s, New Limit should be available
92-
TimeUnit.SECONDS.sleep(1);
88+
89+
assertThat(limits)
90+
.as("Rate limit counters are not as expected. Request: %d, Limit: %d, blocked: %d, allowed: %d",
91+
requestCount, infoLimit, limits, oKs)
92+
.isGreaterThan(oKs)
93+
.isGreaterThanOrEqualTo(infoLimit - tolerance)
94+
.isLessThanOrEqualTo(requestCount - infoLimit + tolerance);
95+
96+
// After the window resets, a new request should pass
97+
TimeUnit.SECONDS.sleep(2);
9398
response = restTemplate.getForEntity(baseUrl + "/info", String.class);
9499
assertThat(response.getStatusCode()).isNotEqualTo(HttpStatus.TOO_MANY_REQUESTS);
95100
}

0 commit comments

Comments
 (0)