|
| 1 | +// Licensed to the Software Freedom Conservancy (SFC) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The SFC licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.openqa.selenium; |
| 19 | + |
| 20 | +import static org.assertj.core.api.Assertions.assertThat; |
| 21 | +import static org.junit.jupiter.api.Assertions.fail; |
| 22 | +import static org.openqa.selenium.WaitingConditions.elementTextToEqual; |
| 23 | +import static org.openqa.selenium.support.ui.ExpectedConditions.titleIs; |
| 24 | +import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfElementLocated; |
| 25 | +import static org.openqa.selenium.testing.drivers.Browser.CHROME; |
| 26 | +import static org.openqa.selenium.testing.drivers.Browser.EDGE; |
| 27 | +import static org.openqa.selenium.testing.drivers.Browser.HTMLUNIT; |
| 28 | +import static org.openqa.selenium.testing.drivers.Browser.SAFARI; |
| 29 | + |
| 30 | +import java.time.Duration; |
| 31 | +import org.junit.jupiter.api.Test; |
| 32 | +import org.openqa.selenium.support.ui.WebDriverWait; |
| 33 | +import org.openqa.selenium.testing.Ignore; |
| 34 | +import org.openqa.selenium.testing.JupiterTestBase; |
| 35 | +import org.openqa.selenium.testing.NeedsFreshDriver; |
| 36 | +import org.openqa.selenium.testing.NotYetImplemented; |
| 37 | + |
| 38 | +public class PageLoadTimeOutTest extends JupiterTestBase { |
| 39 | + |
| 40 | + // Note: If this test ever fixed/enabled on Firefox, check if it also needs @NoDriverAfterTest OR |
| 41 | + // if @NoDriverAfterTest can be removed from some other tests in this class. |
| 42 | + @Test |
| 43 | + @NotYetImplemented(SAFARI) |
| 44 | + public void testPageLoadTimeoutCanBeChanged() { |
| 45 | + testPageLoadTimeoutIsEnforced(2); |
| 46 | + testPageLoadTimeoutIsEnforced(3); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + @NotYetImplemented(SAFARI) |
| 51 | + public void testCanHandleSequentialPageLoadTimeouts() { |
| 52 | + long pageLoadTimeout = 2; |
| 53 | + long pageLoadTimeBuffer = 10; |
| 54 | + driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(2)); |
| 55 | + assertPageLoadTimeoutIsEnforced(pageLoadTimeout, pageLoadTimeBuffer); |
| 56 | + assertPageLoadTimeoutIsEnforced(pageLoadTimeout, pageLoadTimeBuffer); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + void testShouldTimeoutIfAPageTakesTooLongToLoad() { |
| 61 | + try { |
| 62 | + testPageLoadTimeoutIsEnforced(2); |
| 63 | + } finally { |
| 64 | + driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(300)); |
| 65 | + } |
| 66 | + |
| 67 | + // Load another page after get() timed out but before test HTTP server served previous page. |
| 68 | + driver.get(pages.xhtmlTestPage); |
| 69 | + wait.until(titleIs("XHTML Test Page")); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + @Ignore(HTMLUNIT) |
| 74 | + @Ignore(value = SAFARI, reason = "Flaky") |
| 75 | + public void testShouldTimeoutIfAPageTakesTooLongToLoadAfterClick() { |
| 76 | + driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(2)); |
| 77 | + |
| 78 | + driver.get(appServer.whereIs("page_with_link_to_slow_loading_page.html")); |
| 79 | + WebElement link = wait.until(visibilityOfElementLocated(By.id("link-to-slow-loading-page"))); |
| 80 | + |
| 81 | + long start = System.currentTimeMillis(); |
| 82 | + try { |
| 83 | + link.click(); |
| 84 | + fail("I should have timed out"); |
| 85 | + } catch (RuntimeException e) { |
| 86 | + long end = System.currentTimeMillis(); |
| 87 | + |
| 88 | + assertThat(e).isInstanceOf(TimeoutException.class); |
| 89 | + |
| 90 | + int duration = (int) (end - start); |
| 91 | + assertThat(duration).isGreaterThan(2000); |
| 92 | + assertThat(duration).isLessThan(5000); |
| 93 | + } finally { |
| 94 | + driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(300)); |
| 95 | + } |
| 96 | + |
| 97 | + // Load another page after get() timed out but before test HTTP server served previous page. |
| 98 | + driver.get(pages.xhtmlTestPage); |
| 99 | + wait.until(titleIs("XHTML Test Page")); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + @Ignore(value = CHROME, reason = "Flaky") |
| 104 | + @Ignore(value = EDGE, reason = "Flaky") |
| 105 | + @NeedsFreshDriver |
| 106 | + public void testShouldTimeoutIfAPageTakesTooLongToRefresh() { |
| 107 | + // Get the sleeping servlet with a pause of 5 seconds |
| 108 | + String slowPage = appServer.whereIs("sleep?time=5"); |
| 109 | + |
| 110 | + driver.get(slowPage); |
| 111 | + |
| 112 | + driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(2)); |
| 113 | + |
| 114 | + long start = System.currentTimeMillis(); |
| 115 | + try { |
| 116 | + driver.navigate().refresh(); |
| 117 | + fail("I should have timed out"); |
| 118 | + } catch (RuntimeException e) { |
| 119 | + long end = System.currentTimeMillis(); |
| 120 | + |
| 121 | + assertThat(e).isInstanceOf(TimeoutException.class); |
| 122 | + |
| 123 | + int duration = (int) (end - start); |
| 124 | + assertThat(duration).isGreaterThanOrEqualTo(2000); |
| 125 | + assertThat(duration).isLessThan(4000); |
| 126 | + } finally { |
| 127 | + driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(300)); |
| 128 | + } |
| 129 | + |
| 130 | + // Load another page after get() timed out but before test HTTP server served previous page. |
| 131 | + driver.get(pages.xhtmlTestPage); |
| 132 | + wait.until(titleIs("XHTML Test Page")); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + @NotYetImplemented(CHROME) |
| 137 | + @NotYetImplemented(EDGE) |
| 138 | + @NotYetImplemented(value = SAFARI) |
| 139 | + @NotYetImplemented(HTMLUNIT) |
| 140 | + public void testShouldNotStopLoadingPageAfterTimeout() { |
| 141 | + try { |
| 142 | + testPageLoadTimeoutIsEnforced(1); |
| 143 | + } finally { |
| 144 | + driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(300)); |
| 145 | + } |
| 146 | + |
| 147 | + new WebDriverWait(driver, Duration.ofSeconds(30)) |
| 148 | + .ignoring(StaleElementReferenceException.class) |
| 149 | + .until(elementTextToEqual(By.tagName("body"), "Slept for 11s")); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Sets given pageLoadTimeout to the {@link #driver} and asserts that attempt to navigate to a |
| 154 | + * page that takes much longer (10 seconds longer) to load results in a TimeoutException. |
| 155 | + * |
| 156 | + * <p>Side effects: 1) {@link #driver} is configured to use given pageLoadTimeout, 2) test HTTP |
| 157 | + * server still didn't serve the page to browser (some browsers may still be waiting for the page |
| 158 | + * to load despite the fact that driver responded with the timeout). |
| 159 | + */ |
| 160 | + private void testPageLoadTimeoutIsEnforced(long webDriverPageLoadTimeout) { |
| 161 | + // Test page will load this many seconds longer than WD pageLoadTimeout. |
| 162 | + long pageLoadTimeBuffer = 10; |
| 163 | + driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(webDriverPageLoadTimeout)); |
| 164 | + assertPageLoadTimeoutIsEnforced(webDriverPageLoadTimeout, pageLoadTimeBuffer); |
| 165 | + } |
| 166 | + |
| 167 | + private void assertPageLoadTimeoutIsEnforced( |
| 168 | + long webDriverPageLoadTimeout, long pageLoadTimeBuffer) { |
| 169 | + long start = System.currentTimeMillis(); |
| 170 | + try { |
| 171 | + driver.get( |
| 172 | + appServer.whereIs("sleep?time=" + (webDriverPageLoadTimeout + pageLoadTimeBuffer))); |
| 173 | + fail("I should have timed out after " + webDriverPageLoadTimeout + " seconds"); |
| 174 | + } catch (RuntimeException e) { |
| 175 | + long end = System.currentTimeMillis(); |
| 176 | + |
| 177 | + assertThat(e).isInstanceOf(TimeoutException.class); |
| 178 | + |
| 179 | + long duration = end - start; |
| 180 | + assertThat(duration).isGreaterThanOrEqualTo(webDriverPageLoadTimeout * 1000); |
| 181 | + assertThat(duration).isLessThan((webDriverPageLoadTimeout + pageLoadTimeBuffer) * 1000); |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments