Skip to content

Commit 77c7c92

Browse files
committed
Deflake PageLoadingTest by allowing more time for things to load.
Forefox/Desktop and Chrome/Android were especially flaky. Refactor the tests and add testPageLoadTimeoutCanBeChanged().
1 parent e753e9b commit 77c7c92

File tree

1 file changed

+75
-77
lines changed

1 file changed

+75
-77
lines changed

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

Lines changed: 75 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,16 @@
3838
import static org.openqa.selenium.support.ui.ExpectedConditions.not;
3939
import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated;
4040
import static org.openqa.selenium.support.ui.ExpectedConditions.titleIs;
41+
import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfElementLocated;
4142
import static org.openqa.selenium.testing.Ignore.Driver.CHROME;
4243
import static org.openqa.selenium.testing.Ignore.Driver.FIREFOX;
4344
import static org.openqa.selenium.testing.Ignore.Driver.HTMLUNIT;
4445
import static org.openqa.selenium.testing.Ignore.Driver.IE;
4546
import static org.openqa.selenium.testing.Ignore.Driver.MARIONETTE;
4647
import static org.openqa.selenium.testing.Ignore.Driver.PHANTOMJS;
4748
import static org.openqa.selenium.testing.Ignore.Driver.SAFARI;
48-
import static org.openqa.selenium.testing.TestUtilities.isChrome;
4949
import static org.openqa.selenium.testing.TestUtilities.getEffectivePlatform;
50+
import static org.openqa.selenium.testing.TestUtilities.isChrome;
5051
import static org.openqa.selenium.testing.TestUtilities.isFirefox;
5152
import static org.openqa.selenium.testing.TestUtilities.isLocal;
5253
import static org.openqa.selenium.testing.TestUtilities.isNativeEventsEnabled;
@@ -198,7 +199,7 @@ public void testShouldFollowMetaRedirects() throws Exception {
198199
public void testShouldBeAbleToGetAFragmentOnTheCurrentPage() {
199200
driver.get(pages.xhtmlTestPage);
200201
driver.get(pages.xhtmlTestPage + "#text");
201-
driver.findElement(By.id("id1"));
202+
wait.until(presenceOfElementLocated(By.id("id1")));
202203
}
203204

204205
@Ignore(value = {SAFARI, MARIONETTE}, issues = {4062})
@@ -281,7 +282,7 @@ public void testShouldDoNothingIfThereIsNothingToGoBackTo() {
281282
public void testShouldBeAbleToNavigateBackInTheBrowserHistory() {
282283
driver.get(pages.formPage);
283284

284-
driver.findElement(By.id("imageButton")).submit();
285+
wait.until(visibilityOfElementLocated(By.id("imageButton"))).submit();
285286
wait.until(titleIs("We Arrive Here"));
286287

287288
driver.navigate().back();
@@ -293,7 +294,7 @@ public void testShouldBeAbleToNavigateBackInTheBrowserHistory() {
293294
public void testShouldBeAbleToNavigateBackInTheBrowserHistoryInPresenceOfIframes() {
294295
driver.get(pages.xhtmlTestPage);
295296

296-
driver.findElement(By.name("sameWindow")).click();
297+
wait.until(visibilityOfElementLocated(By.name("sameWindow"))).click();
297298
wait.until(titleIs("This page has iframes"));
298299

299300
driver.navigate().back();
@@ -305,7 +306,7 @@ public void testShouldBeAbleToNavigateBackInTheBrowserHistoryInPresenceOfIframes
305306
public void testShouldBeAbleToNavigateForwardsInTheBrowserHistory() {
306307
driver.get(pages.formPage);
307308

308-
driver.findElement(By.id("imageButton")).submit();
309+
wait.until(visibilityOfElementLocated(By.id("imageButton"))).submit();
309310
wait.until(titleIs("We Arrive Here"));
310311

311312
driver.navigate().back();
@@ -370,60 +371,58 @@ public void testShouldNotHangIfDocumentOpenCallIsNeverFollowedByDocumentCloseCal
370371
driver.get(pages.documentWrite);
371372

372373
// If this command succeeds, then all is well.
373-
WebElement body = driver.findElement(By.tagName("body"));
374+
WebElement body = wait.until(visibilityOfElementLocated(By.tagName("body")));
374375
wait.until(elementTextToContain(body, "world"));
375376
}
376377

378+
// Note: If this test ever fixed/enabled on Firefox, check if it also needs @NoDriverAfterTest OR
379+
// if @NoDriverAfterTest can be removed from some other tests in this class.
380+
@Ignore(value = {HTMLUNIT, FIREFOX, MARIONETTE, SAFARI, PHANTOMJS},
381+
reason = "Firefox: fails; Marionette: Not implemented; Safari: see issue 687, comment 41;"
382+
+ "PHANTOMJS, HTMLUNIT: not tested",
383+
issues = {687})
384+
@NeedsLocalEnvironment
385+
@Test
386+
public void testPageLoadTimeoutCanBeChanged() {
387+
try {
388+
testPageLoadTimeoutIsEnforced(2);
389+
testPageLoadTimeoutIsEnforced(3);
390+
} finally {
391+
driver.manage().timeouts().pageLoadTimeout(-1, SECONDS);
392+
}
393+
}
394+
377395
@Ignore(value = {SAFARI, MARIONETTE},
378396
reason = "Not implemented; Safari: see issue 687, comment 41",
379397
issues = {687})
380398
@NeedsLocalEnvironment
399+
@NoDriverAfterTest // Subsequent tests sometimes fail on Firefox.
381400
@Test
382401
public void testShouldTimeoutIfAPageTakesTooLongToLoad() {
383-
driver.manage().timeouts().pageLoadTimeout(2, SECONDS);
384-
385-
// Get the sleeping servlet with a pause of 5 seconds
386-
String slowPage = appServer.whereIs("sleep?time=5");
387-
388-
long start = System.currentTimeMillis();
389402
try {
390-
driver.get(slowPage);
391-
fail("I should have timed out");
392-
} catch (RuntimeException e) {
393-
long end = System.currentTimeMillis();
394-
395-
assertThat(e, is(instanceOf(TimeoutException.class)));
396-
397-
int duration = (int) (end - start);
398-
assertThat(duration, greaterThan(2000));
399-
assertThat(duration, lessThan(5000));
400-
401-
// check that after the exception another page can be loaded
402-
403-
start = System.currentTimeMillis();
404-
driver.get(pages.xhtmlTestPage);
405-
wait.until(titleIs("XHTML Test Page"));
406-
end = System.currentTimeMillis();
407-
duration = (int) (end - start);
408-
assertThat(duration, lessThan(2000));
409-
403+
testPageLoadTimeoutIsEnforced(2);
410404
} finally {
411405
driver.manage().timeouts().pageLoadTimeout(-1, SECONDS);
412406
}
407+
408+
// Load another page after get() timed out but before test HTTP server served previous page.
409+
driver.get(pages.xhtmlTestPage);
410+
wait.until(titleIs("XHTML Test Page"));
413411
}
414412

415413
@Ignore(value = {HTMLUNIT, SAFARI, MARIONETTE},
416414
reason = "Not implemented; Safari: see issue 687, comment 41",
417415
issues = {687})
418416
@NeedsLocalEnvironment
417+
@NoDriverAfterTest // Subsequent tests sometimes fail on Firefox.
419418
@Test
420419
public void testShouldTimeoutIfAPageTakesTooLongToLoadAfterClick() {
421420
assumeFalse(isFirefox(driver) && isNativeEventsEnabled(driver));
422421

423422
driver.manage().timeouts().pageLoadTimeout(2, SECONDS);
424423

425424
driver.get(appServer.whereIs("page_with_link_to_slow_loading_page.html"));
426-
WebElement link = driver.findElement(By.id("link-to-slow-loading-page"));
425+
WebElement link = wait.until(visibilityOfElementLocated(By.id("link-to-slow-loading-page")));
427426

428427
long start = System.currentTimeMillis();
429428
try {
@@ -437,25 +436,20 @@ public void testShouldTimeoutIfAPageTakesTooLongToLoadAfterClick() {
437436
int duration = (int) (end - start);
438437
assertThat(duration, greaterThan(2000));
439438
assertThat(duration, lessThan(5000));
440-
441-
// check that after the exception another page can be loaded
442-
443-
start = System.currentTimeMillis();
444-
driver.get(pages.xhtmlTestPage);
445-
wait.until(titleIs("XHTML Test Page"));
446-
end = System.currentTimeMillis();
447-
duration = (int) (end - start);
448-
assertThat(duration, lessThan(2000));
449-
450439
} finally {
451440
driver.manage().timeouts().pageLoadTimeout(-1, SECONDS);
452441
}
442+
443+
// Load another page after get() timed out but before test HTTP server served previous page.
444+
driver.get(pages.xhtmlTestPage);
445+
wait.until(titleIs("XHTML Test Page"));
453446
}
454447

455448
@Ignore(value = {SAFARI, MARIONETTE},
456449
reason = "Not implemented; Safari: see issue 687, comment 41",
457450
issues = {687})
458451
@NeedsLocalEnvironment
452+
@NoDriverAfterTest // Subsequent tests sometimes fail on Firefox.
459453
@Test
460454
public void testShouldTimeoutIfAPageTakesTooLongToRefresh() {
461455
// Get the sleeping servlet with a pause of 5 seconds
@@ -477,55 +471,31 @@ public void testShouldTimeoutIfAPageTakesTooLongToRefresh() {
477471
int duration = (int) (end - start);
478472
assertThat(duration, greaterThan(2000));
479473
assertThat(duration, lessThan(5000));
480-
481-
// check that after the exception another page can be loaded
482-
483-
start = System.currentTimeMillis();
484-
driver.get(pages.xhtmlTestPage);
485-
wait.until(titleIs("XHTML Test Page"));
486-
end = System.currentTimeMillis();
487-
duration = (int) (end - start);
488-
assertThat(duration, lessThan(2000));
489-
490474
} finally {
491475
driver.manage().timeouts().pageLoadTimeout(-1, SECONDS);
492476
}
477+
478+
// Load another page after get() timed out but before test HTTP server served previous page.
479+
driver.get(pages.xhtmlTestPage);
480+
wait.until(titleIs("XHTML Test Page"));
493481
}
494482

495483
@Ignore(value = {CHROME, HTMLUNIT, SAFARI, MARIONETTE},
496484
reason = "Not implemented; Safari: see issue 687, comment 41",
497485
issues = {687})
498486
@NeedsLocalEnvironment
487+
@NoDriverAfterTest // Subsequent tests sometimes fail on Firefox.
499488
@Test
500489
public void testShouldNotStopLoadingPageAfterTimeout() {
501-
driver.manage().timeouts().pageLoadTimeout(2, SECONDS);
502-
503-
// Get the sleeping servlet with a pause of 5 seconds
504-
String slowPage = appServer.whereIs("sleep?time=5");
505-
506-
long start = System.currentTimeMillis();
507490
try {
508-
driver.get(slowPage);
509-
fail("I should have timed out");
510-
} catch (RuntimeException e) {
511-
long end = System.currentTimeMillis();
512-
513-
assertThat(e, is(instanceOf(TimeoutException.class)));
514-
515-
int duration = (int) (end - start);
516-
assertThat(duration, greaterThan(2000));
517-
assertThat(duration, lessThan(5000));
518-
519-
new WebDriverWait(driver, 30)
520-
.ignoring(StaleElementReferenceException.class)
521-
.until(elementTextToEqual(By.tagName("body"), "Slept for 5s"));
522-
end = System.currentTimeMillis();
523-
duration = (int) (end - start);
524-
assertThat(duration, greaterThan(5000));
525-
491+
testPageLoadTimeoutIsEnforced(1);
526492
} finally {
527493
driver.manage().timeouts().pageLoadTimeout(-1, SECONDS);
528494
}
495+
496+
new WebDriverWait(driver, 30)
497+
.ignoring(StaleElementReferenceException.class)
498+
.until(elementTextToEqual(By.tagName("body"), "Slept for 11s"));
529499
}
530500

531501
@After
@@ -536,4 +506,32 @@ public void quitDriver() {
536506
}
537507
}
538508

509+
/**
510+
* Sets given pageLoadTimeout to the {@link #driver} and asserts that attempt to navigate to a
511+
* page that takes much longer (10 seconds longer) to load results in a TimeoutException.
512+
* <p>
513+
* Side effects: 1) {@link #driver} is configured to use given pageLoadTimeout,
514+
* 2) test HTTP server still didn't serve the page to browser (some browsers may still
515+
* be waiting for the page to load despite the fact that driver responded with the timeout).
516+
*/
517+
private void testPageLoadTimeoutIsEnforced(long webDriverPageLoadTimeout) {
518+
// Test page will load this many seconds longer than WD pageLoadTimeout.
519+
long pageLoadTimeBuffer = 10;
520+
driver.manage().timeouts().pageLoadTimeout(webDriverPageLoadTimeout, SECONDS);
521+
522+
long start = System.currentTimeMillis();
523+
try {
524+
driver
525+
.get(appServer.whereIs("sleep?time=" + (webDriverPageLoadTimeout + pageLoadTimeBuffer)));
526+
fail("I should have timed out after " + webDriverPageLoadTimeout + " seconds");
527+
} catch (RuntimeException e) {
528+
long end = System.currentTimeMillis();
529+
530+
assertThat(e, is(instanceOf(TimeoutException.class)));
531+
532+
long duration = end - start;
533+
assertThat(duration, greaterThan(webDriverPageLoadTimeout * 1000));
534+
assertThat(duration, lessThan((webDriverPageLoadTimeout + pageLoadTimeBuffer) * 1000));
535+
}
536+
}
539537
}

0 commit comments

Comments
 (0)