Skip to content

Commit

Permalink
Create interfaces for RemoteWebDriver to use with Augmenter (#9856)
Browse files Browse the repository at this point in the history
* Implement Network Conditions for Chromium both local and remote drivers
* Allow Chromium casting functionality to be augmented by RemoteWebDriver
* Allow Chromium cdp functionality to be augmented by RemoteWebDriver
* Implement Safari permission endpoint and make accessible to RemoteWebDriver via Augmenter
* Implement Safari attach debugger endpoint and make accessible to RemoteWebDriver via Augmenter
* Allow Chromium permissions functionality to be augmented by RemoteWebDriver
* Allow Chromium launch app functionality to be augmented by RemoteWebDriver
  • Loading branch information
titusfortner authored Sep 28, 2021
1 parent 3b2e16c commit 12a14a2
Show file tree
Hide file tree
Showing 42 changed files with 1,632 additions and 188 deletions.
50 changes: 50 additions & 0 deletions java/src/org/openqa/selenium/chrome/AddHasCasting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// 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.chrome;

import com.google.auto.service.AutoService;
import com.google.common.collect.ImmutableMap;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.remote.AdditionalHttpCommands;
import org.openqa.selenium.remote.AugmenterProvider;
import org.openqa.selenium.remote.CommandInfo;
import org.openqa.selenium.remote.http.HttpMethod;

import java.util.Map;
import java.util.function.Predicate;

import static org.openqa.selenium.remote.BrowserType.CHROME;

@AutoService({AdditionalHttpCommands.class, AugmenterProvider.class})
public class AddHasCasting extends org.openqa.selenium.chromium.AddHasCasting {

@Override
public Map<String, CommandInfo> getAdditionalCommands() {
return ImmutableMap.of(
GET_CAST_SINKS, new CommandInfo("session/:sessionId/goog/cast/get_sinks", HttpMethod.GET),
SET_CAST_SINK_TO_USE, new CommandInfo("session/:sessionId/goog/cast/set_sink_to_use", HttpMethod.POST),
START_CAST_TAB_MIRRORING, new CommandInfo("session/:sessionId/goog/cast/start_tab_mirroring", HttpMethod.POST),
GET_CAST_ISSUE_MESSAGE, new CommandInfo("session/:sessionId/goog/cast/get_issue_message", HttpMethod.GET),
STOP_CASTING, new CommandInfo("session/:sessionId/goog/cast/stop_casting", HttpMethod.POST));
}

@Override
public Predicate<Capabilities> isApplicable() {
return caps -> CHROME.equals(caps.getBrowserName());
}
}
46 changes: 46 additions & 0 deletions java/src/org/openqa/selenium/chrome/AddHasCdp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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.chrome;

import com.google.auto.service.AutoService;
import com.google.common.collect.ImmutableMap;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.remote.AdditionalHttpCommands;
import org.openqa.selenium.remote.AugmenterProvider;
import org.openqa.selenium.remote.CommandInfo;
import org.openqa.selenium.remote.http.HttpMethod;

import java.util.Map;
import java.util.function.Predicate;

import static org.openqa.selenium.remote.BrowserType.CHROME;

@AutoService({AdditionalHttpCommands.class, AugmenterProvider.class})
public class AddHasCdp extends org.openqa.selenium.chromium.AddHasCdp {

@Override
public Map<String, CommandInfo> getAdditionalCommands() {
return ImmutableMap.of(
EXECUTE_CDP, new CommandInfo("session/:sessionId/goog/cdp/execute", HttpMethod.POST));
}

@Override
public Predicate<Capabilities> isApplicable() {
return caps -> CHROME.equals(caps.getBrowserName());
}
}
21 changes: 20 additions & 1 deletion java/src/org/openqa/selenium/chrome/ChromeDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@

package org.openqa.selenium.chrome;

import com.google.common.collect.ImmutableMap;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chromium.ChromiumDriver;
import org.openqa.selenium.chromium.ChromiumDriverCommandExecutor;
import org.openqa.selenium.remote.CommandInfo;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.service.DriverService;

import java.util.Map;

/**
* A {@link WebDriver} implementation that controls a Chrome browser running on the local machine.
Expand Down Expand Up @@ -96,7 +101,21 @@ public ChromeDriver(ChromeDriverService service, ChromeOptions options) {
*/
@Deprecated
public ChromeDriver(ChromeDriverService service, Capabilities capabilities) {
super(new ChromiumDriverCommandExecutor("goog", service), capabilities, ChromeOptions.CAPABILITY);
super(new ChromeDriverCommandExecutor(service), capabilities, ChromeOptions.CAPABILITY);
casting = new AddHasCasting().getImplementation(getCapabilities(), getExecuteMethod());
cdp = new AddHasCdp().getImplementation(getCapabilities(), getExecuteMethod());
}

private static class ChromeDriverCommandExecutor extends ChromiumDriverCommandExecutor {
public ChromeDriverCommandExecutor(DriverService service) {
super(service, getExtraCommands());
}

private static Map<String, CommandInfo> getExtraCommands() {
return ImmutableMap.<String, CommandInfo>builder()
.putAll(new AddHasCasting().getAdditionalCommands())
.putAll(new AddHasCdp().getAdditionalCommands())
.build();
}
}
}
81 changes: 81 additions & 0 deletions java/src/org/openqa/selenium/chromium/AddHasCasting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// 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.chromium;

import com.google.common.collect.ImmutableMap;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.remote.AdditionalHttpCommands;
import org.openqa.selenium.remote.AugmenterProvider;
import org.openqa.selenium.remote.CommandInfo;
import org.openqa.selenium.remote.ExecuteMethod;

