Skip to content

Commit

Permalink
Replacing DesiredCapabilities with MutableCapabilities or ImmutableCa…
Browse files Browse the repository at this point in the history
…pabilities where it's possible in Java binding
  • Loading branch information
barancev committed Sep 28, 2017
1 parent 17dd085 commit d0122c5
Show file tree
Hide file tree
Showing 18 changed files with 62 additions and 74 deletions.
4 changes: 2 additions & 2 deletions java/client/src/org/openqa/selenium/chrome/ChromeOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
import com.google.common.collect.Maps;
import com.google.common.io.Files;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.SessionNotCreatedException;
import org.openqa.selenium.UnexpectedAlertBehaviour;
import org.openqa.selenium.remote.BrowserType;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -68,7 +68,7 @@
public class ChromeOptions extends MutableCapabilities {

/**
* Key used to store a set of ChromeOptions in a {@link DesiredCapabilities}
* Key used to store a set of ChromeOptions in a {@link Capabilities}
* object.
*/
public static final String CAPABILITY = "goog:chromeOptions";
Expand Down
4 changes: 2 additions & 2 deletions java/client/src/org/openqa/selenium/edge/EdgeOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@

import static org.openqa.selenium.remote.CapabilityType.PAGE_LOAD_STRATEGY;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.Platform;
import org.openqa.selenium.remote.BrowserType;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.util.Objects;

Expand All @@ -47,7 +47,7 @@
public class EdgeOptions extends MutableCapabilities {

/**
* Key used to store a set of EdgeOptions in a {@link DesiredCapabilities} object.
* Key used to store a set of EdgeOptions in a {@link Capabilities} object.
*/
public static final String CAPABILITY = "edgeOptions";

Expand Down
10 changes: 5 additions & 5 deletions java/client/src/org/openqa/selenium/firefox/FirefoxDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.remote.CommandExecutor;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.FileDetector;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.service.DriverCommandExecutor;
Expand Down Expand Up @@ -182,17 +182,17 @@ private static Boolean forceMarionetteFromSystemProperty() {
*/
private static Capabilities dropCapabilities(Capabilities capabilities) {
if (capabilities == null) {
return new DesiredCapabilities();
return new ImmutableCapabilities();
}

DesiredCapabilities caps;
MutableCapabilities caps;

if (isLegacy(capabilities)) {
final Set<String> toRemove = Sets.newHashSet(BINARY, PROFILE);
caps = new DesiredCapabilities(
caps = new MutableCapabilities(
Maps.filterKeys(capabilities.asMap(), key -> !toRemove.contains(key)));
} else {
caps = new DesiredCapabilities(capabilities);
caps = new MutableCapabilities(capabilities);
}

// Ensure that the proxy is in a state fit to be sent to the extension
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.google.gson.stream.JsonWriter;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.SessionNotCreatedException;
import org.openqa.selenium.WebDriverException;
Expand Down Expand Up @@ -91,15 +92,12 @@ public class ProtocolHandshake {
.map(Pattern::asPredicate)
.reduce(identity -> false, Predicate::or);

private final static Type MAP_TYPE = new TypeToken<Map<?, ?>>(){}.getType();


public Result createSession(HttpClient client, Command command)
throws IOException {
Capabilities desired = (Capabilities) command.getParameters().get("desiredCapabilities");
desired = desired == null ? new DesiredCapabilities() : desired;
desired = desired == null ? new ImmutableCapabilities() : desired;
Capabilities required = (Capabilities) command.getParameters().get("requiredCapabilities");
required = required == null ? new DesiredCapabilities() : required;
required = required == null ? new ImmutableCapabilities() : required;

BeanToJsonConverter converter = new BeanToJsonConverter();
JsonObject des = (JsonObject) converter.convertObject(desired);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.openqa.selenium.Cookie;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.HasCapabilities;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.NoSuchFrameException;
Expand Down Expand Up @@ -108,7 +109,7 @@ public class RemoteWebDriver implements WebDriver, JavascriptExecutor,

// For cglib
protected RemoteWebDriver() {
init(new DesiredCapabilities());
init(new ImmutableCapabilities());
}

public RemoteWebDriver(Capabilities desiredCapabilities) {
Expand Down Expand Up @@ -154,7 +155,7 @@ public RemoteWebDriver(URL remoteAddress, Capabilities desiredCapabilities) {
}

private void init(Capabilities capabilities) {
capabilities = capabilities == null ? new DesiredCapabilities() : capabilities;
capabilities = capabilities == null ? new ImmutableCapabilities() : capabilities;

logger.addHandler(LoggingHandler.getInstance());

Expand Down
5 changes: 2 additions & 3 deletions java/client/src/org/openqa/selenium/safari/SafariDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.remote.CommandExecutor;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.FileDetector;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.service.DriverCommandExecutor;
Expand All @@ -41,9 +40,9 @@ public SafariDriver() {
}

/**
* Converts the specified {@link DesiredCapabilities} to a {@link SafariOptions}
* Converts the specified {@link Capabilities} to a {@link SafariOptions}
* instance and initializes a new SafariDriver using these options.
* @see SafariOptions#fromCapabilities(org.openqa.selenium.Capabilities)
* @see SafariOptions#fromCapabilities(Capabilities)
*
* @param desiredCapabilities capabilities requested of the driver
* @deprecated Use {@link SafariDriver(SafariOptions)} instead.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.io.IOException;
import java.util.Map;
Expand All @@ -48,7 +47,7 @@
public class SafariOptions extends MutableCapabilities {

/**
* Key used to store SafariOptions in a {@link DesiredCapabilities} object.
* Key used to store SafariOptions in a {@link Capabilities} object.
*/
public static final String CAPABILITY = "safari.options";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.ParallelTestRunner;
import org.openqa.selenium.ParallelTestRunner.Worker;
Expand All @@ -54,7 +55,6 @@
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.Command;
import org.openqa.selenium.remote.CommandExecutor;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.DriverCommand;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.SessionId;
Expand Down Expand Up @@ -118,10 +118,9 @@ public void canStartDriverWithSpecifiedProfile() {

@Test
public void canPassCapabilities() {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PAGE_LOAD_STRATEGY, "none");
Capabilities caps = new ImmutableCapabilities(CapabilityType.PAGE_LOAD_STRATEGY, "none");

localDriver = new FirefoxDriver(capabilities);
localDriver = new FirefoxDriver(caps);

assertEquals(
"none",
Expand Down Expand Up @@ -156,8 +155,7 @@ public void canSetProfileInCapabilities() {
profile.setPreference("browser.startup.page", 1);
profile.setPreference("browser.startup.homepage", pages.xhtmlTestPage);

DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(FirefoxDriver.PROFILE, profile);
Capabilities caps = new ImmutableCapabilities(FirefoxDriver.PROFILE, profile);

localDriver = new FirefoxDriver(caps);
wait.until($ -> "XHTML Test Page".equals(localDriver.getTitle()));
Expand All @@ -167,8 +165,7 @@ public void canSetProfileInCapabilities() {
@Ignore(value = MARIONETTE, reason = "Assumed to be covered by tests for GeckoDriverService")
public void canSetBinaryInCapabilities() throws IOException {
FirefoxBinary binary = spy(new FirefoxBinary());
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(FirefoxDriver.BINARY, binary);
Capabilities caps = new ImmutableCapabilities(FirefoxDriver.BINARY, binary);

localDriver = new FirefoxDriver(caps);

Expand All @@ -178,8 +175,7 @@ public void canSetBinaryInCapabilities() throws IOException {
@Test
public void canSetBinaryPathInCapabilities() throws IOException {
String binPath = new FirefoxBinary().getPath();
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(FirefoxDriver.BINARY, binPath);
Capabilities caps = new ImmutableCapabilities(FirefoxDriver.BINARY, binPath);

localDriver = new FirefoxDriver(caps);
}
Expand Down Expand Up @@ -568,9 +564,8 @@ public void searchingByCssDoesNotOverwriteExistingSizzleDefinition() {

@Test
public void testFirefoxCanNativelyClickOverlappingElements() {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.OVERLAPPING_CHECK_DISABLED, true);
WebDriver secondDriver = new FirefoxDriver(capabilities);
Capabilities caps = new ImmutableCapabilities(CapabilityType.OVERLAPPING_CHECK_DISABLED, true);
WebDriver secondDriver = new FirefoxDriver(caps);
try {
secondDriver.get(appServer.whereIs("click_tests/overlapping_elements.html"));
secondDriver.findElement(By.id("under")).click();
Expand Down
19 changes: 8 additions & 11 deletions java/client/test/org/openqa/selenium/firefox/MarionetteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@

import org.junit.After;
import org.junit.Test;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.testing.Ignore;
import org.openqa.selenium.testing.JUnit4TestBase;

Expand Down Expand Up @@ -102,10 +103,9 @@ public void canStartDriverWithSpecifiedBinaryAndProfile() throws IOException {

@Test
public void canPassCapabilities() {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PAGE_LOAD_STRATEGY, "none");
Capabilities caps = new ImmutableCapabilities(CapabilityType.PAGE_LOAD_STRATEGY, "none");

localDriver = new FirefoxDriver(capabilities);
localDriver = new FirefoxDriver(caps);

verifyItIsMarionette(localDriver);
assertEquals(
Expand Down Expand Up @@ -144,8 +144,7 @@ public void canSetProfileInCapabilities() {
profile.setPreference("browser.startup.page", 1);
profile.setPreference("browser.startup.homepage", pages.xhtmlTestPage);

DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(FirefoxDriver.PROFILE, profile);
Capabilities caps = new ImmutableCapabilities(FirefoxDriver.PROFILE, profile);

localDriver = new FirefoxDriver(caps);
wait.until($ -> "XHTML Test Page".equals(localDriver.getTitle()));
Expand All @@ -159,8 +158,7 @@ public void canUseSameProfileInCapabilitiesAndDirectly() {
profile.setPreference("browser.startup.page", 1);
profile.setPreference("browser.startup.homepage", pages.xhtmlTestPage);

DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(FirefoxDriver.PROFILE, profile);
Capabilities caps = new ImmutableCapabilities(FirefoxDriver.PROFILE, profile);

localDriver = new FirefoxDriver(
new FirefoxOptions()
Expand All @@ -179,14 +177,13 @@ public void canPassCapabilitiesBinaryAndProfileSeparately() throws IOException {
profile.setPreference("browser.startup.page", 1);
profile.setPreference("browser.startup.homepage", pages.xhtmlTestPage);

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PAGE_LOAD_STRATEGY, "none");
Capabilities caps = new ImmutableCapabilities(CapabilityType.PAGE_LOAD_STRATEGY, "none");

localDriver = new FirefoxDriver(
new FirefoxOptions()
.setBinary(binary)
.setProfile(profile)
.merge(capabilities));
.merge(caps));
wait.until($ -> "XHTML Test Page".equals(localDriver.getTitle()));

verifyItIsMarionette(localDriver);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@

package org.openqa.selenium.htmlunit;

import static org.openqa.selenium.remote.CapabilityType.SUPPORTS_JAVASCRIPT;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.StandardSeleniumTests;
import org.openqa.selenium.remote.DesiredCapabilities;

@RunWith(Suite.class)
@Suite.SuiteClasses({
Expand All @@ -40,8 +42,8 @@ public HtmlUnitDriverForTest(Capabilities desiredCapabilities, Capabilities requ
}

private static Capabilities tweak(Capabilities capabilities) {
DesiredCapabilities caps = new DesiredCapabilities(capabilities);
caps.setJavascriptEnabled(true);
MutableCapabilities caps = new MutableCapabilities(capabilities);
caps.setCapability(SUPPORTS_JAVASCRIPT, true);
return caps;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.testing.JUnit4TestBase;
import org.openqa.selenium.testing.NeedsLocalEnvironment;
import org.openqa.selenium.testing.NoDriverAfterTest;
Expand Down Expand Up @@ -72,8 +73,7 @@ public void testPersistentHoverCanBeTurnedOff() throws Exception {
// stopped.
driver.quit();

DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(ENABLE_PERSISTENT_HOVERING, false);
Capabilities caps = new ImmutableCapabilities(ENABLE_PERSISTENT_HOVERING, false);
WebDriverBuilder builder = new WebDriverBuilder().setDesiredCapabilities(caps);
driver = builder.get();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.remote.BeanToJsonConverter;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.JsonToBeanConverter;

import java.util.Map;
Expand Down Expand Up @@ -72,8 +72,8 @@ public void shouldMirrorCapabilitiesFromPassedInIeOptions() {

@Test
public void shouldPopulateIeOptionsFromExistingCapabilitiesWhichLackThem() {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
Capabilities caps = new ImmutableCapabilities(
INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);

InternetExplorerOptions options = new InternetExplorerOptions(caps);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.junit.Test;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
Expand Down Expand Up @@ -62,10 +63,8 @@ public void quitDriver() {

@Test
public void canSetProfileThroughDesiredCapabilities() {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setBrowserName(DesiredCapabilities.firefox().getBrowserName());
MutableCapabilities caps = DesiredCapabilities.firefox();
caps.setCapability(FirefoxDriver.PROFILE, new FirefoxProfile());
caps.setCapability(FirefoxDriver.MARIONETTE, true);

localDriver = new RemoteWebDriver(remoteUrl, caps);
localDriver.get(pages.xhtmlTestPage);
Expand Down
Loading

0 comments on commit d0122c5

Please sign in to comment.