Path: blob/trunk/java/test/org/openqa/selenium/PageLoadTimeOutTest.java
3991 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 java.lang.System.currentTimeMillis;20import static org.assertj.core.api.Assertions.assertThat;21import static org.assertj.core.api.Assertions.assertThatThrownBy;22import static org.openqa.selenium.WaitingConditions.elementTextToEqual;23import static org.openqa.selenium.support.ui.ExpectedConditions.titleIs;24import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfElementLocated;25import static org.openqa.selenium.testing.drivers.Browser.CHROME;26import static org.openqa.selenium.testing.drivers.Browser.EDGE;27import static org.openqa.selenium.testing.drivers.Browser.SAFARI;2829import java.time.Duration;30import org.junit.jupiter.api.Test;31import org.openqa.selenium.support.ui.WebDriverWait;32import org.openqa.selenium.testing.Ignore;33import org.openqa.selenium.testing.JupiterTestBase;34import org.openqa.selenium.testing.NeedsFreshDriver;35import org.openqa.selenium.testing.NotYetImplemented;3637public class PageLoadTimeOutTest extends JupiterTestBase {3839// Note: If this test ever fixed/enabled on Firefox, check if it also needs @NoDriverAfterTest OR40// if @NoDriverAfterTest can be removed from some other tests in this class.41@Test42@NotYetImplemented(SAFARI)43public void testPageLoadTimeoutCanBeChanged() {44testPageLoadTimeoutIsEnforced(2);45testPageLoadTimeoutIsEnforced(3);46}4748@Test49@NotYetImplemented(SAFARI)50public void testCanHandleSequentialPageLoadTimeouts() {51long pageLoadTimeout = 2;52long pageLoadTimeBuffer = 10;53driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(2));54assertPageLoadTimeoutIsEnforced(pageLoadTimeout, pageLoadTimeBuffer);55assertPageLoadTimeoutIsEnforced(pageLoadTimeout, pageLoadTimeBuffer);56}5758@Test59void testShouldTimeoutIfAPageTakesTooLongToLoad() {60try {61testPageLoadTimeoutIsEnforced(2);62} finally {63driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(300));64}6566// Load another page after get() timed out but before test HTTP server served previous page.67driver.get(pages.xhtmlTestPage);68wait.until(titleIs("XHTML Test Page"));69}7071@Test72@Ignore(value = SAFARI, reason = "Flaky")73public void testShouldTimeoutIfAPageTakesTooLongToLoadAfterClick() {74driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(2));75try {76driver.get(appServer.whereIs("page_with_link_to_slow_loading_page.html"));77WebElement link = wait.until(visibilityOfElementLocated(By.id("link-to-slow-loading-page")));7879long start = currentTimeMillis();8081assertThatThrownBy(() -> link.click()).isInstanceOf(TimeoutException.class);8283long duration = currentTimeMillis() - start;84assertThat(duration).isBetween(2000L, 5000L);85} finally {86driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(300));87}8889// Load another page after get() timed out but before test HTTP server served previous page.90driver.get(pages.xhtmlTestPage);91wait.until(titleIs("XHTML Test Page"));92}9394@Test95@Ignore(value = CHROME, reason = "Flaky")96@Ignore(value = EDGE, reason = "Flaky")97@NeedsFreshDriver98public void testShouldTimeoutIfAPageTakesTooLongToRefresh() {99// Get the sleeping servlet with a pause of 5 seconds100String slowPage = appServer.whereIs("sleep?time=5");101102driver.get(slowPage);103104driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(2));105106try {107long start = currentTimeMillis();108109assertThatThrownBy(() -> driver.navigate().refresh()).isInstanceOf(TimeoutException.class);110111long duration = currentTimeMillis() - start;112assertThat(duration).isBetween(2000L, 4000L);113} finally {114driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(300));115}116117// Load another page after get() timed out but before test HTTP server served previous page.118driver.get(pages.xhtmlTestPage);119wait.until(titleIs("XHTML Test Page"));120}121122@Test123@NotYetImplemented(CHROME)124@NotYetImplemented(EDGE)125@NotYetImplemented(value = SAFARI)126public void testShouldNotStopLoadingPageAfterTimeout() {127try {128testPageLoadTimeoutIsEnforced(1);129} finally {130driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(300));131}132133new WebDriverWait(driver, Duration.ofSeconds(30))134.ignoring(StaleElementReferenceException.class)135.until(elementTextToEqual(By.tagName("body"), "Slept for 11s"));136}137138/**139* Sets given pageLoadTimeout to the {@link #driver} and asserts that attempt to navigate to a140* page that takes much longer (10 seconds longer) to load results in a TimeoutException.141*142* <p>Side effects: 1) {@link #driver} is configured to use given pageLoadTimeout, 2) test HTTP143* server still didn't serve the page to browser (some browsers may still be waiting for the page144* to load despite the fact that driver responded with the timeout).145*/146private void testPageLoadTimeoutIsEnforced(long webDriverPageLoadTimeout) {147// Test page will load this many seconds longer than WD pageLoadTimeout.148long pageLoadTimeBuffer = 10;149driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(webDriverPageLoadTimeout));150assertPageLoadTimeoutIsEnforced(webDriverPageLoadTimeout, pageLoadTimeBuffer);151}152153private void assertPageLoadTimeoutIsEnforced(154long webDriverPageLoadTimeout, long pageLoadTimeBuffer) {155long start = currentTimeMillis();156assertThatThrownBy(157() -> {158driver.get(159appServer.whereIs(160"sleep?time=" + (webDriverPageLoadTimeout + pageLoadTimeBuffer)));161})162.as("Should have timed out after " + webDriverPageLoadTimeout + " seconds")163.isInstanceOf(TimeoutException.class);164165long duration = currentTimeMillis() - start;166assertThat(duration).isGreaterThanOrEqualTo(webDriverPageLoadTimeout * 1000);167assertThat(duration).isLessThan((webDriverPageLoadTimeout + pageLoadTimeBuffer) * 1000);168}169}170171172