38
38
import static org .openqa .selenium .support .ui .ExpectedConditions .not ;
39
39
import static org .openqa .selenium .support .ui .ExpectedConditions .presenceOfElementLocated ;
40
40
import static org .openqa .selenium .support .ui .ExpectedConditions .titleIs ;
41
+ import static org .openqa .selenium .support .ui .ExpectedConditions .visibilityOfElementLocated ;
41
42
import static org .openqa .selenium .testing .Ignore .Driver .CHROME ;
42
43
import static org .openqa .selenium .testing .Ignore .Driver .FIREFOX ;
43
44
import static org .openqa .selenium .testing .Ignore .Driver .HTMLUNIT ;
44
45
import static org .openqa .selenium .testing .Ignore .Driver .IE ;
45
46
import static org .openqa .selenium .testing .Ignore .Driver .MARIONETTE ;
46
47
import static org .openqa .selenium .testing .Ignore .Driver .PHANTOMJS ;
47
48
import static org .openqa .selenium .testing .Ignore .Driver .SAFARI ;
48
- import static org .openqa .selenium .testing .TestUtilities .isChrome ;
49
49
import static org .openqa .selenium .testing .TestUtilities .getEffectivePlatform ;
50
+ import static org .openqa .selenium .testing .TestUtilities .isChrome ;
50
51
import static org .openqa .selenium .testing .TestUtilities .isFirefox ;
51
52
import static org .openqa .selenium .testing .TestUtilities .isLocal ;
52
53
import static org .openqa .selenium .testing .TestUtilities .isNativeEventsEnabled ;
@@ -198,7 +199,7 @@ public void testShouldFollowMetaRedirects() throws Exception {
198
199
public void testShouldBeAbleToGetAFragmentOnTheCurrentPage () {
199
200
driver .get (pages .xhtmlTestPage );
200
201
driver .get (pages .xhtmlTestPage + "#text" );
201
- driver . findElement ( By .id ("id1" ));
202
+ wait . until ( presenceOfElementLocated ( By .id ("id1" ) ));
202
203
}
203
204
204
205
@ Ignore (value = {SAFARI , MARIONETTE }, issues = {4062 })
@@ -281,7 +282,7 @@ public void testShouldDoNothingIfThereIsNothingToGoBackTo() {
281
282
public void testShouldBeAbleToNavigateBackInTheBrowserHistory () {
282
283
driver .get (pages .formPage );
283
284
284
- driver . findElement ( By .id ("imageButton" )).submit ();
285
+ wait . until ( visibilityOfElementLocated ( By .id ("imageButton" ) )).submit ();
285
286
wait .until (titleIs ("We Arrive Here" ));
286
287
287
288
driver .navigate ().back ();
@@ -293,7 +294,7 @@ public void testShouldBeAbleToNavigateBackInTheBrowserHistory() {
293
294
public void testShouldBeAbleToNavigateBackInTheBrowserHistoryInPresenceOfIframes () {
294
295
driver .get (pages .xhtmlTestPage );
295
296
296
- driver . findElement ( By .name ("sameWindow" )).click ();
297
+ wait . until ( visibilityOfElementLocated ( By .name ("sameWindow" ) )).click ();
297
298
wait .until (titleIs ("This page has iframes" ));
298
299
299
300
driver .navigate ().back ();
@@ -305,7 +306,7 @@ public void testShouldBeAbleToNavigateBackInTheBrowserHistoryInPresenceOfIframes
305
306
public void testShouldBeAbleToNavigateForwardsInTheBrowserHistory () {
306
307
driver .get (pages .formPage );
307
308
308
- driver . findElement ( By .id ("imageButton" )).submit ();
309
+ wait . until ( visibilityOfElementLocated ( By .id ("imageButton" ) )).submit ();
309
310
wait .until (titleIs ("We Arrive Here" ));
310
311
311
312
driver .navigate ().back ();
@@ -370,60 +371,58 @@ public void testShouldNotHangIfDocumentOpenCallIsNeverFollowedByDocumentCloseCal
370
371
driver .get (pages .documentWrite );
371
372
372
373
// 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" ) ));
374
375
wait .until (elementTextToContain (body , "world" ));
375
376
}
376
377
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
+
377
395
@ Ignore (value = {SAFARI , MARIONETTE },
378
396
reason = "Not implemented; Safari: see issue 687, comment 41" ,
379
397
issues = {687 })
380
398
@ NeedsLocalEnvironment
399
+ @ NoDriverAfterTest // Subsequent tests sometimes fail on Firefox.
381
400
@ Test
382
401
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 ();
389
402
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 );
410
404
} finally {
411
405
driver .manage ().timeouts ().pageLoadTimeout (-1 , SECONDS );
412
406
}
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" ));
413
411
}
414
412
415
413
@ Ignore (value = {HTMLUNIT , SAFARI , MARIONETTE },
416
414
reason = "Not implemented; Safari: see issue 687, comment 41" ,
417
415
issues = {687 })
418
416
@ NeedsLocalEnvironment
417
+ @ NoDriverAfterTest // Subsequent tests sometimes fail on Firefox.
419
418
@ Test
420
419
public void testShouldTimeoutIfAPageTakesTooLongToLoadAfterClick () {
421
420
assumeFalse (isFirefox (driver ) && isNativeEventsEnabled (driver ));
422
421
423
422
driver .manage ().timeouts ().pageLoadTimeout (2 , SECONDS );
424
423
425
424
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" ) ));
427
426
428
427
long start = System .currentTimeMillis ();
429
428
try {
@@ -437,25 +436,20 @@ public void testShouldTimeoutIfAPageTakesTooLongToLoadAfterClick() {
437
436
int duration = (int ) (end - start );
438
437
assertThat (duration , greaterThan (2000 ));
439
438
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
-
450
439
} finally {
451
440
driver .manage ().timeouts ().pageLoadTimeout (-1 , SECONDS );
452
441
}
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" ));
453
446
}
454
447
455
448
@ Ignore (value = {SAFARI , MARIONETTE },
456
449
reason = "Not implemented; Safari: see issue 687, comment 41" ,
457
450
issues = {687 })
458
451
@ NeedsLocalEnvironment
452
+ @ NoDriverAfterTest // Subsequent tests sometimes fail on Firefox.
459
453
@ Test
460
454
public void testShouldTimeoutIfAPageTakesTooLongToRefresh () {
461
455
// Get the sleeping servlet with a pause of 5 seconds
@@ -477,55 +471,31 @@ public void testShouldTimeoutIfAPageTakesTooLongToRefresh() {
477
471
int duration = (int ) (end - start );
478
472
assertThat (duration , greaterThan (2000 ));
479
473
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
-
490
474
} finally {
491
475
driver .manage ().timeouts ().pageLoadTimeout (-1 , SECONDS );
492
476
}
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" ));
493
481
}
494
482
495
483
@ Ignore (value = {CHROME , HTMLUNIT , SAFARI , MARIONETTE },
496
484
reason = "Not implemented; Safari: see issue 687, comment 41" ,
497
485
issues = {687 })
498
486
@ NeedsLocalEnvironment
487
+ @ NoDriverAfterTest // Subsequent tests sometimes fail on Firefox.
499
488
@ Test
500
489
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 ();
507
490
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 );
526
492
} finally {
527
493
driver .manage ().timeouts ().pageLoadTimeout (-1 , SECONDS );
528
494
}
495
+
496
+ new WebDriverWait (driver , 30 )
497
+ .ignoring (StaleElementReferenceException .class )
498
+ .until (elementTextToEqual (By .tagName ("body" ), "Slept for 11s" ));
529
499
}
530
500
531
501
@ After
@@ -536,4 +506,32 @@ public void quitDriver() {
536
506
}
537
507
}
538
508
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
+ }
539
537
}
0 commit comments