Skip to content

Commit 1e9808f

Browse files
committed
[cdp] Add a fallback no-op version of CDP
1 parent b9250a9 commit 1e9808f

7 files changed

Lines changed: 113 additions & 12 deletions

File tree

java/client/src/org/openqa/selenium/chromium/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ java_export(
1616
deps = [
1717
"//java:auto-service",
1818
"//java/client/src/org/openqa/selenium/devtools",
19+
"//java/client/src/org/openqa/selenium/devtools/noop",
1920
"//java/client/src/org/openqa/selenium/json",
2021
"//java/client/src/org/openqa/selenium/remote",
2122
artifact("com.google.guava:guava"),

java/client/src/org/openqa/selenium/chromium/ChromiumDriver.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.openqa.selenium.devtools.DevTools;
2828
import org.openqa.selenium.devtools.DevToolsException;
2929
import org.openqa.selenium.devtools.HasDevTools;
30+
import org.openqa.selenium.devtools.noop.NoOpCdpInfo;
3031
import org.openqa.selenium.html5.LocalStorage;
3132
import org.openqa.selenium.html5.Location;
3233
import org.openqa.selenium.html5.LocationContext;
@@ -47,6 +48,7 @@
4748

4849
import java.util.Map;
4950
import java.util.Optional;
51+
import java.util.logging.Logger;
5052

5153
/**
5254
* A {@link WebDriver} implementation that controls a Chromium browser running on the local machine.
@@ -66,6 +68,7 @@
6668
public class ChromiumDriver extends RemoteWebDriver
6769
implements HasDevTools, HasTouchScreen, LocationContext, NetworkConnection, WebStorage {
6870

71+
private static final Logger LOG = Logger.getLogger(ChromiumDriver.class.getName());
6972
private final RemoteLocationContext locationContext;
7073
private final RemoteWebStorage webStorage;
7174
private final TouchScreen touchScreen;
@@ -87,15 +90,18 @@ protected ChromiumDriver(CommandExecutor commandExecutor, Capabilities capabilit
8790
capabilityKey);
8891

8992
CdpInfo cdpInfo = new CdpVersionFinder().match(getCapabilities().getVersion())
90-
.orElseThrow(() ->
91-
new DevToolsException(String.format(
92-
"Unable to find version of CDP to use for %s. You may need to " +
93-
"include a dependency on a specific version of the CDP using " +
94-
"something similar to " +
95-
"`org.seleniumhq.selenium:selenium-devtools:86` where the " +
96-
"version matches the version of the chromium-based browser " +
97-
"you're using.",
98-
capabilities.getVersion())));
93+
.orElseGet(() -> {
94+
LOG.warning(
95+
String.format(
96+
"Unable to find version of CDP to use for %s. You may need to " +
97+
"include a dependency on a specific version of the CDP using " +
98+
"something similar to " +
99+
"`org.seleniumhq.selenium:selenium-devtools:86` where the " +
100+
"version matches the version of the chromium-based browser " +
101+
"you're using.",
102+
capabilities.getVersion()));
103+
return new NoOpCdpInfo();
104+
});
99105

100106
devTools = connection.map(conn -> new DevTools(cdpInfo.getDomains(), conn));
101107
}

java/client/src/org/openqa/selenium/devtools/CdpVersionFinder.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@
2525
import java.util.Optional;
2626
import java.util.ServiceLoader;
2727
import java.util.Set;
28+
import java.util.logging.Logger;
2829
import java.util.regex.Matcher;
2930
import java.util.regex.Pattern;
3031
import java.util.stream.Collectors;
3132
import java.util.stream.StreamSupport;
3233

3334
public class CdpVersionFinder {
35+
private static final Logger LOG = Logger.getLogger(CdpVersionFinder.class.getName());
3436
private final int fudgeFactor;
3537
private final Set<CdpInfo> infos;
3638
private static final Pattern MAJOR_VERSION_EXTRACTOR = Pattern.compile(".*/(\\d+)\\..*");
@@ -134,6 +136,11 @@ private Optional<CdpInfo> findNearestMatch(int version) {
134136
}
135137
}
136138

139+
LOG.warning(String.format(
140+
"Unable to find an exact match for CDP version %d, so returning the closest version found: %s",
141+
version,
142+
nearestMatch == null ? "a no-op implementation" : nearestMatch.getMajorVersion()));
143+
137144
return Optional.ofNullable(nearestMatch);
138145
}
139146
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
java_library(
3+
name = "noop",
4+
srcs = glob(["*.java"]),
5+
deps = [
6+
"//java/client/src/org/openqa/selenium/devtools",
7+
"//java/client/src/org/openqa/selenium/json",
8+
],
9+
visibility = [
10+
"//java/client/src/org/openqa/selenium/chromium:__pkg__",
11+
]
12+
)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.openqa.selenium.devtools.noop;
19+
20+
import org.openqa.selenium.devtools.CdpInfo;
21+
22+
public class NoOpCdpInfo extends CdpInfo {
23+
24+
public NoOpCdpInfo() {
25+
super(1, NoOpDomains::new);
26+
}
27+
28+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.openqa.selenium.devtools.noop;
19+
20+
import org.openqa.selenium.devtools.DevToolsException;
21+
import org.openqa.selenium.devtools.idealized.Domains;
22+
import org.openqa.selenium.devtools.idealized.fetch.Fetch;
23+
import org.openqa.selenium.devtools.idealized.log.Log;
24+
import org.openqa.selenium.devtools.idealized.target.Target;
25+
26+
public class NoOpDomains implements Domains {
27+
28+
private final static String WARNING =
29+
"You are using a no-op implementation of the CDP. The most likely reason" +
30+
" for this is that Selenium was unable to find an implementation of the " +
31+
"CDP protocol that matches your browser. Please be sure to include an " +
32+
"implementation on the classpath, possibly by adding a new (maven) " +
33+
"dependency of `org.seleniumhq.selenium:selenium-devtools:NN` where " +
34+
"`NN` matches the major version of the browser you're using.";
35+
36+
@Override
37+
public Fetch fetch() {
38+
throw new DevToolsException(WARNING);
39+
}
40+
41+
@Override
42+
public Log log() {
43+
throw new DevToolsException(WARNING);
44+
}
45+
46+
@Override
47+
public Target target() {
48+
throw new DevToolsException(WARNING);
49+
}
50+
}

java/client/src/org/openqa/selenium/devtools/v84/V84CdpInfo.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919

2020
import com.google.auto.service.AutoService;
2121
import org.openqa.selenium.devtools.CdpInfo;
22-
import org.openqa.selenium.devtools.idealized.Domains;
23-
24-
import java.util.function.Supplier;
2522

2623
@AutoService(CdpInfo.class)
2724
public class V84CdpInfo extends CdpInfo {

0 commit comments

Comments
 (0)