Path: blob/trunk/java/test/org/openqa/selenium/PageLoadTimeOutTest.java
1865 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617package org.openqa.selenium;1819import static org.assertj.core.api.Assertions.assertThat;20import static org.junit.jupiter.api.Assertions.fail;21import static org.openqa.selenium.WaitingConditions.elementTextToEqual;22import static org.openqa.selenium.support.ui.ExpectedConditions.titleIs;23import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfElementLocated;24import static org.openqa.selenium.testing.drivers.Browser.CHROME;25import static org.openqa.selenium.testing.drivers.Browser.EDGE;26import static org.openqa.selenium.testing.drivers.Browser.SAFARI;2728import java.time.Duration;29import org.junit.jupiter.api.Test;30import org.openqa.selenium.support.ui.WebDriverWait;31import org.openqa.selenium.testing.Ignore;32import org.openqa.selenium.testing.JupiterTestBase;33import org.openqa.selenium.testing.NeedsFreshDriver;34import org.openqa.selenium.testing.NotYetImplemented;3536public class PageLoadTimeOutTest extends JupiterTestBase {3738// Note: If this test ever fixed/enabled on Firefox, check if it also needs @NoDriverAfterTest OR39// if @NoDriverAfterTest can be removed from some other tests in this class.40@Test41@NotYetImplemented(SAFARI)42public void testPageLoadTimeoutCanBeChanged() {43testPageLoadTimeoutIsEnforced(2);44testPageLoadTimeoutIsEnforced(3);45}4647@Test48@NotYetImplemented(SAFARI)49public void testCanHandleSequentialPageLoadTimeouts() {50long pageLoadTimeout = 2;51long pageLoadTimeBuffer = 10;52driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(2));53assertPageLoadTimeoutIsEnforced(pageLoadTimeout, pageLoadTimeBuffer);54assertPageLoadTimeoutIsEnforced(pageLoadTimeout, pageLoadTimeBuffer);55}5657@Test58void testShouldTimeoutIfAPageTakesTooLongToLoad() {59try {60testPageLoadTimeoutIsEnforced(2);61} finally {62driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(300));63}6465// Load another page after get() timed out but before test HTTP server served previous page.66driver.get(pages.xhtmlTestPage);67wait.until(titleIs("XHTML Test Page"));68}6970@Test71@Ignore(value = SAFARI, reason = "Flaky")72public void testShouldTimeoutIfAPageTakesTooLongToLoadAfterClick() {73driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(2));7475driver.get(appServer.whereIs("page_with_link_to_slow_loading_page.html"));76WebElement link = wait.until(visibilityOfElementLocated(By.id("link-to-slow-loading-page")));7778long start = System.currentTimeMillis();79try {80link.click();81fail("I should have timed out");82} catch (RuntimeException e) {83long end = System.currentTimeMillis();8485assertThat(e).isInstanceOf(TimeoutException.class);8687int duration = (int) (end - start);88assertThat(duration).isGreaterThan(2000);89assertThat(duration).isLessThan(5000);90} finally {91driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(300));92}9394// Load another page after get() timed out but before test HTTP server served previous page.95driver.get(pages.xhtmlTestPage);96wait.until(titleIs("XHTML Test Page"));97}9899@Test100@Ignore(value = CHROME, reason = "Flaky")101@Ignore(value = EDGE, reason = "Flaky")102@NeedsFreshDriver103public void testShouldTimeoutIfAPageTakesTooLongToRefresh() {104// Get the sleeping servlet with a pause of 5 seconds105String slowPage = appServer.whereIs("sleep?time=5");106107driver.get(slowPage);108109driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(2));110111long start = System.currentTimeMillis();112try {113driver.navigate().refresh();114fail("I should have timed out");115} catch (RuntimeException e) {116long end = System.currentTimeMillis();117118assertThat(e).isInstanceOf(TimeoutException.class);119120int duration = (int) (end - start);121assertThat(duration).isGreaterThanOrEqualTo(2000);122assertThat(duration).isLessThan(4000);123} finally {124driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(300));125}126127// Load another page after get() timed out but before test HTTP server served previous page.128driver.get(pages.xhtmlTestPage);129wait.until(titleIs("XHTML Test Page"));130}131132@Test133@NotYetImplemented(CHROME)134@NotYetImplemented(EDGE)135@NotYetImplemented(value = SAFARI)136public void testShouldNotStopLoadingPageAfterTimeout() {137try {138testPageLoadTimeoutIsEnforced(1);139} finally {140driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(300));141}142143new WebDriverWait(driver, Duration.ofSeconds(30))144.ignoring(StaleElementReferenceException.class)145.until(elementTextToEqual(By.tagName("body"), "Slept for 11s"));146}147148/**149* Sets given pageLoadTimeout to the {@link #driver} and asserts that attempt to navigate to a150* page that takes much longer (10 seconds longer) to load results in a TimeoutException.151*152* <p>Side effects: 1) {@link #driver} is configured to use given pageLoadTimeout, 2) test HTTP153* server still didn't serve the page to browser (some browsers may still be waiting for the page154* to load despite the fact that driver responded with the timeout).155*/156private void testPageLoadTimeoutIsEnforced(long webDriverPageLoadTimeout) {157// Test page will load this many seconds longer than WD pageLoadTimeout.158long pageLoadTimeBuffer = 10;159driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(webDriverPageLoadTimeout));160assertPageLoadTimeoutIsEnforced(webDriverPageLoadTimeout, pageLoadTimeBuffer);161}162163private void assertPageLoadTimeoutIsEnforced(164long webDriverPageLoadTimeout, long pageLoadTimeBuffer) {165long start = System.currentTimeMillis();166try {167driver.get(168appServer.whereIs("sleep?time=" + (webDriverPageLoadTimeout + pageLoadTimeBuffer)));169fail("I should have timed out after " + webDriverPageLoadTimeout + " seconds");170} catch (RuntimeException e) {171long end = System.currentTimeMillis();172173assertThat(e).isInstanceOf(TimeoutException.class);174175long duration = end - start;176assertThat(duration).isGreaterThanOrEqualTo(webDriverPageLoadTimeout * 1000);177assertThat(duration).isLessThan((webDriverPageLoadTimeout + pageLoadTimeBuffer) * 1000);178}179}180}181182183