Skip to content

Commit

Permalink
Allow Chromium permissions functionality to be augmented by RemoteWeb…
Browse files Browse the repository at this point in the history
…Driver
  • Loading branch information
titusfortner committed Sep 22, 2021
1 parent 89d4403 commit 37a3b75
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 6 deletions.
71 changes: 71 additions & 0 deletions java/src/org/openqa/selenium/chromium/AddHasPermissions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// 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.ArrayList;
import java.util.Arrays;
import java.util.Map;
import java.util.function.Predicate;

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

@AutoService({AdditionalHttpCommands.class, AugmenterProvider.class})
public class AddHasPermissions implements AugmenterProvider<HasPermissions>, AdditionalHttpCommands {

public static final String SET_PERMISSION = "setPermission";

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

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

@Override public Predicate<Capabilities> isApplicable() {
String[] validBrowsers = new String[] { EDGE, CHROME, "msedge" };
return caps -> new ArrayList<>(Arrays.asList(validBrowsers)).contains(caps.getBrowserName());
}

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

@Override public HasPermissions getImplementation(Capabilities capabilities, ExecuteMethod executeMethod) {
return new HasPermissions() {
@Override
public void setPermission(String name, String value) {
Require.nonNull("Permission name", name);
Require.nonNull("Permission value", value);

executeMethod.execute(SET_PERMISSION, ImmutableMap.of("descriptor", ImmutableMap.of("name", name), "state", value));
}
};
}
}
8 changes: 5 additions & 3 deletions java/src/org/openqa/selenium/chromium/ChromiumDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ public class ChromiumDriver extends RemoteWebDriver implements
WebStorage,
HasNetworkConditions,
HasCasting,
HasCdp {
HasCdp,
HasPermissions {

private static final Logger LOG = Logger.getLogger(ChromiumDriver.class.getName());

Expand All @@ -86,6 +87,7 @@ public class ChromiumDriver extends RemoteWebDriver implements
private final TouchScreen touchScreen;
private final RemoteNetworkConnection networkConnection;
private final HasNetworkConditions networkConditions;
private final HasPermissions permissions;
protected HasCasting casting;
protected HasCdp cdp;
private final Optional<Connection> connection;
Expand All @@ -95,6 +97,7 @@ protected ChromiumDriver(CommandExecutor commandExecutor, Capabilities capabilit
super(commandExecutor, capabilities);
locationContext = new RemoteLocationContext(getExecuteMethod());
webStorage = new RemoteWebStorage(getExecuteMethod());
permissions = new AddHasPermissions().getImplementation(getCapabilities(), getExecuteMethod());
touchScreen = new RemoteTouchScreen(getExecuteMethod());
networkConnection = new RemoteNetworkConnection(getExecuteMethod());
networkConditions = new AddHasNetworkConditions().getImplementation(getCapabilities(), getExecuteMethod());
Expand Down Expand Up @@ -235,8 +238,7 @@ public void stopCasting(String deviceName) {
}

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

@Override public ChromiumNetworkConditions getNetworkConditions() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ private static Map<String, CommandInfo> getExtraCommands(Map<String, CommandInfo
return ImmutableMap.<String, CommandInfo>builder()
.putAll(commands)
.putAll(new AddHasNetworkConditions().getAdditionalCommands())
.putAll(new AddHasPermissions().getAdditionalCommands())
.put(ChromiumDriverCommand.LAUNCH_APP, new CommandInfo("/session/:sessionId/chromium/launch_app", HttpMethod.POST))
.put(ChromiumDriverCommand.SET_PERMISSION, new CommandInfo("/session/:sessionId/permissions", HttpMethod.POST))
.build();
}
}
33 changes: 33 additions & 0 deletions java/src/org/openqa/selenium/chromium/HasPermissions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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 org.openqa.selenium.Beta;

import java.util.Map;

@Beta
public interface HasPermissions {

/**
*
* @param name what permission to set.
* @param value what to set the permission to.
*/
public void setPermission(String name, String value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
package org.openqa.selenium.chrome;

import org.junit.Test;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.chromium.ChromiumDriver;
import org.openqa.selenium.chromium.ChromiumNetworkConditions;
import org.openqa.selenium.chromium.HasCasting;
import org.openqa.selenium.chromium.HasCdp;
import org.openqa.selenium.chromium.HasNetworkConditions;
import org.openqa.selenium.chromium.HasPermissions;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.testing.JUnit4TestBase;
Expand Down Expand Up @@ -61,6 +63,26 @@ public void canSetPermission() {
assertThat(checkPermission(driver, CLIPBOARD_WRITE)).isEqualTo("prompt");
}

@Test
public void shouldAllowRemoteWebDriverToAugmentHasPermissions() throws MalformedURLException {
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/"), new ChromeOptions());
WebDriver augmentedDriver = new Augmenter().augment(driver);

try {
driver.get(pages.clicksPage);
assertThat(checkPermission(driver, CLIPBOARD_READ)).isEqualTo("prompt");
assertThat(checkPermission(driver, CLIPBOARD_WRITE)).isEqualTo("granted");

((HasPermissions) augmentedDriver).setPermission(CLIPBOARD_READ, "denied");
((HasPermissions) augmentedDriver).setPermission(CLIPBOARD_WRITE, "prompt");

assertThat(checkPermission(driver, CLIPBOARD_READ)).isEqualTo("denied");
assertThat(checkPermission(driver, CLIPBOARD_WRITE)).isEqualTo("prompt");
} finally {
driver.quit();
}
}

@Test
public void canSetPermissionHeadless() {
ChromeOptions options = new ChromeOptions();
Expand All @@ -80,9 +102,9 @@ public void canSetPermissionHeadless() {
assertThat(checkPermission(driver, CLIPBOARD_WRITE)).isEqualTo("granted");
}

public String checkPermission(ChromiumDriver driver, String permission){
public String checkPermission(WebDriver driver, String permission){
@SuppressWarnings("unchecked")
Map<String, Object> result = (Map<String, Object>) driver.executeAsyncScript(
Map<String, Object> result = (Map<String, Object>) ((JavascriptExecutor) driver).executeAsyncScript(
"callback = arguments[arguments.length - 1];"
+ "callback(navigator.permissions.query({"
+ "name: arguments[0]"
Expand Down

0 comments on commit 37a3b75

Please sign in to comment.