Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/java/test/org/openqa/selenium/PageLoadTimeOutTest.java
3991 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 java.lang.System.currentTimeMillis;
21
import static org.assertj.core.api.Assertions.assertThat;
22
import static org.assertj.core.api.Assertions.assertThatThrownBy;
23
import static org.openqa.selenium.WaitingConditions.elementTextToEqual;
24
import static org.openqa.selenium.support.ui.ExpectedConditions.titleIs;
25
import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfElementLocated;
26
import static org.openqa.selenium.testing.drivers.Browser.CHROME;
27
import static org.openqa.selenium.testing.drivers.Browser.EDGE;
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(value = SAFARI, reason = "Flaky")
74
public void testShouldTimeoutIfAPageTakesTooLongToLoadAfterClick() {
75
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(2));
76
try {
77
driver.get(appServer.whereIs("page_with_link_to_slow_loading_page.html"));
78
WebElement link = wait.until(visibilityOfElementLocated(By.id("link-to-slow-loading-page")));
79
80
long start = currentTimeMillis();
81
82
assertThatThrownBy(() -> link.click()).isInstanceOf(TimeoutException.class);
83
84
long duration = currentTimeMillis() - start;
85
assertThat(duration).isBetween(2000L, 5000L);
86
} finally {
87
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(300));
88
}
89
90
// Load another page after get() timed out but before test HTTP server served previous page.
91
driver.get(pages.xhtmlTestPage);
92
wait.until(titleIs("XHTML Test Page"));
93
}
94
95
@Test
96
@Ignore(value = CHROME, reason = "Flaky")
97
@Ignore(value = EDGE, reason = "Flaky")
98
@NeedsFreshDriver
99
public void testShouldTimeoutIfAPageTakesTooLongToRefresh() {
100
// Get the sleeping servlet with a pause of 5 seconds
101
String slowPage = appServer.whereIs("sleep?time=5");
102
103
driver.get(slowPage);
104
105
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(2));
106
107
try {
108
long start = currentTimeMillis();
109
110
assertThatThrownBy(() -> driver.navigate().refresh()).isInstanceOf(TimeoutException.class);
111
112
long duration = currentTimeMillis() - start;
113
assertThat(duration).isBetween(2000L, 4000L);
114
} finally {
115
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(300));
116
}
117
118
// Load another page after get() timed out but before test HTTP server served previous page.
119
driver.get(pages.xhtmlTestPage);
120
wait.until(titleIs("XHTML Test Page"));
121
}
122
123
@Test
124
@NotYetImplemented(CHROME)
125
@NotYetImplemented(EDGE)
126
@NotYetImplemented(value = SAFARI)
127
public void testShouldNotStopLoadingPageAfterTimeout() {
128
try {
129
testPageLoadTimeoutIsEnforced(1);
130
} finally {
131
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(300));
132
}
133
134
new WebDriverWait(driver, Duration.ofSeconds(30))
135
.ignoring(StaleElementReferenceException.class)
136
.until(elementTextToEqual(By.tagName("body"), "Slept for 11s"));
137
}
138
139
/**
140
* Sets given pageLoadTimeout to the {@link #driver} and asserts that attempt to navigate to a
141
* page that takes much longer (10 seconds longer) to load results in a TimeoutException.
142
*
143
* <p>Side effects: 1) {@link #driver} is configured to use given pageLoadTimeout, 2) test HTTP
144
* server still didn't serve the page to browser (some browsers may still be waiting for the page
145
* to load despite the fact that driver responded with the timeout).
146
*/
147
private void testPageLoadTimeoutIsEnforced(long webDriverPageLoadTimeout) {
148
// Test page will load this many seconds longer than WD pageLoadTimeout.
149
long pageLoadTimeBuffer = 10;
150
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(webDriverPageLoadTimeout));
151
assertPageLoadTimeoutIsEnforced(webDriverPageLoadTimeout, pageLoadTimeBuffer);
152
}
153
154
private void assertPageLoadTimeoutIsEnforced(
155
long webDriverPageLoadTimeout, long pageLoadTimeBuffer) {
156
long start = currentTimeMillis();
157
assertThatThrownBy(
158
() -> {
159
driver.get(
160
appServer.whereIs(
161
"sleep?time=" + (webDriverPageLoadTimeout + pageLoadTimeBuffer)));
162
})
163
.as("Should have timed out after " + webDriverPageLoadTimeout + " seconds")
164
.isInstanceOf(TimeoutException.class);
165
166
long duration = currentTimeMillis() - start;
167
assertThat(duration).isGreaterThanOrEqualTo(webDriverPageLoadTimeout * 1000);
168
assertThat(duration).isLessThan((webDriverPageLoadTimeout + pageLoadTimeBuffer) * 1000);
169
}
170
}
171
172