Skip to content

Commit ecd7297

Browse files
authored
Upgrade to Selenium 4.33.0; fix timeout breaks (#295)
1 parent 7d5acd0 commit ecd7297

File tree

5 files changed

+106
-20
lines changed

5 files changed

+106
-20
lines changed

selenium4Deps.gradle

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ sourceSets {
2121
}
2222
}
2323
}
24-
24+
2525
dependencies {
2626
constraints {
2727
api 'com.nordstrom.tools:testng-foundation:5.2.1-j11'
28-
api 'org.seleniumhq.selenium:selenium-grid:4.32.0'
29-
api 'org.seleniumhq.selenium:selenium-support:4.32.0'
30-
api 'org.seleniumhq.selenium:selenium-chrome-driver:4.32.0'
31-
api 'org.seleniumhq.selenium:selenium-edge-driver:4.32.0'
32-
api 'org.seleniumhq.selenium:selenium-firefox-driver:4.32.0'
28+
api 'org.seleniumhq.selenium:selenium-grid:4.33.0'
29+
api 'org.seleniumhq.selenium:selenium-support:4.33.0'
30+
api 'org.seleniumhq.selenium:selenium-chrome-driver:4.33.0'
31+
api 'org.seleniumhq.selenium:selenium-edge-driver:4.33.0'
32+
api 'org.seleniumhq.selenium:selenium-firefox-driver:4.33.0'
3333
api 'org.seleniumhq.selenium:selenium-opera-driver:4.4.0'
34-
api 'org.seleniumhq.selenium:selenium-safari-driver:4.32.0'
34+
api 'org.seleniumhq.selenium:selenium-safari-driver:4.33.0'
3535
api 'com.nordstrom.ui-tools:htmlunit-remote:4.32.0'
3636
api 'org.seleniumhq.selenium:htmlunit3-driver:4.32.0'
3737
api 'org.htmlunit:htmlunit:4.12.0'

src/main/java/com/nordstrom/automation/selenium/core/DriverManager.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.lang.reflect.Method;
44
import java.time.Duration;
55
import java.util.Optional;
6-
import java.util.concurrent.TimeUnit;
76
import java.util.function.Function;
87

98
import org.openqa.selenium.JavascriptExecutor;
@@ -180,13 +179,8 @@ public static WebDriver injectDriver(TestBase instance, final Method method) {
180179
*/
181180
public static void setDriverTimeouts(final WebDriver driver, final SeleniumConfig config) {
182181
Timeouts timeouts = driver.manage().timeouts();
183-
timeouts.implicitlyWait(WaitType.IMPLIED.getInterval(config), TimeUnit.SECONDS);
184-
185-
try {
186-
timeouts.pageLoadTimeout(WaitType.PAGE_LOAD.getInterval(config), TimeUnit.SECONDS);
187-
} catch (WebDriverException eaten) {
188-
// unsupported feature: nothing to do here
189-
}
182+
WebDriverUtils.implicitlyWait(timeouts, Duration.ofSeconds(WaitType.IMPLIED.getInterval(config)));
183+
WebDriverUtils.pageLoadTimeout(timeouts, Duration.ofSeconds(WaitType.PAGE_LOAD.getInterval(config)));
190184
}
191185

192186
/**

src/main/java/com/nordstrom/automation/selenium/model/RobustElementWrapper.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22

33
import java.lang.reflect.InvocationTargetException;
44
import java.lang.reflect.Method;
5+
import java.time.Duration;
56
import java.util.Arrays;
67
import java.util.List;
78
import java.util.Objects;
8-
import java.util.concurrent.TimeUnit;
9-
109
import org.openqa.selenium.By;
1110
import org.openqa.selenium.NoSuchElementException;
1211
import org.openqa.selenium.SearchContext;
@@ -333,14 +332,14 @@ public String toString() {
333332
* @throws StaleElementReferenceElementException if container element has gone stale
334333
* @throws NoSuchElementException if unable to find element specified by the wrapper
335334
*/
336-
@SuppressWarnings("deprecation")
337335
private static RobustElementWrapper acquireReference(final RobustElementWrapper wrapper) {
338336
NoSuchElementException thrown = null;
339337
SearchContext context = wrapper.context.getWrappedContext();
340338

341339
if (wrapper.strategy == Strategy.LOCATOR) {
342340
// disable implicit wait
343-
Timeouts timeouts = wrapper.driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
341+
Timeouts timeouts = wrapper.driver.manage().timeouts();
342+
WebDriverUtils.implicitlyWait(timeouts, Duration.ZERO);
344343
try {
345344
// if index specified
346345
if (wrapper.index > 0) {
@@ -370,7 +369,7 @@ private static RobustElementWrapper acquireReference(final RobustElementWrapper
370369
}
371370
} finally {
372371
// restore implicit wait
373-
timeouts.implicitlyWait(WaitType.IMPLIED.getInterval(), TimeUnit.SECONDS);
372+
WebDriverUtils.implicitlyWait(timeouts, Duration.ofSeconds(WaitType.IMPLIED.getInterval()));
374373
}
375374
} else {
376375
// if context is a container element

src/selenium3/java/com/nordstrom/automation/selenium/core/WebDriverUtils.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
package com.nordstrom.automation.selenium.core;
22

3+
import java.time.Duration;
34
import java.util.Arrays;
45
import java.util.Collections;
56
import java.util.Iterator;
67
import java.util.List;
8+
import java.util.concurrent.TimeUnit;
79
import java.util.regex.Matcher;
810
import java.util.regex.Pattern;
911

1012
import org.openqa.selenium.Capabilities;
1113
import org.openqa.selenium.ElementNotVisibleException;
1214
import org.openqa.selenium.HasCapabilities;
1315
import org.openqa.selenium.JavascriptExecutor;
16+
import org.openqa.selenium.NoSuchElementException;
1417
import org.openqa.selenium.NotFoundException;
1518
import org.openqa.selenium.Platform;
1619
import org.openqa.selenium.SearchContext;
@@ -21,6 +24,7 @@
2124
import org.openqa.selenium.WebDriverException;
2225
import org.openqa.selenium.WebElement;
2326
import org.openqa.selenium.WrapsDriver;
27+
import org.openqa.selenium.WebDriver.Timeouts;
2428
import org.openqa.selenium.remote.CapabilityType;
2529

2630
import com.nordstrom.automation.selenium.model.RobustJavascriptExecutor;
@@ -146,6 +150,49 @@ public static String getDomAttributeOf(final WebElement element, final String na
146150
return JsUtility.runAndReturn(getDriver(element), script, element);
147151
}
148152

153+
/**
154+
* Specifies the amount of time the driver should wait when searching for an element if it is
155+
* not immediately present.
156+
*
157+
* <p>When searching for a single element, the driver should poll the page until the element has
158+
* been found, or this timeout expires before throwing a {@link NoSuchElementException}. When
159+
* searching for multiple elements, the driver should poll the page until at least one element
160+
* has been found or this timeout has expired.
161+
*
162+
* <p>Increasing the implicit wait timeout should be used judiciously as it will have an adverse
163+
* effect on test run time, especially when used with slower location strategies like XPath.
164+
*
165+
* <p>If the timeout is negative, not null, or greater than 2e16 - 1, an error code with invalid
166+
* argument will be returned.
167+
*
168+
* @param timeouts target {@link Timeouts} object
169+
* @param duration The duration to wait.
170+
* @return A self reference.
171+
*/
172+
public static Timeouts implicitlyWait(final Timeouts timeouts, final Duration duration) {
173+
return timeouts.implicitlyWait(duration.getSeconds(), TimeUnit.SECONDS);
174+
}
175+
176+
/**
177+
* Sets the amount of time to wait for a page load to complete before throwing an error. If the
178+
* timeout is negative, not null, or greater than 2e16 - 1, an error code with invalid argument
179+
* will be returned.
180+
*
181+
* @param timeouts target {@link Timeouts} object
182+
* @param duration The timeout value.
183+
* @return A Timeouts interface.
184+
* @see <a href="https://www.w3.org/TR/webdriver/#set-timeouts">W3C WebDriver</a>
185+
* @see <a href="https://www.w3.org/TR/webdriver/#dfn-timeouts-configuration">W3C WebDriver</a>
186+
*/
187+
public static Timeouts pageLoadTimeout(final Timeouts timeouts, final Duration duration) {
188+
try {
189+
return timeouts.pageLoadTimeout(duration.getSeconds(), TimeUnit.SECONDS);
190+
} catch (WebDriverException eaten) {
191+
// unsupported
192+
return timeouts;
193+
}
194+
}
195+
149196
/**
150197
* Remove hidden elements from specified list.
151198
*

src/selenium4/java/com/nordstrom/automation/selenium/core/WebDriverUtils.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.nordstrom.automation.selenium.core;
22

3+
import java.time.Duration;
34
import java.util.Arrays;
45
import java.util.Collections;
56
import java.util.Iterator;
@@ -11,13 +12,15 @@
1112
import org.openqa.selenium.ElementNotInteractableException;
1213
import org.openqa.selenium.HasCapabilities;
1314
import org.openqa.selenium.JavascriptExecutor;
15+
import org.openqa.selenium.NoSuchElementException;
1416
import org.openqa.selenium.NotFoundException;
1517
import org.openqa.selenium.Platform;
1618
import org.openqa.selenium.SearchContext;
1719
import org.openqa.selenium.StaleElementReferenceException;
1820
import org.openqa.selenium.TimeoutException;
1921
import org.openqa.selenium.UnhandledAlertException;
2022
import org.openqa.selenium.WebDriver;
23+
import org.openqa.selenium.WebDriver.Timeouts;
2124
import org.openqa.selenium.WebDriverException;
2225
import org.openqa.selenium.WebElement;
2326
import org.openqa.selenium.WrapsDriver;
@@ -150,6 +153,49 @@ public static String getDomAttributeOf(final WebElement element, final String na
150153
return element.getDomAttribute(name);
151154
}
152155

156+
/**
157+
* Specifies the amount of time the driver should wait when searching for an element if it is
158+
* not immediately present.
159+
*
160+
* <p>When searching for a single element, the driver should poll the page until the element has
161+
* been found, or this timeout expires before throwing a {@link NoSuchElementException}. When
162+
* searching for multiple elements, the driver should poll the page until at least one element
163+
* has been found or this timeout has expired.
164+
*
165+
* <p>Increasing the implicit wait timeout should be used judiciously as it will have an adverse
166+
* effect on test run time, especially when used with slower location strategies like XPath.
167+
*
168+
* <p>If the timeout is negative, not null, or greater than 2e16 - 1, an error code with invalid
169+
* argument will be returned.
170+
*
171+
* @param timeouts target {@link Timeouts} object
172+
* @param duration The duration to wait.
173+
* @return A self reference.
174+
*/
175+
public static Timeouts implicitlyWait(final Timeouts timeouts, final Duration duration) {
176+
return timeouts.implicitlyWait(duration);
177+
}
178+
179+
/**
180+
* Sets the amount of time to wait for a page load to complete before throwing an error. If the
181+
* timeout is negative, not null, or greater than 2e16 - 1, an error code with invalid argument
182+
* will be returned.
183+
*
184+
* @param timeouts target {@link Timeouts} object
185+
* @param duration The timeout value.
186+
* @return A Timeouts interface.
187+
* @see <a href="https://www.w3.org/TR/webdriver/#set-timeouts">W3C WebDriver</a>
188+
* @see <a href="https://www.w3.org/TR/webdriver/#dfn-timeouts-configuration">W3C WebDriver</a>
189+
*/
190+
public static Timeouts pageLoadTimeout(final Timeouts timeouts, final Duration duration) {
191+
try {
192+
return timeouts.pageLoadTimeout(duration);
193+
} catch (WebDriverException eaten) {
194+
// unsupported
195+
return timeouts;
196+
}
197+
}
198+
153199
/**
154200
* Remove hidden elements from specified list.
155201
*

0 commit comments

Comments
 (0)