diff --git a/java/src/org/openqa/selenium/remote/AbstractDriverOptions.java b/java/src/org/openqa/selenium/remote/AbstractDriverOptions.java index 5e8fca0231024..83a77860a3b7c 100644 --- a/java/src/org/openqa/selenium/remote/AbstractDriverOptions.java +++ b/java/src/org/openqa/selenium/remote/AbstractDriverOptions.java @@ -17,18 +17,6 @@ package org.openqa.selenium.remote; -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.IMPLICIT_TIMEOUT; -import static org.openqa.selenium.remote.CapabilityType.PAGE_LOAD_STRATEGY; -import static org.openqa.selenium.remote.CapabilityType.PAGE_LOAD_TIMEOUT; -import static org.openqa.selenium.remote.CapabilityType.PLATFORM_NAME; -import static org.openqa.selenium.remote.CapabilityType.PROXY; -import static org.openqa.selenium.remote.CapabilityType.SCRIPT_TIMEOUT; -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; - import org.openqa.selenium.MutableCapabilities; import org.openqa.selenium.PageLoadStrategy; import org.openqa.selenium.Proxy; @@ -36,17 +24,25 @@ import org.openqa.selenium.internal.Require; import java.time.Duration; -import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.HashMap; -import java.util.List; 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 { + private Map timeouts = new HashMap<>(); + public DO setBrowserVersion(String browserVersion) { setCapability( BROWSER_VERSION, @@ -61,19 +57,21 @@ public DO setPlatformName(String platformName) { return (DO) this; } - public DO setTimeouts(Map timeouts) { - HashMap convertedTimeouts = new HashMap<>(); - List validTimeouts = new ArrayList<>(Arrays.asList(IMPLICIT_TIMEOUT, PAGE_LOAD_TIMEOUT, SCRIPT_TIMEOUT)); + public DO setImplicitWaitTimeout(Duration timeout) { + this.timeouts.put("implicit", timeout.toMillis()); + setCapability(TIMEOUTS, this.timeouts); + return (DO) this; + } - Require.nonNull("Timeouts", timeouts).forEach((k, v) -> { - if (validTimeouts.contains(k)) { - convertedTimeouts.put(k, v.toMillis()); - } else { - throw new IllegalArgumentException(k + " is not one of the valid timeout values: " + validTimeouts); - } - }); + public DO setPageLoadTimeout(Duration timeout) { + this.timeouts.put("pageLoad", timeout.toMillis()); + setCapability(TIMEOUTS, this.timeouts); + return (DO) this; + } - setCapability(TIMEOUTS, convertedTimeouts); + public DO setScriptTimeout(Duration timeout) { + this.timeouts.put("script", timeout.toMillis()); + setCapability(TIMEOUTS, this.timeouts); return (DO) this; } diff --git a/java/src/org/openqa/selenium/remote/CapabilityType.java b/java/src/org/openqa/selenium/remote/CapabilityType.java index 5c0d5b550ee77..8698b87d9f44b 100644 --- a/java/src/org/openqa/selenium/remote/CapabilityType.java +++ b/java/src/org/openqa/selenium/remote/CapabilityType.java @@ -48,9 +48,6 @@ public interface CapabilityType { String OVERLAPPING_CHECK_DISABLED = "overlappingCheckDisabled"; String STRICT_FILE_INTERACTABILITY = "strictFileInteractability"; String TIMEOUTS = "timeouts"; - String IMPLICIT_TIMEOUT = "implicit"; - String PAGE_LOAD_TIMEOUT = "pageLoad"; - String SCRIPT_TIMEOUT = "script"; String LOGGING_PREFS = "loggingPrefs"; diff --git a/java/test/org/openqa/selenium/chrome/ChromeOptionsTest.java b/java/test/org/openqa/selenium/chrome/ChromeOptionsTest.java index 62a24dc55e005..d09649174d185 100644 --- a/java/test/org/openqa/selenium/chrome/ChromeOptionsTest.java +++ b/java/test/org/openqa/selenium/chrome/ChromeOptionsTest.java @@ -41,9 +41,6 @@ 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.IMPLICIT_TIMEOUT; -import static org.openqa.selenium.remote.CapabilityType.PAGE_LOAD_TIMEOUT; -import static org.openqa.selenium.remote.CapabilityType.SCRIPT_TIMEOUT; @Category(UnitTests.class) public class ChromeOptionsTest { @@ -76,18 +73,15 @@ public void canBuildLogLevelFromStringRepresentation() { @Test public void canAddW3CCompliantOptions() { ChromeOptions chromeOptions = new ChromeOptions(); - chromeOptions.setBrowserVersion("99"); - chromeOptions.setPlatformName("9 3/4"); - chromeOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.IGNORE); - chromeOptions.setAcceptInsecureCerts(true); - chromeOptions.setPageLoadStrategy(PageLoadStrategy.EAGER); - chromeOptions.setStrictFileInteractability(true); - - Map timeouts = new HashMap<>(); - timeouts.put(IMPLICIT_TIMEOUT, Duration.ofSeconds(1)); - timeouts.put(PAGE_LOAD_TIMEOUT, Duration.ofSeconds(2)); - timeouts.put(SCRIPT_TIMEOUT, Duration.ofSeconds(3)); - chromeOptions.setTimeouts(timeouts); + 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"); @@ -98,35 +92,29 @@ public void canAddW3CCompliantOptions() { assertThat(mappedOptions.get("pageLoadStrategy").toString()).isEqualTo("eager"); assertThat(mappedOptions.get("strictFileInteractability")).isEqualTo(true); - Map convertedTimeouts = new HashMap<>(); - convertedTimeouts.put("implicit", 1000L); - convertedTimeouts.put("pageLoad", 2000L); - convertedTimeouts.put("script", 3000L); + Map expectedTimeouts = new HashMap<>(); + expectedTimeouts.put("implicit", 1000L); + expectedTimeouts.put("pageLoad", 2000L); + expectedTimeouts.put("script", 3000L); - assertThat(mappedOptions.get("timeouts")).isEqualTo(convertedTimeouts); + assertThat(expectedTimeouts).isEqualTo(mappedOptions.get("timeouts")); } @Test - public void canAddOneTimeout() { + public void canAddSequentialTimeouts() { ChromeOptions chromeOptions = new ChromeOptions(); - Map timeouts = new HashMap<>(); - timeouts.put("implicit", Duration.ofSeconds(1)); + chromeOptions.setImplicitWaitTimeout(Duration.ofSeconds(1)); - Map convertedTimeouts = new HashMap<>(); - convertedTimeouts.put("implicit", 1000L); - - chromeOptions.setTimeouts(timeouts); Map mappedOptions = chromeOptions.asMap(); - assertThat(mappedOptions.get("timeouts")).isEqualTo(convertedTimeouts); - } + Map expectedTimeouts = new HashMap<>(); - @Test(expected = IllegalArgumentException.class) - public void canNotAddInvalidTimeout() { - ChromeOptions chromeOptions = new ChromeOptions(); - Map timeouts = new HashMap<>(); - timeouts.put("foo", Duration.ofSeconds(1)); + expectedTimeouts.put("implicit", 1000L); + assertThat(expectedTimeouts).isEqualTo(mappedOptions.get("timeouts")); - chromeOptions.setTimeouts(timeouts); + chromeOptions.setPageLoadTimeout(Duration.ofSeconds(2)); + expectedTimeouts.put("pageLoad", 2000L); + Map mappedOptions2 = chromeOptions.asMap(); + assertThat(expectedTimeouts).isEqualTo(mappedOptions2.get("timeouts")); } @Test