Skip to content

Commit

Permalink
Merge a14aae3 into 7e541ed
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner authored May 24, 2022
2 parents 7e541ed + a14aae3 commit b33b53c
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 28 deletions.
47 changes: 47 additions & 0 deletions java/src/org/openqa/selenium/AcceptedW3CCapabilityKeys.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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.regex.Pattern;
import java.util.stream.Stream;

public class AcceptedW3CCapabilityKeys implements Predicate<String> {

protected static final Predicate<String> ACCEPTED_W3C_PATTERNS = Stream.of(
"^[\\w-]+:.*$",
"^acceptInsecureCerts$",
"^browserName$",
"^browserVersion$",
"^platformName$",
"^pageLoadStrategy$",
"^proxy$",
"^setWindowRect$",
"^strictFileInteractability$",
"^timeouts$",
"^unhandledPromptBehavior$",
"^webSocketUrl$") // from webdriver-bidi
.map(Pattern::compile)
.map(Pattern::asPredicate)
.reduce(identity -> false, Predicate::or);

@Override
public boolean test(String capabilityName) {
return ACCEPTED_W3C_PATTERNS.test(capabilityName);
}
}
12 changes: 12 additions & 0 deletions java/src/org/openqa/selenium/MutableCapabilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import org.openqa.selenium.internal.Require;

import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
Expand All @@ -27,6 +29,7 @@

public class MutableCapabilities implements Capabilities {

private static final Logger LOG = Logger.getLogger(MutableCapabilities.class.getName());
private static final Set<String> OPTION_KEYS;
static {
HashSet<String> keys = new HashSet<>();
Expand Down Expand Up @@ -101,6 +104,15 @@ public void setCapability(String key, Object value) {
return;
}

boolean w3cCompliant = new AcceptedW3CCapabilityKeys().test(key);
if (!w3cCompliant) {
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/",
key));
}

SharedCapabilitiesMethods.setCapability(caps, key, value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,5 @@

package org.openqa.selenium.remote;

import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.stream.Stream;

public class AcceptedW3CCapabilityKeys implements Predicate<String> {

private static final Predicate<String> ACCEPTED_W3C_PATTERNS = Stream.of(
"^[\\w-]+:.*$",
"^acceptInsecureCerts$",
"^browserName$",
"^browserVersion$",
"^platformName$",
"^pageLoadStrategy$",
"^proxy$",
"^setWindowRect$",
"^strictFileInteractability$",
"^timeouts$",
"^unhandledPromptBehavior$",
"^webSocketUrl$") // from webdriver-bidi
.map(Pattern::compile)
.map(Pattern::asPredicate)
.reduce(identity -> false, Predicate::or);

@Override
public boolean test(String capabilityName) {
return ACCEPTED_W3C_PATTERNS.test(capabilityName);
}
public class AcceptedW3CCapabilityKeys extends org.openqa.selenium.AcceptedW3CCapabilityKeys {
}
19 changes: 18 additions & 1 deletion java/src/org/openqa/selenium/remote/ProtocolHandshake.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,18 @@
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.google.common.net.HttpHeaders.CONTENT_LENGTH;
import static com.google.common.net.HttpHeaders.CONTENT_TYPE;
import static com.google.common.net.HttpHeaders.WARNING;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.openqa.selenium.json.Json.JSON_UTF_8;
import static org.openqa.selenium.remote.CapabilityType.PROXY;
Expand All @@ -63,7 +67,20 @@ public Result createSession(HttpHandler client, Command command) throws IOExcept

if (result.isRight()) {
Result toReturn = result.right();
LOG.info(String.format("Detected dialect: %s", toReturn.dialect));
LOG.info(String.format("Detected upstream dialect: %s", toReturn.dialect));

List<String> invalid = desired.asMap().keySet()
.stream()
.filter(key -> !(new AcceptedW3CCapabilityKeys().test(key)))
.collect(Collectors.toList());

if (!invalid.isEmpty()) {
LOG.log(Level.WARNING,
() -> String.format("Support for Legacy Capabilities is deprecated; " +
"You are sending the following invalid capabilities: %s; " +
"Please update to W3C Syntax: https://www.selenium.dev/blog/2022/legacy-protocol-support/",
invalid));
}
return toReturn;
} else {
throw result.left();
Expand Down

0 comments on commit b33b53c

Please sign in to comment.