Skip to content

Commit

Permalink
[java] Logging warnings when non-W3C caps are being used.
Browse files Browse the repository at this point in the history
Helps with #10374
  • Loading branch information
diemol committed May 25, 2022
1 parent 0d6a695 commit a297017
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.remote;
package org.openqa.selenium;

import java.util.function.Predicate;
import java.util.regex.Pattern;
Expand All @@ -36,9 +36,9 @@ public class AcceptedW3CCapabilityKeys implements Predicate<String> {
"^timeouts$",
"^unhandledPromptBehavior$",
"^webSocketUrl$") // from webdriver-bidi
.map(Pattern::compile)
.map(Pattern::asPredicate)
.reduce(identity -> false, Predicate::or);
.map(Pattern::compile)
.map(Pattern::asPredicate)
.reduce(identity -> false, Predicate::or);

@Override
public boolean test(String capabilityName) {
Expand Down
3 changes: 3 additions & 0 deletions java/src/org/openqa/selenium/MutableCapabilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
public class MutableCapabilities implements Capabilities {

private static final Set<String> OPTION_KEYS;

static {
HashSet<String> keys = new HashSet<>();
keys.add("chromeOptions");
Expand Down Expand Up @@ -87,6 +88,8 @@ public void setCapability(String capabilityName, Platform value) {
public void setCapability(String key, Object value) {
Require.nonNull("Capability name", key);

W3CCapabilityKeysValidator.validateCapability(key);

// We have to special-case some keys and values because of the popular idiom of calling
// something like "capabilities.setCapability(SafariOptions.CAPABILITY, new SafariOptions());"
// and this is no longer needed as options are capabilities. There will be a large amount of
Expand Down
3 changes: 3 additions & 0 deletions java/src/org/openqa/selenium/PersistentCapabilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.stream.Stream;

public class PersistentCapabilities implements Capabilities {

private final ImmutableCapabilities caps;
private final ImmutableCapabilities overrides;
private final int hashCode;
Expand All @@ -52,6 +53,8 @@ public PersistentCapabilities setCapability(String name, Object value) {
Require.nonNull("Name", name);
Require.nonNull("Value", value);

W3CCapabilityKeysValidator.validateCapability(name);

return new PersistentCapabilities(this, new ImmutableCapabilities(name, value));
}

Expand Down
2 changes: 2 additions & 0 deletions java/src/org/openqa/selenium/SharedCapabilitiesMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ static boolean equals(Capabilities left, Capabilities right) {
}

static void setCapability(Map<String, Object> caps, String key, Object value) {
W3CCapabilityKeysValidator.validateCapability(key);

if ("loggingPrefs".equals(key) && value instanceof Map) {
LoggingPreferences prefs = new LoggingPreferences();
@SuppressWarnings("unchecked") Map<String, String> prefsMap = (Map<String, String>) value;
Expand Down
39 changes: 39 additions & 0 deletions java/src/org/openqa/selenium/W3CCapabilityKeysValidator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium;

import java.util.function.Predicate;
import java.util.logging.Level;
import java.util.logging.Logger;

public class W3CCapabilityKeysValidator {

private static final Logger LOG = Logger.getLogger(W3CCapabilityKeysValidator.class.getName());
private static final Predicate<String> ACCEPTED_W3C_PATTERNS = new AcceptedW3CCapabilityKeys();

public static void validateCapability(String capabilityName) {
if (!ACCEPTED_W3C_PATTERNS.test(capabilityName)) {
LOG.log(Level.WARNING,
() -> String.format("Support for Legacy Capabilities is deprecated; " +
"You are sending \"%s\" which is an invalid capability. " +
"Please update to W3C Syntax: https://www.selenium.dev/blog/2022/legacy-protocol-support/",
capabilityName));
}

}
}
2 changes: 2 additions & 0 deletions java/src/org/openqa/selenium/remote/CapabilitiesUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;

import org.openqa.selenium.AcceptedW3CCapabilityKeys;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.Proxy;
Expand Down
32 changes: 17 additions & 15 deletions java/src/org/openqa/selenium/remote/NewSessionPayload.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import com.google.common.io.CharSource;
import com.google.common.io.CharStreams;
import com.google.common.io.FileBackedOutputStream;

import org.openqa.selenium.AcceptedW3CCapabilityKeys;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.internal.Require;
Expand Down Expand Up @@ -61,21 +63,6 @@ public class NewSessionPayload implements Closeable {
private final FileBackedOutputStream backingStore;
private final ImmutableSet<Dialect> dialects;

public static NewSessionPayload create(Capabilities caps) {
// We need to convert the capabilities into a new session payload. At this point we're dealing
// with references, so I'm Just Sure This Will Be Fine.
return create(ImmutableMap.of("desiredCapabilities", caps.asMap()));
}

public static NewSessionPayload create(Map<String, ?> source) {
String json = new Json().toJson(Require.nonNull("Payload", source));
return new NewSessionPayload(new StringReader(json));
}

public static NewSessionPayload create(Reader source) {
return new NewSessionPayload(source);
}

private NewSessionPayload(Reader source) {
// Dedicate up to 10% of all RAM or 20% of available RAM (whichever is smaller) to storing this
// payload.
Expand Down Expand Up @@ -116,6 +103,21 @@ private NewSessionPayload(Reader source) {

}

public static NewSessionPayload create(Capabilities caps) {
// We need to convert the capabilities into a new session payload. At this point we're dealing
// with references, so I'm Just Sure This Will Be Fine.
return create(ImmutableMap.of("desiredCapabilities", caps.asMap()));
}

public static NewSessionPayload create(Map<String, ?> source) {
String json = new Json().toJson(Require.nonNull("Payload", source));
return new NewSessionPayload(new StringReader(json));
}

public static NewSessionPayload create(Reader source) {
return new NewSessionPayload(source);
}

private void validate() throws IOException {
Map<String, Object> alwaysMatch = getAlwaysMatch();
if (alwaysMatch == null) {
Expand Down
1 change: 1 addition & 0 deletions java/src/org/openqa/selenium/remote/ProtocolHandshake.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.common.io.CountingOutputStream;
import com.google.common.io.FileBackedOutputStream;

import org.openqa.selenium.AcceptedW3CCapabilityKeys;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.Proxy;
Expand Down
11 changes: 10 additions & 1 deletion java/src/org/openqa/selenium/remote/RemoteWebDriverBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableSet;

import org.openqa.selenium.AcceptedW3CCapabilityKeys;
import org.openqa.selenium.Beta;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.Credentials;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.SessionNotCreatedException;
import org.openqa.selenium.UsernameAndPassword;
import org.openqa.selenium.W3CCapabilityKeysValidator;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverInfo;
import org.openqa.selenium.internal.Either;
Expand Down Expand Up @@ -53,6 +56,7 @@
import java.util.Set;
import java.util.TreeMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.logging.Logger;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -89,7 +93,9 @@
*/
@Beta
public class RemoteWebDriverBuilder {

private static final Logger LOG = Logger.getLogger(RemoteWebDriverBuilder.class.getName());
private static final Predicate<String> ACCEPTED_W3C_PATTERNS = new AcceptedW3CCapabilityKeys();
private static final Set<String> ILLEGAL_METADATA_KEYS = ImmutableSet.of(
"alwaysMatch",
"capabilities",
Expand Down Expand Up @@ -198,11 +204,14 @@ public RemoteWebDriverBuilder setCapability(String capabilityName, Object value)
Require.nonNull("Capability name", capabilityName);
Require.nonNull("Capability value", value);

W3CCapabilityKeysValidator.validateCapability(capabilityName);

Object previous = additionalCapabilities.put(capabilityName, value);
if (previous != null) {
LOG.log(
getDebugLogLevel(),
String.format("Overwriting capability %s. Previous value %s, new value %s", capabilityName, previous, value));
String.format("Overwriting capability %s. Previous value %s, new value %s", capabilityName,
previous, value));
}

return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@

import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.openqa.selenium.AcceptedW3CCapabilityKeys;
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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.remote.AcceptedW3CCapabilityKeys;
import org.openqa.selenium.AcceptedW3CCapabilityKeys;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.testing.UnitTests;

Expand Down

0 comments on commit a297017

Please sign in to comment.