Skip to content

Commit f31bcf5

Browse files
committed
Fixing testShouldNotStopLoadingPageAfterTimeout() which shows about 1% flakiness on Firefox on Linux.
1 parent 2890f2a commit f31bcf5

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

java/client/test/org/openqa/selenium/PageLoadingTest.java

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
package org.openqa.selenium;
1919

2020
import static java.util.concurrent.TimeUnit.SECONDS;
21-
import static org.hamcrest.Matchers.anyOf;
2221
import static org.hamcrest.Matchers.equalTo;
2322
import static org.hamcrest.Matchers.greaterThan;
2423
import static org.hamcrest.Matchers.instanceOf;
@@ -56,6 +55,7 @@
5655
import org.junit.Test;
5756
import org.openqa.selenium.remote.CapabilityType;
5857
import org.openqa.selenium.remote.DesiredCapabilities;
58+
import org.openqa.selenium.support.ui.ExpectedCondition;
5959
import org.openqa.selenium.support.ui.WebDriverWait;
6060
import org.openqa.selenium.testing.Ignore;
6161
import org.openqa.selenium.testing.JUnit4TestBase;
@@ -267,7 +267,32 @@ public void testShouldDoNothingIfThereIsNothingToGoBackTo() {
267267

268268
driver.navigate().back();
269269
// We may have returned to the browser's home page
270-
assertThat(driver.getTitle(), anyOf(equalTo(originalTitle), equalTo("We Leave From Here")));
270+
wait.until(either(titleIs(originalTitle), titleIs("We Leave From Here")));
271+
}
272+
273+
/**
274+
* A logical OR of the two given conditions.
275+
* TODO: Move to ExpectedConditions: generalize to N conditions, unit test.
276+
*/
277+
public static ExpectedCondition<Object> either(
278+
final ExpectedCondition<?> one,
279+
final ExpectedCondition<?> two) {
280+
return new ExpectedCondition<Object>() {
281+
@Override
282+
public Object apply(WebDriver driver) {
283+
Object result = one.apply(driver);
284+
if (result != null && !Boolean.FALSE.equals(result)) {
285+
return result;
286+
} else {
287+
return two.apply(driver);
288+
}
289+
}
290+
291+
@Override
292+
public String toString() {
293+
return "either of the conditions to be valid: " + one + ", " + two;
294+
}
295+
};
271296
}
272297

273298
@Ignore(value = {ANDROID, SAFARI, MARIONETTE}, issues = {3771})
@@ -436,7 +461,7 @@ public void testShouldTimeoutIfAPageTakesTooLongToLoadAfterClick() {
436461

437462
start = System.currentTimeMillis();
438463
driver.get(pages.xhtmlTestPage);
439-
assertThat(driver.getTitle(), equalTo("XHTML Test Page"));
464+
wait.until(titleIs("XHTML Test Page"));
440465
end = System.currentTimeMillis();
441466
duration = (int) (end - start);
442467
assertThat(duration, lessThan(2000));
@@ -510,7 +535,9 @@ public void testShouldNotStopLoadingPageAfterTimeout() {
510535
assertThat(duration, greaterThan(2000));
511536
assertThat(duration, lessThan(5000));
512537

513-
wait.until(elementTextToEqual(By.tagName("body"), "Slept for 5s"));
538+
new WebDriverWait(driver, 30)
539+
.ignoring(StaleElementReferenceException.class)
540+
.until(elementTextToEqual(By.tagName("body"), "Slept for 5s"));
514541
end = System.currentTimeMillis();
515542
duration = (int) (end - start);
516543
assertThat(duration, greaterThan(5000));

0 commit comments

Comments
 (0)