Skip to content

Commit cd20deb

Browse files
committed
Break up page loading tests to execute more quickly
1 parent 8f6cc22 commit cd20deb

File tree

5 files changed

+429
-323
lines changed

5 files changed

+429
-323
lines changed

java/test/org/openqa/selenium/FormHandlingTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,10 @@ public void testShouldBeAbleToUploadTheSameFileTwice() throws IOException {
184184
uploadElement.sendKeys(file.getAbsolutePath());
185185
uploadElement.submit();
186186

187+
// Apparently on chrome we need to wait for the element to be gone if we're loading the same
188+
// page again
189+
wait.until(d -> d.findElements(By.id("upload")).isEmpty());
190+
187191
driver.get(pages.formPage);
188192
uploadElement = driver.findElement(By.id("upload"));
189193
assertThat(uploadElement.getAttribute("value")).isEmpty();
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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.openqa.selenium.WaitingConditions.windowToBeSwitchedToWithName;
22+
import static org.openqa.selenium.support.ui.ExpectedConditions.not;
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.HTMLUNIT;
26+
import static org.openqa.selenium.testing.drivers.Browser.SAFARI;
27+
28+
import org.junit.jupiter.api.Test;
29+
import org.openqa.selenium.testing.Ignore;
30+
import org.openqa.selenium.testing.JupiterTestBase;
31+
import org.openqa.selenium.testing.NeedsFreshDriver;
32+
import org.openqa.selenium.testing.NotYetImplemented;
33+
34+
public class HistoryNavigationTest extends JupiterTestBase {
35+
36+
@NeedsFreshDriver
37+
@Test
38+
@NotYetImplemented(
39+
value = HTMLUNIT,
40+
reason = "HtmlUnit: can't execute JavaScript before a page is loaded")
41+
@Ignore(value = SAFARI, reason = "Hanging")
42+
public void testShouldDoNothingIfThereIsNothingToGoBackTo() {
43+
((JavascriptExecutor) driver)
44+
.executeScript("window.open(arguments[0], 'newWindow')", pages.formPage);
45+
wait.until(windowToBeSwitchedToWithName("newWindow"));
46+
wait.until(titleIs("We Leave From Here"));
47+
String originalTitle = driver.getTitle();
48+
driver.get(pages.blankPage);
49+
wait.until(not(titleIs(originalTitle)));
50+
driver.navigate().back();
51+
wait.until(titleIs(originalTitle));
52+
driver.navigate().back(); // Nothing to go back to, must stay.
53+
assertThat(driver.getTitle()).isEqualTo(originalTitle);
54+
}
55+
56+
@Test
57+
@Ignore(SAFARI)
58+
public void testShouldBeAbleToNavigateBackInTheBrowserHistory() {
59+
driver.get(pages.formPage);
60+
61+
wait.until(visibilityOfElementLocated(By.id("imageButton"))).submit();
62+
wait.until(titleIs("We Arrive Here"));
63+
64+
driver.navigate().back();
65+
wait.until(titleIs("We Leave From Here"));
66+
}
67+
68+
@Test
69+
void testShouldBeAbleToNavigateBackInTheBrowserHistoryInPresenceOfIframes() {
70+
driver.get(pages.xhtmlTestPage);
71+
72+
wait.until(visibilityOfElementLocated(By.name("sameWindow"))).click();
73+
wait.until(titleIs("This page has iframes"));
74+
75+
driver.navigate().back();
76+
wait.until(titleIs("XHTML Test Page"));
77+
}
78+
79+
@Test
80+
void testShouldBeAbleToNavigateForwardsInTheBrowserHistory() {
81+
driver.get(pages.formPage);
82+
83+
wait.until(visibilityOfElementLocated(By.id("imageButton"))).submit();
84+
wait.until(titleIs("We Arrive Here"));
85+
86+
driver.navigate().back();
87+
wait.until(titleIs("We Leave From Here"));
88+
89+
driver.navigate().forward();
90+
wait.until(titleIs("We Arrive Here"));
91+
}
92+
}
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
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

Comments
 (0)