Skip to content

Commit

Permalink
[cdp] Handle the case when the first window closes
Browse files Browse the repository at this point in the history
This means that we need to make the remote webdriver aware of the CDP
protocol, and that makes me very sad indeed.
  • Loading branch information
shs96c committed Sep 27, 2021
1 parent fa37d18 commit ddfcd94
Show file tree
Hide file tree
Showing 26 changed files with 194 additions and 53 deletions.
1 change: 0 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ JAVA_RELEASE_TARGETS = %w[
//java/src/org/openqa/selenium/devtools/v85:v85.publish
//java/src/org/openqa/selenium/devtools/v93:v93.publish
//java/src/org/openqa/selenium/devtools/v94:v94.publish
//java/src/org/openqa/selenium/devtools:devtools.publish
//java/src/org/openqa/selenium/edge:edge.publish
//java/src/org/openqa/selenium/firefox/xpi:xpi.publish
//java/src/org/openqa/selenium/firefox:firefox.publish
Expand Down
1 change: 1 addition & 0 deletions java/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ v4.0.0-rc-2
* Add strongly-typed methods for setting timeouts and other w3c
capabilities on the base options class.
* Enable live view for Dynamic Grid
* Merged devtools maven artifact into the main remote driver

v4.0.0-rc-1
===========
Expand Down
2 changes: 0 additions & 2 deletions java/src/org/openqa/selenium/chrome/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ java_export(
"//java:auto-service",
"//java/src/org/openqa/selenium:core",
"//java/src/org/openqa/selenium/chromium",
"//java/src/org/openqa/selenium/devtools",
"//java/src/org/openqa/selenium/json",
"//java/src/org/openqa/selenium/remote",
"//java/src/org/openqa/selenium/remote/http",
artifact("com.google.guava:guava"),
],
)
3 changes: 1 addition & 2 deletions java/src/org/openqa/selenium/chromium/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ java_export(
"//visibility:public",
],
exports = [
"//java/src/org/openqa/selenium/devtools",
"//java/src/org/openqa/selenium/remote",
],
deps = [
"//java:auto-service",
"//java/src/org/openqa/selenium/devtools",
"//java/src/org/openqa/selenium/json",
"//java/src/org/openqa/selenium/remote",
artifact("com.google.guava:guava"),
Expand Down
4 changes: 2 additions & 2 deletions java/src/org/openqa/selenium/chromium/ChromiumDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ public Map<String, Object> executeCdpCommand(String commandName, Map<String, Obj
}

@Override
public DevTools getDevTools() {
return devTools.orElseThrow(() -> new WebDriverException("Unable to create DevTools connection"));
public Optional<DevTools> maybeGetDevTools() {
return devTools;
}

public String getCastSinks() {
Expand Down
34 changes: 23 additions & 11 deletions java/src/org/openqa/selenium/devtools/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
load("@rules_jvm_external//:defs.bzl", "artifact")
load("//common:defs.bzl", "copy_file")
load("//java:defs.bzl", "java_binary", "java_export", "java_library")
load("//java:version.bzl", "SE_VERSION")
load("//java:defs.bzl", "java_binary", "java_library")

GENERATOR_SOURCES = [
"CdpClientGenerator.java",
Expand Down Expand Up @@ -33,7 +32,25 @@ copy_file(
out = "mutation-listener.js",
)

java_export(
AUGMENTER_SRCS = [
"DevToolsProvider.java",
]

java_library(
name = "augmenter",
srcs = AUGMENTER_SRCS,
visibility = [
"//java/src/org/openqa/selenium/remote:__pkg__",
],
deps = [
":devtools",
"//java:auto-service",
"//java/src/org/openqa/selenium:core",
"//java/src/org/openqa/selenium/remote:api",
],
)

java_library(
name = "devtools",
srcs = glob(
[
Expand All @@ -42,18 +59,13 @@ java_export(
"idealized/**/*.java",
"noop/*.java",
],
exclude = PROTOTYPE_SOURCES + GENERATOR_SOURCES,
exclude = AUGMENTER_SRCS + GENERATOR_SOURCES + PROTOTYPE_SOURCES,
),
maven_coordinates = "org.seleniumhq.selenium:selenium-devtools:%s" % SE_VERSION,
opens_to = [
"org.openqa.selenium.json",
],
pom_template = "//java/src/org/openqa/selenium:template-pom",
resources = [
":mutation-listener",
],
visibility = [
"//visibility:public",
"//java/src/org/openqa/selenium/remote:__pkg__",
],
exports = [
":devtools-prototypes",
Expand All @@ -63,7 +75,7 @@ java_export(
"//java:auto-service",
"//java/src/org/openqa/selenium:core",
"//java/src/org/openqa/selenium/json",
"//java/src/org/openqa/selenium/remote",
"//java/src/org/openqa/selenium/remote/http",
artifact("com.google.guava:guava"),
],
)
Expand Down
4 changes: 4 additions & 0 deletions java/src/org/openqa/selenium/devtools/DevTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public Domains getDomains() {

@Override
public void close() {
disconnectSession();
}

public void disconnectSession() {
if (cdpSession != null) {
SessionID id = cdpSession;
cdpSession = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public HasDevTools getImplementation(Capabilities caps, ExecuteMethod executeMet
CdpInfo info = new CdpVersionFinder().match(version).orElseGet(NoOpCdpInfo::new);
Optional<DevTools> devTools = SeleniumCdpConnection.create(caps).map(conn -> new DevTools(info::getDomains, conn));

return () -> devTools.orElseThrow(() -> new IllegalStateException("Unable to create connection to " + caps));
return () -> devTools;
}

private String getCdpUrl(Capabilities caps) {
Expand Down
8 changes: 7 additions & 1 deletion java/src/org/openqa/selenium/devtools/HasDevTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@

package org.openqa.selenium.devtools;

import java.util.Optional;

public interface HasDevTools {

DevTools getDevTools();
default DevTools getDevTools() {
return maybeGetDevTools().orElseThrow(() -> new DevToolsException("Unable to create DevTools connection"));
}

Optional<DevTools> maybeGetDevTools();

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.openqa.selenium.devtools.idealized.target;

import org.openqa.selenium.devtools.Command;
import org.openqa.selenium.devtools.Event;
import org.openqa.selenium.devtools.idealized.target.model.SessionID;
import org.openqa.selenium.devtools.idealized.target.model.TargetID;
import org.openqa.selenium.devtools.idealized.target.model.TargetInfo;
Expand All @@ -37,4 +38,6 @@ public interface Target {
Command<SessionID> attachToTarget(TargetID targetId);

Command<Void> setAutoAttach();

Event<TargetID> detached();
}
5 changes: 2 additions & 3 deletions java/src/org/openqa/selenium/devtools/v85/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ java_export(
":cdp",
"//java:auto-service",
"//java/src/org/openqa/selenium:core",
"//java/src/org/openqa/selenium/devtools",
"//java/src/org/openqa/selenium/json",
"//java/src/org/openqa/selenium/remote/http",
"//java/src/org/openqa/selenium/remote",
artifact("com.google.guava:guava"),
],
)
Expand All @@ -38,8 +37,8 @@ java_library(
],
deps = [
"//java/src/org/openqa/selenium:core",
"//java/src/org/openqa/selenium/devtools",
"//java/src/org/openqa/selenium/json",
"//java/src/org/openqa/selenium/remote",
artifact("com.google.guava:guava"),
],
)
Expand Down
13 changes: 13 additions & 0 deletions java/src/org/openqa/selenium/devtools/v85/V85Target.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.common.collect.ImmutableMap;
import org.openqa.selenium.devtools.Command;
import org.openqa.selenium.devtools.ConverterFunctions;
import org.openqa.selenium.devtools.Event;
import org.openqa.selenium.devtools.idealized.browser.model.BrowserContextID;
import org.openqa.selenium.devtools.idealized.target.model.SessionID;
import org.openqa.selenium.devtools.idealized.target.model.TargetID;
Expand Down Expand Up @@ -86,4 +87,16 @@ public Command<SessionID> attachToTarget(TargetID targetId) {
public Command<Void> setAutoAttach() {
return Target.setAutoAttach(true, false, Optional.of(true));
}

@Override
public Event<TargetID> detached() {
return new Event<>(
"Target.detachedFromTarget",
input -> {
Function<JsonInput, org.openqa.selenium.devtools.v85.target.model.TargetID> converter =
ConverterFunctions.map("targetId", org.openqa.selenium.devtools.v85.target.model.TargetID.class);
return new TargetID(converter.apply(input).toString());
}
);
}
}
5 changes: 2 additions & 3 deletions java/src/org/openqa/selenium/devtools/v93/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ java_export(
":cdp",
"//java:auto-service",
"//java/src/org/openqa/selenium:core",
"//java/src/org/openqa/selenium/devtools",
"//java/src/org/openqa/selenium/json",
"//java/src/org/openqa/selenium/remote/http",
"//java/src/org/openqa/selenium/remote",
artifact("com.google.guava:guava"),
],
)
Expand All @@ -40,8 +39,8 @@ java_library(
],
deps = [
"//java/src/org/openqa/selenium:core",
"//java/src/org/openqa/selenium/devtools",
"//java/src/org/openqa/selenium/json",
"//java/src/org/openqa/selenium/remote",
artifact("com.google.guava:guava"),
],
)
Expand Down
14 changes: 14 additions & 0 deletions java/src/org/openqa/selenium/devtools/v93/V93Target.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.common.collect.ImmutableMap;
import org.openqa.selenium.devtools.Command;
import org.openqa.selenium.devtools.ConverterFunctions;
import org.openqa.selenium.devtools.Event;
import org.openqa.selenium.devtools.idealized.browser.model.BrowserContextID;
import org.openqa.selenium.devtools.idealized.target.model.SessionID;
import org.openqa.selenium.devtools.idealized.target.model.TargetID;
Expand Down Expand Up @@ -86,4 +87,17 @@ public Command<SessionID> attachToTarget(TargetID targetId) {
public Command<Void> setAutoAttach() {
return Target.setAutoAttach(true, false, Optional.of(true));
}

@Override
public Event<TargetID> detached() {
return new Event<>(
"Target.detachedFromTarget",
input -> {
Function<JsonInput, org.openqa.selenium.devtools.v93.target.model.TargetID> converter =
ConverterFunctions.map("targetId", org.openqa.selenium.devtools.v93.target.model.TargetID.class);
return new TargetID(converter.apply(input).toString());
}
);
}

}
5 changes: 2 additions & 3 deletions java/src/org/openqa/selenium/devtools/v94/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ java_export(
":cdp",
"//java:auto-service",
"//java/src/org/openqa/selenium:core",
"//java/src/org/openqa/selenium/devtools",
"//java/src/org/openqa/selenium/json",
"//java/src/org/openqa/selenium/remote/http",
"//java/src/org/openqa/selenium/remote",
artifact("com.google.guava:guava"),
],
)
Expand All @@ -40,8 +39,8 @@ java_library(
],
deps = [
"//java/src/org/openqa/selenium:core",
"//java/src/org/openqa/selenium/devtools",
"//java/src/org/openqa/selenium/json",
"//java/src/org/openqa/selenium/remote",
artifact("com.google.guava:guava"),
],
)
Expand Down
13 changes: 13 additions & 0 deletions java/src/org/openqa/selenium/devtools/v94/V94Target.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.common.collect.ImmutableMap;
import org.openqa.selenium.devtools.Command;
import org.openqa.selenium.devtools.ConverterFunctions;
import org.openqa.selenium.devtools.Event;
import org.openqa.selenium.devtools.idealized.browser.model.BrowserContextID;
import org.openqa.selenium.devtools.idealized.target.model.SessionID;
import org.openqa.selenium.devtools.idealized.target.model.TargetID;
Expand Down Expand Up @@ -86,4 +87,16 @@ public Command<SessionID> attachToTarget(TargetID targetId) {
public Command<Void> setAutoAttach() {
return Target.setAutoAttach(true, false, Optional.of(true));
}

@Override
public Event<TargetID> detached() {
return new Event<>(
"Target.detachedFromTarget",
input -> {
Function<JsonInput, org.openqa.selenium.devtools.v94.target.model.TargetID> converter =
ConverterFunctions.map("targetId", org.openqa.selenium.devtools.v94.target.model.TargetID.class);
return new TargetID(converter.apply(input).toString());
}
);
}
}
1 change: 0 additions & 1 deletion java/src/org/openqa/selenium/firefox/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ java_export(
visibility = ["//visibility:public"],
deps = [
"//java:auto-service",
"//java/src/org/openqa/selenium/devtools",
"//java/src/org/openqa/selenium/devtools/v85",
"//java/src/org/openqa/selenium/json",
"//java/src/org/openqa/selenium/remote",
Expand Down
37 changes: 26 additions & 11 deletions java/src/org/openqa/selenium/firefox/FirefoxDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -331,20 +331,35 @@ private static Capabilities dropCapabilities(Capabilities capabilities) {
}

@Override
public DevTools getDevTools() {
if (devTools == null) {
URI wsUri = cdpUri.orElseThrow(() ->
new DevToolsException("This version of Firefox or geckodriver does not support CDP"));
public Optional<DevTools> maybeGetDevTools() {
if (devTools != null) {
return Optional.of(devTools);
}

if (!cdpUri.isPresent()) {
return Optional.empty();
}

URI wsUri = cdpUri.orElseThrow(() ->
new DevToolsException("This version of Firefox or geckodriver does not support CDP"));
HttpClient.Factory clientFactory = HttpClient.Factory.createDefault();

HttpClient.Factory clientFactory = HttpClient.Factory.createDefault();
ClientConfig wsConfig = ClientConfig.defaultConfig().baseUri(wsUri);
HttpClient wsClient = clientFactory.createClient(wsConfig);

ClientConfig wsConfig = ClientConfig.defaultConfig().baseUri(wsUri);
HttpClient wsClient = clientFactory.createClient(wsConfig);
Connection connection = new Connection(wsClient, wsUri.toString());
CdpInfo cdpInfo = new CdpVersionFinder().match("85.0").orElseGet(NoOpCdpInfo::new);
devTools = new DevTools(cdpInfo::getDomains, connection);

Connection connection = new Connection(wsClient, wsUri.toString());
CdpInfo cdpInfo = new CdpVersionFinder().match("85.0").orElseGet(NoOpCdpInfo::new);
devTools = new DevTools(cdpInfo::getDomains, connection);
return Optional.of(devTools);
}

@Override
public DevTools getDevTools() {
if (!cdpUri.isPresent()) {
throw new DevToolsException("This version of Firefox or geckodriver does not support CDP");
}
return devTools;

return maybeGetDevTools().orElseThrow(() -> new DevToolsException("Unable to initialize CDP connection"));
}
}
1 change: 0 additions & 1 deletion java/src/org/openqa/selenium/grid/node/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ java_library(
],
deps = [
"//java/src/org/openqa/selenium:core",
"//java/src/org/openqa/selenium/devtools",
"//java/src/org/openqa/selenium/grid/component",
"//java/src/org/openqa/selenium/grid/data",
"//java/src/org/openqa/selenium/grid/jmx",
Expand Down
1 change: 0 additions & 1 deletion java/src/org/openqa/selenium/grid/node/config/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ java_library(
deps = [
"//java:auto-service",
"//java/src/org/openqa/selenium/chromium",
"//java/src/org/openqa/selenium/devtools",
"//java/src/org/openqa/selenium/grid/config",
"//java/src/org/openqa/selenium/grid/data",
"//java/src/org/openqa/selenium/grid/node",
Expand Down
Loading

0 comments on commit ddfcd94

Please sign in to comment.