Skip to content

Commit

Permalink
[cdp] Allow the chromium driver to register for event callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Aug 18, 2020
1 parent 24cf824 commit 4d7df92
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 7 deletions.
23 changes: 16 additions & 7 deletions java/client/src/org/openqa/selenium/chromium/ChromiumDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
package org.openqa.selenium.chromium;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
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.noop.NoOpCdpInfo;
import org.openqa.selenium.html5.LocalStorage;
Expand All @@ -36,6 +36,8 @@
import org.openqa.selenium.interactions.HasTouchScreen;
import org.openqa.selenium.interactions.TouchScreen;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.logging.EventType;
import org.openqa.selenium.logging.HasLogEvents;
import org.openqa.selenium.mobile.NetworkConnection;
import org.openqa.selenium.remote.CommandExecutor;
import org.openqa.selenium.remote.FileDetector;
Expand All @@ -48,6 +50,7 @@

import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.logging.Logger;

/**
Expand All @@ -66,7 +69,7 @@
* to the appropriate interface.
*/
public class ChromiumDriver extends RemoteWebDriver
implements HasDevTools, HasTouchScreen, LocationContext, NetworkConnection, WebStorage {
implements HasDevTools, HasLogEvents, HasTouchScreen, LocationContext, NetworkConnection, WebStorage {

private static final Logger LOG = Logger.getLogger(ChromiumDriver.class.getName());
private final RemoteLocationContext locationContext;
Expand Down Expand Up @@ -113,6 +116,12 @@ public void setFileDetector(FileDetector detector) {
"via RemoteWebDriver");
}

@Override
public <X> void onLogEvent(EventType<X> kind) {
Require.nonNull("Event type", kind);
kind.initializeLogger(this);
}

@Override
public LocalStorage getLocalStorage() {
return webStorage.getLocalStorage();
Expand Down Expand Up @@ -181,7 +190,7 @@ public DevTools getDevTools() {
}

public String getCastSinks() {
Object response = getExecuteMethod().execute(ChromiumDriverCommand.GET_CAST_SINKS, null);
Object response = getExecuteMethod().execute(ChromiumDriverCommand.GET_CAST_SINKS, null);
return response.toString();
}

Expand All @@ -191,19 +200,19 @@ public String getCastIssueMessage() {
}

public void selectCastSink(String deviceName) {
Object response = getExecuteMethod().execute(ChromiumDriverCommand.SET_CAST_SINK_TO_USE, ImmutableMap.of("sinkName", deviceName));
getExecuteMethod().execute(ChromiumDriverCommand.SET_CAST_SINK_TO_USE, ImmutableMap.of("sinkName", deviceName));
}

public void startTabMirroring(String deviceName) {
Object response = getExecuteMethod().execute(ChromiumDriverCommand.START_CAST_TAB_MIRRORING, ImmutableMap.of("sinkName", deviceName));
getExecuteMethod().execute(ChromiumDriverCommand.START_CAST_TAB_MIRRORING, ImmutableMap.of("sinkName", deviceName));
}

public void stopCasting(String deviceName) {
Object response = getExecuteMethod().execute(ChromiumDriverCommand.STOP_CASTING, ImmutableMap.of("sinkName", deviceName));
getExecuteMethod().execute(ChromiumDriverCommand.STOP_CASTING, ImmutableMap.of("sinkName", deviceName));
}

public void setPermission(String name, String value) {
Object response = getExecuteMethod().execute(ChromiumDriverCommand.SET_PERMISSION,
getExecuteMethod().execute(ChromiumDriverCommand.SET_PERMISSION,
ImmutableMap.of("descriptor", ImmutableMap.of("name", name), "state", value));
}

Expand Down
27 changes: 27 additions & 0 deletions java/client/src/org/openqa/selenium/logging/EventType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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.logging;

@FunctionalInterface
public interface EventType<X> {
void consume(X event);

default void initializeLogger(HasLogEvents loggable) {
// no-op
}
}
28 changes: 28 additions & 0 deletions java/client/src/org/openqa/selenium/logging/HasLogEvents.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 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.logging;

import java.util.Set;
import java.util.function.Consumer;

public interface HasLogEvents {

<X> void onLogEvent(EventType<X> kind);

}

0 comments on commit 4d7df92

Please sign in to comment.