import java.util.List;
import java.util.Map;
import java.util.function.Predicate;

public abstract class AddHasCasting implements AugmenterProvider<HasCasting>, AdditionalHttpCommands {

public static final String GET_CAST_SINKS = "getCastSinks";
public static final String SET_CAST_SINK_TO_USE = "selectCastSink";
public static final String START_CAST_TAB_MIRRORING = "startCastTabMirroring";
public static final String GET_CAST_ISSUE_MESSAGE = "getCastIssueMessage";
public static final String STOP_CASTING = "stopCasting";

@Override
public abstract Map<String, CommandInfo> getAdditionalCommands();

@Override
public abstract Predicate<Capabilities> isApplicable();

@Override
public Class<HasCasting> getDescribedInterface() {
return HasCasting.class;
}

@Override
public HasCasting getImplementation(Capabilities capabilities, ExecuteMethod executeMethod) {
return new HasCasting() {
@Override public List<Map<String, String>> getCastSinks() {
return (List<Map<String, String>>) executeMethod.execute(GET_CAST_SINKS, null);
}

@Override public void selectCastSink(String deviceName) {
Require.nonNull("Device Name", deviceName);

executeMethod.execute(SET_CAST_SINK_TO_USE, ImmutableMap.of("sinkName", deviceName));
}

@Override public void startTabMirroring(String deviceName) {
Require.nonNull("Device Name", deviceName);

executeMethod.execute(START_CAST_TAB_MIRRORING, ImmutableMap.of("sinkName", deviceName));
}

@Override public String getCastIssueMessage() {
return executeMethod.execute(GET_CAST_ISSUE_MESSAGE, null).toString();
}

@Override public void stopCasting(String deviceName) {
Require.nonNull("Device Name", deviceName);

executeMethod.execute(STOP_CASTING, ImmutableMap.of("sinkName", deviceName));
}
};
}
}
65 changes: 65 additions & 0 deletions java/src/org/openqa/selenium/chromium/AddHasCdp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// 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.chromium;

import com.google.common.collect.ImmutableMap;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.remote.AdditionalHttpCommands;
import org.openqa.selenium.remote.AugmenterProvider;
import org.openqa.selenium.remote.CommandInfo;
import org.openqa.selenium.remote.ExecuteMethod;

import java.util.Map;
import java.util.function.Predicate;

import static org.openqa.selenium.chromium.ChromiumDriver.KNOWN_CHROMIUM_BROWSERS;

public abstract class AddHasCdp implements AugmenterProvider<HasCdp>, AdditionalHttpCommands {

public static final String EXECUTE_CDP = "executeCdpCommand";

@Override
public abstract Map<String, CommandInfo> getAdditionalCommands();

@Override
public Predicate<Capabilities> isApplicable() {
return caps -> KNOWN_CHROMIUM_BROWSERS.contains(caps.getBrowserName());
}

@Override
public Class<HasCdp> getDescribedInterface() {
return HasCdp.class;
}

@Override
public HasCdp getImplementation(Capabilities capabilities, ExecuteMethod executeMethod) {
return new HasCdp() {
@Override
public Map<String, Object> executeCdpCommand(String commandName, Map<String, Object> parameters) {
Require.nonNull("Command name", commandName);
Require.nonNull("Parameters", parameters);

Map<String, Object> toReturn = (Map<String, Object>) executeMethod.execute(
EXECUTE_CDP, ImmutableMap.of("cmd", commandName, "params", parameters));

return ImmutableMap.copyOf(toReturn);
}
};
}
}
69 changes: 69 additions & 0 deletions java/src/org/openqa/selenium/chromium/AddHasLaunchApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// 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.chromium;

import com.google.auto.service.AutoService;
import com.google.common.collect.ImmutableMap;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.remote.AdditionalHttpCommands;
import org.openqa.selenium.remote.AugmenterProvider;
import org.openqa.selenium.remote.CommandInfo;
import org.openqa.selenium.remote.ExecuteMethod;
import org.openqa.selenium.remote.http.HttpMethod;

import java.util.Map;
import java.util.function.Predicate;

import static org.openqa.selenium.chromium.ChromiumDriver.KNOWN_CHROMIUM_BROWSERS;

@AutoService({AdditionalHttpCommands.class, AugmenterProvider.class})
public class AddHasLaunchApp implements AugmenterProvider<HasLaunchApp>, AdditionalHttpCommands {

public static final String LAUNCH_APP = "launchApp";

private static final Map<String, CommandInfo> COMMANDS = ImmutableMap.of(
LAUNCH_APP, new CommandInfo("/session/:sessionId/chromium/launch_app", HttpMethod.POST));

@Override
public Map<String, CommandInfo> getAdditionalCommands() {
return COMMANDS;
}

@Override
public Predicate<Capabilities> isApplicable() {
return caps -> KNOWN_CHROMIUM_BROWSERS.contains(caps.getBrowserName());
}

@Override
public Class<HasLaunchApp> getDescribedInterface() {
return HasLaunchApp.class;
}

@Override
public HasLaunchApp getImplementation(Capabilities capabilities, ExecuteMethod executeMethod) {
return new HasLaunchApp() {
@Override
public void launchApp(String id) {
Require.nonNull("id of Chromium App", id);

executeMethod.execute(LAUNCH_APP, ImmutableMap.of("id", id));
}
};
}
}
Loading

0 comments on commit 12a14a2

Please sign in to comment.