Path: blob/trunk/java/test/org/openqa/selenium/PageLoadingStrategyTest.java
1865 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617package org.openqa.selenium;1819import static org.assertj.core.api.Assertions.assertThat;20import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated;21import static org.openqa.selenium.testing.drivers.Browser.CHROME;22import static org.openqa.selenium.testing.drivers.Browser.EDGE;2324import org.junit.jupiter.api.Test;25import org.openqa.selenium.remote.CapabilityType;26import org.openqa.selenium.testing.Ignore;27import org.openqa.selenium.testing.JupiterTestBase;28import org.openqa.selenium.testing.NoDriverAfterTest;29import org.openqa.selenium.testing.NoDriverBeforeTest;30import org.openqa.selenium.testing.drivers.Browser;3132public class PageLoadingStrategyTest extends JupiterTestBase {3334@Test35@NoDriverBeforeTest36@NoDriverAfterTest37@Ignore(value = CHROME, reason = "Flaky")38@Ignore(value = EDGE, reason = "Flaky")39public void testNoneStrategyShouldNotWaitForPageToRefresh() {40initDriverWithLoadStrategy("none");4142String slowPage = appServer.whereIs("sleep?time=5");4344driver.get(slowPage);45// We discard the element, but want a check to make sure the page is loaded46wait.until(presenceOfElementLocated(By.tagName("body")));4748long start = System.currentTimeMillis();49driver.navigate().refresh();50long end = System.currentTimeMillis();5152long duration = end - start;53// The slow loading resource on that page takes 6 seconds to return,54// but with 'none' page loading strategy 'refresh' operation should not wait.55assertThat(duration).as("Page loading duration").isLessThan(1000);56}5758@Test59@NoDriverBeforeTest60@NoDriverAfterTest61public void testEagerStrategyShouldNotWaitForResources() {62initDriverWithLoadStrategy("eager");6364String slowPage = appServer.whereIs("slowLoadingResourcePage.html");6566long start = System.currentTimeMillis();67driver.get(slowPage);68// We discard the element, but want a check to make sure the GET actually69// completed.70wait.until(presenceOfElementLocated(By.id("peas")));71long end = System.currentTimeMillis();7273// The slow loading resource on that page takes 6 seconds to return. If we74// waited for it, our load time should be over 6 seconds.75long duration = end - start;76assertThat(duration).as("Page loading duration").isLessThan(5 * 1000);77}7879@Test80@NoDriverBeforeTest81@NoDriverAfterTest82public void testEagerStrategyShouldNotWaitForResourcesOnRefresh() {83initDriverWithLoadStrategy("eager");8485String slowPage = appServer.whereIs("slowLoadingResourcePage.html");8687driver.get(slowPage);88// We discard the element, but want a check to make sure the GET actually completed.89wait.until(presenceOfElementLocated(By.id("peas")));9091long start = System.currentTimeMillis();92driver.navigate().refresh();93// We discard the element, but want a check to make sure the refresh actually completed.94wait.until(presenceOfElementLocated(By.id("peas")));95long end = System.currentTimeMillis();9697// The slow loading resource on that page takes 6 seconds to return. If we98// waited for it, our load time should be over 6 seconds.99long duration = end - start;100assertThat(duration).as("Page loading duration").isLessThan(5 * 1000);101}102103@Test104@NoDriverBeforeTest105@NoDriverAfterTest106public void testEagerStrategyShouldWaitForDocumentToBeLoaded() {107initDriverWithLoadStrategy("eager");108109String slowPage = appServer.whereIs("sleep?time=3");110111driver.get(slowPage);112113// We discard the element, but want a check to make sure the GET actually completed.114wait.until(presenceOfElementLocated(By.tagName("body")));115}116117@Test118void testNormalStrategyShouldWaitForDocumentToBeLoaded() {119driver.get(pages.simpleTestPage);120assertThat(driver.getTitle()).isEqualTo("Hello WebDriver");121}122123@Test124@NoDriverBeforeTest125@NoDriverAfterTest126public void testNoneStrategyShouldNotWaitForPageToLoad() {127initDriverWithLoadStrategy("none");128129String slowPage = appServer.whereIs("sleep?time=5");130131long start = System.currentTimeMillis();132driver.get(slowPage);133long end = System.currentTimeMillis();134135long duration = end - start;136// The slow loading resource on that page takes 6 seconds to return,137// but with 'none' page loading strategy 'get' operation should not wait.138assertThat(duration).as("Page loading duration").isLessThan(1000);139}140141private void initDriverWithLoadStrategy(String strategy) {142Capabilities caps =143Browser.detect()144.getCapabilities()145.merge(new ImmutableCapabilities(CapabilityType.PAGE_LOAD_STRATEGY, strategy));146createNewDriver(caps);147}148}149150151