From b86d8475b409e7fb27565f3312b4b67226f2f656 Mon Sep 17 00:00:00 2001 From: Titus Date: Mon, 20 Sep 2021 14:38:32 -0500 Subject: [PATCH] Add methods to options classes for w3c compliant capabilities (#9828) --- .../remote/AbstractDriverOptions.java | 66 +++++++++++++++++-- .../selenium/remote/CapabilityType.java | 1 + .../org/openqa/selenium/chrome/BUILD.bazel | 4 ++ .../selenium/chrome/ChromeOptionsTest.java | 66 ++++++++++++++++++- 4 files changed, 129 insertions(+), 8 deletions(-) diff --git a/java/src/org/openqa/selenium/remote/AbstractDriverOptions.java b/java/src/org/openqa/selenium/remote/AbstractDriverOptions.java index 8c31fb2e65637..9eb71c8ce2b8d 100644 --- a/java/src/org/openqa/selenium/remote/AbstractDriverOptions.java +++ b/java/src/org/openqa/selenium/remote/AbstractDriverOptions.java @@ -17,26 +17,67 @@ package org.openqa.selenium.remote; -import static org.openqa.selenium.remote.CapabilityType.ACCEPT_INSECURE_CERTS; -import static org.openqa.selenium.remote.CapabilityType.PAGE_LOAD_STRATEGY; -import static org.openqa.selenium.remote.CapabilityType.PROXY; -import static org.openqa.selenium.remote.CapabilityType.STRICT_FILE_INTERACTABILITY; -import static org.openqa.selenium.remote.CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR; -import static org.openqa.selenium.remote.CapabilityType.UNHANDLED_PROMPT_BEHAVIOUR; - import org.openqa.selenium.MutableCapabilities; import org.openqa.selenium.PageLoadStrategy; import org.openqa.selenium.Proxy; import org.openqa.selenium.UnexpectedAlertBehaviour; import org.openqa.selenium.internal.Require; +import java.time.Duration; import java.util.Collections; +import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util.TreeMap; import java.util.TreeSet; +import static org.openqa.selenium.remote.CapabilityType.ACCEPT_INSECURE_CERTS; +import static org.openqa.selenium.remote.CapabilityType.BROWSER_VERSION; +import static org.openqa.selenium.remote.CapabilityType.PAGE_LOAD_STRATEGY; +import static org.openqa.selenium.remote.CapabilityType.PLATFORM_NAME; +import static org.openqa.selenium.remote.CapabilityType.PROXY; +import static org.openqa.selenium.remote.CapabilityType.STRICT_FILE_INTERACTABILITY; +import static org.openqa.selenium.remote.CapabilityType.TIMEOUTS; +import static org.openqa.selenium.remote.CapabilityType.UNHANDLED_PROMPT_BEHAVIOUR; + public abstract class AbstractDriverOptions extends MutableCapabilities { + public DO setBrowserVersion(String browserVersion) { + setCapability( + BROWSER_VERSION, + Require.nonNull("Browser version", browserVersion)); + return (DO) this; + } + + public DO setPlatformName(String platformName) { + setCapability( + PLATFORM_NAME, + Require.nonNull("Platform Name", platformName)); + return (DO) this; + } + + public DO setImplicitWaitTimeout(Duration timeout) { + Map timeouts = getTimeouts(); + timeouts.put("implicit", timeout.toMillis()); + + setCapability(TIMEOUTS, Collections.unmodifiableMap(timeouts)); + return (DO) this; + } + + public DO setPageLoadTimeout(Duration timeout) { + Map timeouts = getTimeouts(); + timeouts.put("pageLoad", timeout.toMillis()); + + setCapability(TIMEOUTS, Collections.unmodifiableMap(timeouts)); + return (DO) this; + } + + public DO setScriptTimeout(Duration timeout) { + Map timeouts = getTimeouts(); + timeouts.put("script", timeout.toMillis()); + + setCapability(TIMEOUTS, Collections.unmodifiableMap(timeouts)); + return (DO) this; + } public DO setPageLoadStrategy(PageLoadStrategy strategy) { setCapability( @@ -94,4 +135,15 @@ public Map asMap() { getExtraCapabilityNames().forEach(name -> toReturn.put(name, getCapability(name))); return Collections.unmodifiableMap(toReturn); } + + private Map getTimeouts() { + Map newTimeouts = new HashMap<>(); + Object raw = getCapability(TIMEOUTS); + ((Map) raw).forEach((key, value) -> { + if (key instanceof String && value instanceof Number) { + newTimeouts.put((String) key, (Number) value); + } + }); + return newTimeouts; + } } diff --git a/java/src/org/openqa/selenium/remote/CapabilityType.java b/java/src/org/openqa/selenium/remote/CapabilityType.java index 58d957def79fe..8698b87d9f44b 100644 --- a/java/src/org/openqa/selenium/remote/CapabilityType.java +++ b/java/src/org/openqa/selenium/remote/CapabilityType.java @@ -47,6 +47,7 @@ public interface CapabilityType { String HAS_TOUCHSCREEN = "hasTouchScreen"; String OVERLAPPING_CHECK_DISABLED = "overlappingCheckDisabled"; String STRICT_FILE_INTERACTABILITY = "strictFileInteractability"; + String TIMEOUTS = "timeouts"; String LOGGING_PREFS = "loggingPrefs"; diff --git a/java/test/org/openqa/selenium/chrome/BUILD.bazel b/java/test/org/openqa/selenium/chrome/BUILD.bazel index 69d3153efb518..271f0d990bc08 100644 --- a/java/test/org/openqa/selenium/chrome/BUILD.bazel +++ b/java/test/org/openqa/selenium/chrome/BUILD.bazel @@ -22,4 +22,8 @@ java_selenium_test_suite( artifact("org.assertj:assertj-core"), artifact("org.mockito:mockito-core"), ], + javacopts = [ + "--release", + "11", + ], ) diff --git a/java/test/org/openqa/selenium/chrome/ChromeOptionsTest.java b/java/test/org/openqa/selenium/chrome/ChromeOptionsTest.java index 2c208f297f0ca..60e5ab8737c9d 100644 --- a/java/test/org/openqa/selenium/chrome/ChromeOptionsTest.java +++ b/java/test/org/openqa/selenium/chrome/ChromeOptionsTest.java @@ -19,17 +19,20 @@ import org.junit.Test; import org.junit.experimental.categories.Category; +import org.openqa.selenium.PageLoadStrategy; +import org.openqa.selenium.UnexpectedAlertBehaviour; import org.openqa.selenium.remote.AcceptedW3CCapabilityKeys; import org.openqa.selenium.testing.TestUtilities; import org.openqa.selenium.testing.UnitTests; import java.io.File; +import java.time.Duration; import java.util.Base64; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import java.util.function.Predicate; -import java.util.stream.Collectors; import static java.util.stream.Collectors.toSet; import static org.assertj.core.api.Assertions.assertThat; @@ -38,6 +41,7 @@ import static org.assertj.core.api.InstanceOfAssertFactories.MAP; import static org.openqa.selenium.chrome.ChromeDriverLogLevel.OFF; import static org.openqa.selenium.chrome.ChromeDriverLogLevel.SEVERE; +import static org.openqa.selenium.remote.CapabilityType.TIMEOUTS; @Category(UnitTests.class) public class ChromeOptionsTest { @@ -67,6 +71,66 @@ public void canBuildLogLevelFromStringRepresentation() { assertThat(ChromeDriverLogLevel.fromString("SEVERE")).isEqualTo(SEVERE); } + @Test + public void canAddW3CCompliantOptions() { + ChromeOptions chromeOptions = new ChromeOptions(); + chromeOptions.setBrowserVersion("99") + .setPlatformName("9 3/4") + .setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.IGNORE) + .setAcceptInsecureCerts(true) + .setPageLoadStrategy(PageLoadStrategy.EAGER) + .setStrictFileInteractability(true) + .setImplicitWaitTimeout(Duration.ofSeconds(1)) + .setPageLoadTimeout(Duration.ofSeconds(2)) + .setScriptTimeout(Duration.ofSeconds(3)); + + Map mappedOptions = chromeOptions.asMap(); + assertThat(mappedOptions.get("browserName")).isEqualTo("chrome"); + assertThat(mappedOptions.get("browserVersion")).isEqualTo("99"); + assertThat(mappedOptions.get("platformName")).isEqualTo("9 3/4"); + assertThat(mappedOptions.get("unhandledPromptBehavior").toString()).isEqualTo("ignore"); + assertThat(mappedOptions.get("acceptInsecureCerts")).isEqualTo(true); + assertThat(mappedOptions.get("pageLoadStrategy").toString()).isEqualTo("eager"); + assertThat(mappedOptions.get("strictFileInteractability")).isEqualTo(true); + + Map expectedTimeouts = new HashMap<>(); + expectedTimeouts.put("implicit", 1000L); + expectedTimeouts.put("pageLoad", 2000L); + expectedTimeouts.put("script", 3000L); + + assertThat(expectedTimeouts).isEqualTo(mappedOptions.get("timeouts")); + } + + @Test + public void canAddSequentialTimeouts() { + ChromeOptions chromeOptions = new ChromeOptions(); + chromeOptions.setImplicitWaitTimeout(Duration.ofSeconds(1)); + + Map mappedOptions = chromeOptions.asMap(); + Map expectedTimeouts = new HashMap<>(); + + expectedTimeouts.put("implicit", 1000L); + assertThat(expectedTimeouts).isEqualTo(mappedOptions.get("timeouts")); + + chromeOptions.setPageLoadTimeout(Duration.ofSeconds(2)); + expectedTimeouts.put("pageLoad", 2000L); + Map mappedOptions2 = chromeOptions.asMap(); + assertThat(expectedTimeouts).isEqualTo(mappedOptions2.get("timeouts")); + } + + @Test + public void mixAddingTimeoutsCapsAndSetter() { + ChromeOptions chromeOptions = new ChromeOptions(); + chromeOptions.setCapability(TIMEOUTS, Map.of("implicit", 1000)); + chromeOptions.setPageLoadTimeout(Duration.ofSeconds(2)); + + Map expectedTimeouts = new HashMap<>(); + expectedTimeouts.put("implicit", 1000); + expectedTimeouts.put("pageLoad", 2000L); + + assertThat(chromeOptions.asMap().get("timeouts")).isEqualTo(expectedTimeouts); + } + @Test public void mergingOptionsMergesArguments() { ChromeOptions one = new ChromeOptions().addArguments("verbose");