Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/java/test/org/openqa/selenium/ImplicitWaitTest.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.assertj.core.api.Assertions.assertThatExceptionOfType;
22
import static org.openqa.selenium.testing.drivers.Browser.FIREFOX;
23
import static org.openqa.selenium.testing.drivers.Browser.IE;
24
import static org.openqa.selenium.testing.drivers.Browser.SAFARI;
25
26
import java.time.Duration;
27
import java.util.List;
28
import org.junit.jupiter.api.AfterEach;
29
import org.junit.jupiter.api.BeforeEach;
30
import org.junit.jupiter.api.Test;
31
import org.openqa.selenium.support.ui.ExpectedConditions;
32
import org.openqa.selenium.support.ui.Wait;
33
import org.openqa.selenium.support.ui.WebDriverWait;
34
import org.openqa.selenium.testing.Ignore;
35
import org.openqa.selenium.testing.JupiterTestBase;
36
import org.openqa.selenium.testing.NotYetImplemented;
37
38
class ImplicitWaitTest extends JupiterTestBase {
39
40
@BeforeEach
41
public void setUp() {
42
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(0));
43
}
44
45
@AfterEach
46
public void tearDown() {
47
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(0));
48
}
49
50
@Test
51
void shouldSetAndGetImplicitWaitTimeout() {
52
Duration timeout = driver.manage().timeouts().getImplicitWaitTimeout();
53
assertThat(timeout).hasMillis(0);
54
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(3000));
55
Duration timeout2 = driver.manage().timeouts().getImplicitWaitTimeout();
56
assertThat(timeout2).hasMillis(3000);
57
}
58
59
@Test
60
void testShouldImplicitlyWaitForASingleElement() {
61
driver.get(pages.dynamicPage);
62
WebElement add = driver.findElement(By.id("adder"));
63
64
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(3000));
65
66
add.click();
67
driver.findElement(By.id("box0")); // All is well if this doesn't throw.
68
}
69
70
@Test
71
void testShouldStillFailToFindAnElementWhenImplicitWaitsAreEnabled() {
72
driver.get(pages.dynamicPage);
73
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));
74
assertThatExceptionOfType(NoSuchElementException.class)
75
.isThrownBy(() -> driver.findElement(By.id("box0")));
76
}
77
78
@Test
79
void testShouldReturnAfterFirstAttemptToFindOneAfterDisablingImplicitWaits() {
80
driver.get(pages.dynamicPage);
81
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(3000));
82
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(0));
83
assertThatExceptionOfType(NoSuchElementException.class)
84
.isThrownBy(() -> driver.findElement(By.id("box0")));
85
}
86
87
@Test
88
void testShouldImplicitlyWaitUntilAtLeastOneElementIsFoundWhenSearchingForMany() {
89
driver.get(pages.dynamicPage);
90
WebElement add = driver.findElement(By.id("adder"));
91
92
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(2000));
93
add.click();
94
add.click();
95
96
List<WebElement> elements = driver.findElements(By.className("redbox"));
97
assertThat(elements).isNotEmpty();
98
}
99
100
@Test
101
void testShouldStillFailToFindElementsWhenImplicitWaitsAreEnabled() {
102
driver.get(pages.dynamicPage);
103
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));
104
List<WebElement> elements = driver.findElements(By.className("redbox"));
105
assertThat(elements).isEmpty();
106
}
107
108
@Test
109
void testShouldStillFailToFindElementsByIdWhenImplicitWaitsAreEnabled() {
110
driver.get(pages.dynamicPage);
111
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));
112
List<WebElement> elements = driver.findElements(By.id("redbox"));
113
assertThat(elements).isEmpty();
114
}
115
116
@Test
117
void testShouldReturnAfterFirstAttemptToFindManyAfterDisablingImplicitWaits() {
118
driver.get(pages.dynamicPage);
119
WebElement add = driver.findElement(By.id("adder"));
120
121
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(1100));
122
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(0));
123
add.click();
124
125
List<WebElement> elements = driver.findElements(By.className("redbox"));
126
assertThat(elements).isEmpty();
127
}
128
129
@Test
130
@Ignore(IE)
131
@Ignore(FIREFOX)
132
@NotYetImplemented(SAFARI)
133
public void testShouldImplicitlyWaitForAnElementToBeVisibleBeforeInteracting() {
134
driver.get(pages.dynamicPage);
135
136
WebElement reveal = driver.findElement(By.id("reveal"));
137
WebElement revealed = driver.findElement(By.id("revealed"));
138
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(5000));
139
140
assertThat(revealed.isDisplayed()).isFalse();
141
reveal.click();
142
revealed.sendKeys("hello world");
143
}
144
145
@Test
146
@NotYetImplemented(SAFARI)
147
public void testShouldRetainImplicitlyWaitFromTheReturnedWebDriverOfFrameSwitchTo() {
148
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(1));
149
driver.get(pages.xhtmlTestPage);
150
driver.findElement(By.name("windowOne")).click();
151
152
Wait<WebDriver> wait = new WebDriverWait(driver, Duration.ofSeconds(1));
153
wait.until(ExpectedConditions.numberOfWindowsToBe(2));
154
String handle = (String) driver.getWindowHandles().toArray()[1];
155
156
WebDriver newWindow = driver.switchTo().window(handle);
157
158
long start = System.currentTimeMillis();
159
160
newWindow.findElements(By.id("this-crazy-thing-does-not-exist"));
161
162
long end = System.currentTimeMillis();
163
164
long time = end - start;
165
166
assertThat(time).isGreaterThanOrEqualTo(1000);
167
}
168
}
169
170