Path: blob/trunk/java/test/org/openqa/selenium/ImplicitWaitTest.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.assertj.core.api.Assertions.assertThatExceptionOfType;21import static org.openqa.selenium.testing.drivers.Browser.FIREFOX;22import static org.openqa.selenium.testing.drivers.Browser.IE;23import static org.openqa.selenium.testing.drivers.Browser.SAFARI;2425import java.time.Duration;26import java.util.List;27import org.junit.jupiter.api.AfterEach;28import org.junit.jupiter.api.BeforeEach;29import org.junit.jupiter.api.Test;30import org.openqa.selenium.support.ui.ExpectedConditions;31import org.openqa.selenium.support.ui.Wait;32import org.openqa.selenium.support.ui.WebDriverWait;33import org.openqa.selenium.testing.Ignore;34import org.openqa.selenium.testing.JupiterTestBase;35import org.openqa.selenium.testing.NotYetImplemented;3637class ImplicitWaitTest extends JupiterTestBase {3839@BeforeEach40public void setUp() {41driver.manage().timeouts().implicitlyWait(Duration.ofMillis(0));42}4344@AfterEach45public void tearDown() {46driver.manage().timeouts().implicitlyWait(Duration.ofMillis(0));47}4849@Test50void shouldSetAndGetImplicitWaitTimeout() {51Duration timeout = driver.manage().timeouts().getImplicitWaitTimeout();52assertThat(timeout).hasMillis(0);53driver.manage().timeouts().implicitlyWait(Duration.ofMillis(3000));54Duration timeout2 = driver.manage().timeouts().getImplicitWaitTimeout();55assertThat(timeout2).hasMillis(3000);56}5758@Test59void testShouldImplicitlyWaitForASingleElement() {60driver.get(pages.dynamicPage);61WebElement add = driver.findElement(By.id("adder"));6263driver.manage().timeouts().implicitlyWait(Duration.ofMillis(3000));6465add.click();66driver.findElement(By.id("box0")); // All is well if this doesn't throw.67}6869@Test70void testShouldStillFailToFindAnElementWhenImplicitWaitsAreEnabled() {71driver.get(pages.dynamicPage);72driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));73assertThatExceptionOfType(NoSuchElementException.class)74.isThrownBy(() -> driver.findElement(By.id("box0")));75}7677@Test78void testShouldReturnAfterFirstAttemptToFindOneAfterDisablingImplicitWaits() {79driver.get(pages.dynamicPage);80driver.manage().timeouts().implicitlyWait(Duration.ofMillis(3000));81driver.manage().timeouts().implicitlyWait(Duration.ofMillis(0));82assertThatExceptionOfType(NoSuchElementException.class)83.isThrownBy(() -> driver.findElement(By.id("box0")));84}8586@Test87void testShouldImplicitlyWaitUntilAtLeastOneElementIsFoundWhenSearchingForMany() {88driver.get(pages.dynamicPage);89WebElement add = driver.findElement(By.id("adder"));9091driver.manage().timeouts().implicitlyWait(Duration.ofMillis(2000));92add.click();93add.click();9495List<WebElement> elements = driver.findElements(By.className("redbox"));96assertThat(elements).isNotEmpty();97}9899@Test100void testShouldStillFailToFindElementsWhenImplicitWaitsAreEnabled() {101driver.get(pages.dynamicPage);102driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));103List<WebElement> elements = driver.findElements(By.className("redbox"));104assertThat(elements).isEmpty();105}106107@Test108void testShouldStillFailToFindElementsByIdWhenImplicitWaitsAreEnabled() {109driver.get(pages.dynamicPage);110driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));111List<WebElement> elements = driver.findElements(By.id("redbox"));112assertThat(elements).isEmpty();113}114115@Test116void testShouldReturnAfterFirstAttemptToFindManyAfterDisablingImplicitWaits() {117driver.get(pages.dynamicPage);118WebElement add = driver.findElement(By.id("adder"));119120driver.manage().timeouts().implicitlyWait(Duration.ofMillis(1100));121driver.manage().timeouts().implicitlyWait(Duration.ofMillis(0));122add.click();123124List<WebElement> elements = driver.findElements(By.className("redbox"));125assertThat(elements).isEmpty();126}127128@Test129@Ignore(IE)130@Ignore(FIREFOX)131@NotYetImplemented(SAFARI)132public void testShouldImplicitlyWaitForAnElementToBeVisibleBeforeInteracting() {133driver.get(pages.dynamicPage);134135WebElement reveal = driver.findElement(By.id("reveal"));136WebElement revealed = driver.findElement(By.id("revealed"));137driver.manage().timeouts().implicitlyWait(Duration.ofMillis(5000));138139assertThat(revealed.isDisplayed()).isFalse();140reveal.click();141revealed.sendKeys("hello world");142}143144@Test145@NotYetImplemented(SAFARI)146public void testShouldRetainImplicitlyWaitFromTheReturnedWebDriverOfFrameSwitchTo() {147driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(1));148driver.get(pages.xhtmlTestPage);149driver.findElement(By.name("windowOne")).click();150151Wait<WebDriver> wait = new WebDriverWait(driver, Duration.ofSeconds(1));152wait.until(ExpectedConditions.numberOfWindowsToBe(2));153String handle = (String) driver.getWindowHandles().toArray()[1];154155WebDriver newWindow = driver.switchTo().window(handle);156157long start = System.currentTimeMillis();158159newWindow.findElements(By.id("this-crazy-thing-does-not-exist"));160161long end = System.currentTimeMillis();162163long time = end - start;164165assertThat(time).isGreaterThanOrEqualTo(1000);166}167}168169170