Skip to content

Commit

Permalink
[cdp] Look up CDP implementation dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Aug 17, 2020
1 parent afb76e1 commit b9250a9
Show file tree
Hide file tree
Showing 9 changed files with 355 additions and 5 deletions.
1 change: 0 additions & 1 deletion java/client/src/org/openqa/selenium/chromium/BUILD.bazel
Expand Up @@ -16,7 +16,6 @@ java_export(
deps = [
"//java:auto-service",
"//java/client/src/org/openqa/selenium/devtools",
"//java/client/src/org/openqa/selenium/devtools/v84",
"//java/client/src/org/openqa/selenium/json",
"//java/client/src/org/openqa/selenium/remote",
artifact("com.google.guava:guava"),
Expand Down
18 changes: 16 additions & 2 deletions java/client/src/org/openqa/selenium/chromium/ChromiumDriver.java
Expand Up @@ -21,10 +21,12 @@
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.devtools.CdpInfo;
import org.openqa.selenium.devtools.CdpVersionFinder;
import org.openqa.selenium.devtools.Connection;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.DevToolsException;
import org.openqa.selenium.devtools.HasDevTools;
import org.openqa.selenium.devtools.v84.V84Domains;
import org.openqa.selenium.html5.LocalStorage;
import org.openqa.selenium.html5.Location;
import org.openqa.selenium.html5.LocationContext;
Expand Down Expand Up @@ -83,7 +85,19 @@ protected ChromiumDriver(CommandExecutor commandExecutor, Capabilities capabilit
factory,
getCapabilities(),
capabilityKey);
devTools = connection.map(conn -> new DevTools(new V84Domains(), conn));

CdpInfo cdpInfo = new CdpVersionFinder().match(getCapabilities().getVersion())
.orElseThrow(() ->
new DevToolsException(String.format(
"Unable to find version of CDP to use for %s. You may need to " +
"include a dependency on a specific version of the CDP using " +
"something similar to " +
"`org.seleniumhq.selenium:selenium-devtools:86` where the " +
"version matches the version of the chromium-based browser " +
"you're using.",
capabilities.getVersion())));

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

@Override
Expand Down
52 changes: 52 additions & 0 deletions java/client/src/org/openqa/selenium/devtools/CdpInfo.java
@@ -0,0 +1,52 @@
// 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.devtools;

import org.openqa.selenium.devtools.idealized.Domains;
import org.openqa.selenium.internal.Require;

import java.util.function.Supplier;

public abstract class CdpInfo implements Comparable<CdpInfo> {

private final int majorVersion;
private final Supplier<Domains> domains;

public CdpInfo(int majorVersion, Supplier<Domains> domains) {
this.majorVersion = majorVersion;
this.domains = Require.nonNull("Domain supplier", domains);
}

public int getMajorVersion() {
return majorVersion;
}

public Domains getDomains() {
return domains.get();
}

@Override
public int compareTo(CdpInfo that) {
return Integer.compare(this.getMajorVersion(), that.getMajorVersion());
}

@Override
public String toString() {
return "CDP version: " + getMajorVersion();
}
}
139 changes: 139 additions & 0 deletions java/client/src/org/openqa/selenium/devtools/CdpVersionFinder.java
@@ -0,0 +1,139 @@
// 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.devtools;

import com.google.common.collect.ImmutableSet;
import org.openqa.selenium.internal.Require;

import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

public class CdpVersionFinder {
private final int fudgeFactor;
private final Set<CdpInfo> infos;
private static final Pattern MAJOR_VERSION_EXTRACTOR = Pattern.compile(".*/(\\d+)\\..*");
private static final Pattern BROWSER_NAME_VERSION = Pattern.compile("(\\d+)\\..*");

public CdpVersionFinder() {
this(
5,
StreamSupport.stream(ServiceLoader.load(CdpInfo.class).spliterator(), false).collect(Collectors.toSet()));
}

public CdpVersionFinder(int versionFudgeFactor, Collection<CdpInfo> infos) {
this.fudgeFactor = Require.nonNegative("Version fudge factor", versionFudgeFactor);

Require.nonNull("CDP versions", infos);

this.infos = ImmutableSet.copyOf(infos);
}

/**
* Take the output of `/json/version` from a CDP-enabled tool and uses
* that information to find a match.
*/
public Optional<CdpInfo> match(Map<String, Object> versionJson) {
/* The json may look like:
{
"Browser": "Chrome/85.0.4183.69",
"Protocol-Version": "1.3",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.69 Safari/537.36",
"V8-Version": "8.5.210.19",
"WebKit-Version": "537.36 (@4554ea1a1171bd8d06951a4b7d9336afe6c59967)",
"webSocketDebuggerUrl": "ws://localhost:9222/devtools/browser/c0ef43a1-7bb0-48e3-9cec-d6bb048cb720"
}
{
"Browser": "Edg/84.0.522.59",
"Protocol-Version": "1.3",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36 Edg/84.0.522.59",
"V8-Version": "8.4.371.23",
"WebKit-Version": "537.36 (@52ea6e40afcc988eef78d29d50f9077893fa1a12)",
"webSocketDebuggerUrl": "ws://localhost:9222/devtools/browser/c7922624-12e8-4301-8b08-fa446944c5cc"
}
*/
Require.nonNull("JSON", versionJson);

// We are assured by MS and Google that the `Browser` major version
// should match the version of chromium used, so let's grab that.

Object rawBrowser = versionJson.get("Browser");
if (!(rawBrowser instanceof String)) {
return Optional.empty();
}

Matcher matcher = MAJOR_VERSION_EXTRACTOR.matcher(rawBrowser.toString());
return fromMatcher(matcher);
}

/**
* Takes a `browserVersion` from a {@link org.openqa.selenium.Capabilities}
* instance and returns the matching CDP version.
*/
public Optional<CdpInfo> match(String browserVersion) {
Require.nonNull("Browser version", browserVersion);

Matcher matcher = BROWSER_NAME_VERSION.matcher(browserVersion);
return fromMatcher(matcher);
}

private Optional<CdpInfo> fromMatcher(Matcher matcher) {
if (matcher.matches()) {
String major = matcher.group(1);
try {
int version = Integer.parseInt(major);

return findNearestMatch(version);
} catch (NumberFormatException e) {
return Optional.empty();
}
}

return Optional.empty();
}

private Optional<CdpInfo> findNearestMatch(int version) {
CdpInfo nearestMatch = null;

for (CdpInfo info : infos) {
if (info.getMajorVersion() == version) {
return Optional.of(info);
}

// Never return a higher version
if (info.getMajorVersion() > version) {
continue;
}

if (version - info.getMajorVersion() < fudgeFactor) {
if (nearestMatch == null || info.getMajorVersion() > nearestMatch.getMajorVersion()) {
nearestMatch = info;
}
}
}

return Optional.ofNullable(nearestMatch);
}
}
4 changes: 2 additions & 2 deletions java/client/src/org/openqa/selenium/devtools/v84/BUILD.bazel
@@ -1,12 +1,11 @@
load("@rules_jvm_external//:defs.bzl", "artifact")
load("//:copy_file.bzl", "copy_file")
load("//java:defs.bzl", "java_export")
load("//java:version.bzl", "SE_VERSION")

java_export(
name = "v84",
srcs = glob(["*.java"]),
maven_coordinates = "org.seleniumhq.selenium:selenium-devtools-v84:%s" % SE_VERSION,
maven_coordinates = "org.seleniumhq.selenium:selenium-devtools:84",
pom_template = "//java/client/src/org/openqa/selenium:template-pom",
visibility = [
"//visibility:public",
Expand All @@ -16,6 +15,7 @@ java_export(
],
deps = [
":cdp",
"//java:auto-service",
"//java/client/src/org/openqa/selenium/devtools",
"//java/client/src/org/openqa/selenium/json",
"//java/client/src/org/openqa/selenium/remote/http",
Expand Down
32 changes: 32 additions & 0 deletions java/client/src/org/openqa/selenium/devtools/v84/V84CdpInfo.java
@@ -0,0 +1,32 @@
// 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.devtools.v84;

import com.google.auto.service.AutoService;
import org.openqa.selenium.devtools.CdpInfo;
import org.openqa.selenium.devtools.idealized.Domains;

import java.util.function.Supplier;

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

public V84CdpInfo() {
super(84, V84Domains::new);
}
}
1 change: 1 addition & 0 deletions java/client/test/org/openqa/selenium/devtools/BUILD.bazel
Expand Up @@ -14,6 +14,7 @@ java_selenium_test_suite(
":test-lib",
"//java/client/src/org/openqa/selenium/devtools",
"//java/client/src/org/openqa/selenium/devtools/v84",
"//java/client/src/org/openqa/selenium/json",
"//java/client/src/org/openqa/selenium/remote",
"//java/client/test/org/openqa/selenium/testing:annotations",
artifact("com.google.guava:guava"),
Expand Down

0 comments on commit b9250a9

Please sign in to comment.