Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/java/test/org/openqa/selenium/PageLoadTimeOutTest.java
1865 views
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.SAFARI;
28
29
import java.time.Duration;
30
import org.junit.jupiter.api.Test;
31
import org.openqa.selenium.support.ui.WebDriverWait;
32
import org.openqa.selenium.testing.Ignore;
33
import org.openqa.selenium.testing.JupiterTestBase;
34
import org.openqa.selenium.testing.NeedsFreshDriver;
35
import org.openqa.selenium.testing.NotYetImplemented;
36
37
public class PageLoadTimeOutTest extends JupiterTestBase {
38
39
// Note: If this test ever fixed/enabled on Firefox, check if it also needs @NoDriverAfterTest OR
40
// if @NoDriverAfterTest can be removed from some other tests in this class.
41
@Test
42
@NotYetImplemented(SAFARI)
43
public void testPageLoadTimeoutCanBeChanged() {
44
testPageLoadTimeoutIsEnforced(2);
45
testPageLoadTimeoutIsEnforced(3);
46
}
47
48
@Test
49
@NotYetImplemented(SAFARI)
50
public void testCanHandleSequentialPageLoadTimeouts() {
51
long pageLoadTimeout = 2;
52
long pageLoadTimeBuffer = 10;
53
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(2));
54
assertPageLoadTimeoutIsEnforced(pageLoadTimeout, pageLoadTimeBuffer);
55
assertPageLoadTimeoutIsEnforced(pageLoadTimeout, pageLoadTimeBuffer);
56
}
57
58
@Test
59
void testShouldTimeoutIfAPageTakesTooLongToLoad() {
60
try {
61
testPageLoadTimeoutIsEnforced(2);
62
} finally {
63
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(300));
64
}
65
66
// Load another page after get() timed out but before test HTTP server served previous page.
67
driver.get(pages.xhtmlTestPage);
68
wait.until(titleIs("XHTML Test Page"));
69
}
70
71
@Test
72
@Ignore(value = SAFARI, reason = "Flaky")
73
public void testShouldTimeoutIfAPageTakesTooLongToLoadAfterClick() {
74
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(2));
75
76
driver.get(appServer.whereIs("page_with_link_to_slow_loading_page.html"));
77
WebElement link = wait.until(visibilityOfElementLocated(By.id("link-to-slow-loading-page")));
78
79
long start = System.currentTimeMillis();
80
try {
81
link.click();
82
fail("I should have timed out");
83
} catch (RuntimeException e) {
84
long end = System.currentTimeMillis();
85
86
assertThat(e).isInstanceOf(TimeoutException.class);
87
88
int duration = (int) (end - start);
89
assertThat(duration).isGreaterThan(2000);
90
assertThat(duration).isLessThan(5000);
91
} finally {
92
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(300));
93
}
94
95
// Load another page after get() timed out but before test HTTP server served previous page.
96
driver.get(pages.xhtmlTestPage);
97
wait.until(titleIs("XHTML Test Page"));
98
}
99
100
@Test
101
@Ignore(value = CHROME, reason = "Flaky")
102
@Ignore(value = EDGE, reason = "Flaky")
103
@NeedsFreshDriver
104
public void testShouldTimeoutIfAPageTakesTooLongToRefresh() {
105
// Get the sleeping servlet with a pause of 5 seconds
106
String slowPage = appServer.whereIs("sleep?time=5");
107
108
driver.get(slowPage);
109
110
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(2));
111
112
long start = System.currentTimeMillis();
113
try {
114
driver.navigate().refresh();
115
fail("I should have timed out");
116
} catch (RuntimeException e) {
117
long end = System.currentTimeMillis();
118
119
assertThat(e).isInstanceOf(TimeoutException.class);
120
121
int duration = (int) (end - start);
122
assertThat(duration).isGreaterThanOrEqualTo(2000);
123
assertThat(duration).isLessThan(4000);
124
} finally {
125
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(300));
126
}
127
128
// Load another page after get() timed out but before test HTTP server served previous page.
129
driver.get(pages.xhtmlTestPage);
130
wait.until(titleIs("XHTML Test Page"));
131
}
132
133
@Test
134
@NotYetImplemented(CHROME)
135
@NotYetImplemented(EDGE)
136
@NotYetImplemented(value = SAFARI)
137
public void testShouldNotStopLoadingPageAfterTimeout() {
138
try {
139
testPageLoadTimeoutIsEnforced(1);
140
} finally {
141
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(300));
142
}
143
144
new WebDriverWait(driver, Duration.ofSeconds(30))
145
.ignoring(StaleElementReferenceException.class)
146
.until(elementTextToEqual(By.tagName("body"), "Slept for 11s"));
147
}
148
149
/**
150
* Sets given pageLoadTimeout to the {@link #driver} and asserts that attempt to navigate to a
151
* page that takes much longer (10 seconds longer) to load results in a TimeoutException.
152
*
153
* <p>Side effects: 1) {@link #driver} is configured to use given pageLoadTimeout, 2) test HTTP
154
* server still didn't serve the page to browser (some browsers may still be waiting for the page
155
* to load despite the fact that driver responded with the timeout).
156
*/
157
private void testPageLoadTimeoutIsEnforced(long webDriverPageLoadTimeout) {
158
// Test page will load this many seconds longer than WD pageLoadTimeout.
159
long pageLoadTimeBuffer = 10;
160
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(webDriverPageLoadTimeout));
161
assertPageLoadTimeoutIsEnforced(webDriverPageLoadTimeout, pageLoadTimeBuffer);
162
}
163
164
private void assertPageLoadTimeoutIsEnforced(
165
long webDriverPageLoadTimeout, long pageLoadTimeBuffer) {
166
long start = System.currentTimeMillis();
167
try {
168
driver.get(
169
appServer.whereIs("sleep?time=" + (webDriverPageLoadTimeout + pageLoadTimeBuffer)));
170
fail("I should have timed out after " + webDriverPageLoadTimeout + " seconds");
171
} catch (RuntimeException e) {
172
long end = System.currentTimeMillis();
173
174
assertThat(e).isInstanceOf(TimeoutException.class);
175
176
long duration = end - start;
177
assertThat(duration).isGreaterThanOrEqualTo(webDriverPageLoadTimeout * 1000);
178
assertThat(duration).isLessThan((webDriverPageLoadTimeout + pageLoadTimeBuffer) * 1000);
179
}
180
}
181
}
182
183