Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/java/test/org/openqa/selenium/PageLoadingStrategyTest.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.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated;
22
import static org.openqa.selenium.testing.drivers.Browser.CHROME;
23
import static org.openqa.selenium.testing.drivers.Browser.EDGE;
24
25
import org.junit.jupiter.api.Test;
26
import org.openqa.selenium.remote.CapabilityType;
27
import org.openqa.selenium.testing.Ignore;
28
import org.openqa.selenium.testing.JupiterTestBase;
29
import org.openqa.selenium.testing.NoDriverAfterTest;
30
import org.openqa.selenium.testing.NoDriverBeforeTest;
31
import org.openqa.selenium.testing.drivers.Browser;
32
33
public class PageLoadingStrategyTest extends JupiterTestBase {
34
35
@Test
36
@NoDriverBeforeTest
37
@NoDriverAfterTest
38
@Ignore(value = CHROME, reason = "Flaky")
39
@Ignore(value = EDGE, reason = "Flaky")
40
public void testNoneStrategyShouldNotWaitForPageToRefresh() {
41
initDriverWithLoadStrategy("none");
42
43
String slowPage = appServer.whereIs("sleep?time=5");
44
45
driver.get(slowPage);
46
// We discard the element, but want a check to make sure the page is loaded
47
wait.until(presenceOfElementLocated(By.tagName("body")));
48
49
long start = System.currentTimeMillis();
50
driver.navigate().refresh();
51
long end = System.currentTimeMillis();
52
53
long duration = end - start;
54
// The slow loading resource on that page takes 6 seconds to return,
55
// but with 'none' page loading strategy 'refresh' operation should not wait.
56
assertThat(duration).as("Page loading duration").isLessThan(1000);
57
}
58
59
@Test
60
@NoDriverBeforeTest
61
@NoDriverAfterTest
62
public void testEagerStrategyShouldNotWaitForResources() {
63
initDriverWithLoadStrategy("eager");
64
65
String slowPage = appServer.whereIs("slowLoadingResourcePage.html");
66
67
long start = System.currentTimeMillis();
68
driver.get(slowPage);
69
// We discard the element, but want a check to make sure the GET actually
70
// completed.
71
wait.until(presenceOfElementLocated(By.id("peas")));
72
long end = System.currentTimeMillis();
73
74
// The slow loading resource on that page takes 6 seconds to return. If we
75
// waited for it, our load time should be over 6 seconds.
76
long duration = end - start;
77
assertThat(duration).as("Page loading duration").isLessThan(5 * 1000);
78
}
79
80
@Test
81
@NoDriverBeforeTest
82
@NoDriverAfterTest
83
public void testEagerStrategyShouldNotWaitForResourcesOnRefresh() {
84
initDriverWithLoadStrategy("eager");
85
86
String slowPage = appServer.whereIs("slowLoadingResourcePage.html");
87
88
driver.get(slowPage);
89
// We discard the element, but want a check to make sure the GET actually completed.
90
wait.until(presenceOfElementLocated(By.id("peas")));
91
92
long start = System.currentTimeMillis();
93
driver.navigate().refresh();
94
// We discard the element, but want a check to make sure the refresh actually completed.
95
wait.until(presenceOfElementLocated(By.id("peas")));
96
long end = System.currentTimeMillis();
97
98
// The slow loading resource on that page takes 6 seconds to return. If we
99
// waited for it, our load time should be over 6 seconds.
100
long duration = end - start;
101
assertThat(duration).as("Page loading duration").isLessThan(5 * 1000);
102
}
103
104
@Test
105
@NoDriverBeforeTest
106
@NoDriverAfterTest
107
public void testEagerStrategyShouldWaitForDocumentToBeLoaded() {
108
initDriverWithLoadStrategy("eager");
109
110
String slowPage = appServer.whereIs("sleep?time=3");
111
112
driver.get(slowPage);
113
114
// We discard the element, but want a check to make sure the GET actually completed.
115
wait.until(presenceOfElementLocated(By.tagName("body")));
116
}
117
118
@Test
119
void testNormalStrategyShouldWaitForDocumentToBeLoaded() {
120
driver.get(pages.simpleTestPage);
121
assertThat(driver.getTitle()).isEqualTo("Hello WebDriver");
122
}
123
124
@Test
125
@NoDriverBeforeTest
126
@NoDriverAfterTest
127
public void testNoneStrategyShouldNotWaitForPageToLoad() {
128
initDriverWithLoadStrategy("none");
129
130
String slowPage = appServer.whereIs("sleep?time=5");
131
132
long start = System.currentTimeMillis();
133
driver.get(slowPage);
134
long end = System.currentTimeMillis();
135
136
long duration = end - start;
137
// The slow loading resource on that page takes 6 seconds to return,
138
// but with 'none' page loading strategy 'get' operation should not wait.
139
assertThat(duration).as("Page loading duration").isLessThan(1000);
140
}
141
142
private void initDriverWithLoadStrategy(String strategy) {
143
Capabilities caps =
144
Browser.detect()
145
.getCapabilities()
146
.merge(new ImmutableCapabilities(CapabilityType.PAGE_LOAD_STRATEGY, strategy));
147
createNewDriver(caps);
148
}
149
}
150
151