From 34ef9d186641391195d9d63027aea49328cd8bb0 Mon Sep 17 00:00:00 2001 From: Diego Molina Date: Tue, 7 Jun 2022 14:47:43 +0200 Subject: [PATCH 1/3] [java] Throwing warnings for non-W3C before creating session --- .../selenium/PersistentCapabilities.java | 2 - .../selenium/SharedCapabilitiesMethods.java | 2 - .../selenium/W3CCapabilityKeysValidator.java | 39 ------------------- .../selenium/remote/RemoteWebDriver.java | 15 +++++++ .../remote/RemoteWebDriverBuilder.java | 3 -- 5 files changed, 15 insertions(+), 46 deletions(-) delete mode 100644 java/src/org/openqa/selenium/W3CCapabilityKeysValidator.java diff --git a/java/src/org/openqa/selenium/PersistentCapabilities.java b/java/src/org/openqa/selenium/PersistentCapabilities.java index c5fddfafd13f4..864877adc43f6 100644 --- a/java/src/org/openqa/selenium/PersistentCapabilities.java +++ b/java/src/org/openqa/selenium/PersistentCapabilities.java @@ -53,8 +53,6 @@ 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)); } diff --git a/java/src/org/openqa/selenium/SharedCapabilitiesMethods.java b/java/src/org/openqa/selenium/SharedCapabilitiesMethods.java index 8beb62c5680f7..98635369a7d7c 100644 --- a/java/src/org/openqa/selenium/SharedCapabilitiesMethods.java +++ b/java/src/org/openqa/selenium/SharedCapabilitiesMethods.java @@ -46,8 +46,6 @@ static boolean equals(Capabilities left, Capabilities right) { } static void setCapability(Map caps, String key, Object value) { - W3CCapabilityKeysValidator.validateCapability(key); - if ("loggingPrefs".equals(key) && value instanceof Map) { LoggingPreferences prefs = new LoggingPreferences(); @SuppressWarnings("unchecked") Map prefsMap = (Map) value; diff --git a/java/src/org/openqa/selenium/W3CCapabilityKeysValidator.java b/java/src/org/openqa/selenium/W3CCapabilityKeysValidator.java deleted file mode 100644 index a29c346e23934..0000000000000 --- a/java/src/org/openqa/selenium/W3CCapabilityKeysValidator.java +++ /dev/null @@ -1,39 +0,0 @@ -// 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 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)); - } - - } -} diff --git a/java/src/org/openqa/selenium/remote/RemoteWebDriver.java b/java/src/org/openqa/selenium/remote/RemoteWebDriver.java index 9cb370486effc..e6f45d4b9fca4 100644 --- a/java/src/org/openqa/selenium/remote/RemoteWebDriver.java +++ b/java/src/org/openqa/selenium/remote/RemoteWebDriver.java @@ -20,6 +20,7 @@ import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; +import org.openqa.selenium.AcceptedW3CCapabilityKeys; import org.openqa.selenium.Alert; import org.openqa.selenium.Beta; import org.openqa.selenium.By; @@ -243,6 +244,20 @@ protected void setSessionId(String opaqueKey) { } protected void startSession(Capabilities capabilities) { + // Throwing warnings for non-W3C WebDriver compliant capabilities + List invalid = capabilities.asMap().keySet() + .stream() + .filter(key -> !(new AcceptedW3CCapabilityKeys().test(key))) + .collect(Collectors.toList()); + + if (!invalid.isEmpty()) { + logger.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)); + } + Response response = execute(DriverCommand.NEW_SESSION(singleton(capabilities))); if (response == null) { diff --git a/java/src/org/openqa/selenium/remote/RemoteWebDriverBuilder.java b/java/src/org/openqa/selenium/remote/RemoteWebDriverBuilder.java index 722a327c9a32d..009f0ca2cd8fa 100644 --- a/java/src/org/openqa/selenium/remote/RemoteWebDriverBuilder.java +++ b/java/src/org/openqa/selenium/remote/RemoteWebDriverBuilder.java @@ -26,7 +26,6 @@ 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; @@ -201,8 +200,6 @@ 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( From 43ef5d3587eaa6cb7628b72b76fb555d6b503021 Mon Sep 17 00:00:00 2001 From: Diego Molina Date: Wed, 8 Jun 2022 10:41:51 +0200 Subject: [PATCH 2/3] [java] Ignoring checks IE caps on top level --- .../selenium/remote/RemoteWebDriver.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/java/src/org/openqa/selenium/remote/RemoteWebDriver.java b/java/src/org/openqa/selenium/remote/RemoteWebDriver.java index e6f45d4b9fca4..0685caf9f509d 100644 --- a/java/src/org/openqa/selenium/remote/RemoteWebDriver.java +++ b/java/src/org/openqa/selenium/remote/RemoteWebDriver.java @@ -75,6 +75,7 @@ import java.net.MalformedURLException; import java.net.URL; import java.time.Duration; +import java.util.Arrays; import java.util.Base64; import java.util.Collection; import java.util.Collections; @@ -108,6 +109,26 @@ public class RemoteWebDriver implements WebDriver, PrintsPage, TakesScreenshot { + private static final List IE_CAPABILITY_NAMES = Arrays.asList( + "browserAttachTimeout", + "elementScrollBehavior", + "enablePersistentHover", + "ie.enableFullPageScreenshot", + "ie.forceCreateProcessApi", + "ie.forceShellWindowsApi", + "ie.ensureCleanSession", + "ie.browserCommandLineSwitches", + "ie.usePerProcessProxy", + "ignoreZoomSetting", + "initialBrowserUrl", + "ignoreProtectedModeSettings", + "requireWindowFocus", + "ie.fileUploadDialogTimeout", + "nativeEvents", + "ie.useLegacyFileUploadDialogHandling", + "ie.edgechromium", + "ie.edgepath"); + // TODO: This static logger should be unified with the per-instance localLogs private static final Logger logger = Logger.getLogger(RemoteWebDriver.class.getName()); private final ElementLocation elementLocation = new ElementLocation(); @@ -248,6 +269,7 @@ protected void startSession(Capabilities capabilities) { List invalid = capabilities.asMap().keySet() .stream() .filter(key -> !(new AcceptedW3CCapabilityKeys().test(key))) + .filter(key -> !IE_CAPABILITY_NAMES.contains(key)) .collect(Collectors.toList()); if (!invalid.isEmpty()) { From b1bad09d47e9b7c0cd476ea50f9b97c10d620b26 Mon Sep 17 00:00:00 2001 From: Diego Molina Date: Wed, 8 Jun 2022 11:04:22 +0200 Subject: [PATCH 3/3] [java] Fixing code smell --- .../org/openqa/selenium/remote/RemoteWebDriverBuilder.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/java/src/org/openqa/selenium/remote/RemoteWebDriverBuilder.java b/java/src/org/openqa/selenium/remote/RemoteWebDriverBuilder.java index 009f0ca2cd8fa..0b776ed1f7087 100644 --- a/java/src/org/openqa/selenium/remote/RemoteWebDriverBuilder.java +++ b/java/src/org/openqa/selenium/remote/RemoteWebDriverBuilder.java @@ -204,8 +204,8 @@ public RemoteWebDriverBuilder setCapability(String capabilityName, Object 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;