Skip to content

Commit

Permalink
Add methods to options classes for w3c compliant capabilities (#9828)
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed Sep 20, 2021
1 parent 2c071c8 commit b86d847
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 8 deletions.
66 changes: 59 additions & 7 deletions java/src/org/openqa/selenium/remote/AbstractDriverOptions.java
Expand Up @@ -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<DO extends 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<String, Number> timeouts = getTimeouts();
timeouts.put("implicit", timeout.toMillis());

setCapability(TIMEOUTS, Collections.unmodifiableMap(timeouts));
return (DO) this;
}

public DO setPageLoadTimeout(Duration timeout) {
Map<String, Number> timeouts = getTimeouts();
timeouts.put("pageLoad", timeout.toMillis());

setCapability(TIMEOUTS, Collections.unmodifiableMap(timeouts));
return (DO) this;
}

public DO setScriptTimeout(Duration timeout) {
Map<String, Number> timeouts = getTimeouts();
timeouts.put("script", timeout.toMillis());

setCapability(TIMEOUTS, Collections.unmodifiableMap(timeouts));
return (DO) this;
}

public DO setPageLoadStrategy(PageLoadStrategy strategy) {
setCapability(
Expand Down Expand Up @@ -94,4 +135,15 @@ public Map<String, Object> asMap() {
getExtraCapabilityNames().forEach(name -> toReturn.put(name, getCapability(name)));
return Collections.unmodifiableMap(toReturn);
}

private Map<String, Number> getTimeouts() {
Map<String, Number> 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;
}
}
1 change: 1 addition & 0 deletions java/src/org/openqa/selenium/remote/CapabilityType.java
Expand Up @@ -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";

Expand Down
4 changes: 4 additions & 0 deletions java/test/org/openqa/selenium/chrome/BUILD.bazel
Expand Up @@ -22,4 +22,8 @@ java_selenium_test_suite(
artifact("org.assertj:assertj-core"),
artifact("org.mockito:mockito-core"),
],
javacopts = [
"--release",
"11",
],
)
66 changes: 65 additions & 1 deletion java/test/org/openqa/selenium/chrome/ChromeOptionsTest.java
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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<String, Object> 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<String, Long> 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<String, Object> mappedOptions = chromeOptions.asMap();
Map<String, Long> expectedTimeouts = new HashMap<>();

expectedTimeouts.put("implicit", 1000L);
assertThat(expectedTimeouts).isEqualTo(mappedOptions.get("timeouts"));

chromeOptions.setPageLoadTimeout(Duration.ofSeconds(2));
expectedTimeouts.put("pageLoad", 2000L);
Map<String, Object> 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<String, Number> 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");
Expand Down

0 comments on commit b86d847

Please sign in to comment.