Skip to content

Commit

Permalink
Add ChromiumDriver.setPermission to Java bindings
Browse files Browse the repository at this point in the history
Add setPermission to ChromiumDriver to support the same command added
for version 78. This change updates the Java client bindings.

This will allow Selenium code to override default permissions that
would otherwise require user action.

Fixes #7539
  • Loading branch information
TriciaCrichton authored and shs96c committed Sep 10, 2019
1 parent f25d117 commit 2260d41
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ public void stopCasting(String deviceName) {
Object response = getExecuteMethod().execute(ChromiumDriverCommand.STOP_CASTING, ImmutableMap.of("sinkName", deviceName));
}

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

@Override
public void quit() {
connection.ifPresent(Connection::close);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,7 @@ private ChromiumDriverCommand() {}
static final String SET_CAST_SINK_TO_USE = "selectCastSink";
static final String START_CAST_TAB_MIRRORING = "startCastTabMirroring";
static final String GET_CAST_ISSUE_MESSAGE = "getCastIssueMessage";
static final String STOP_CASTING = "stopCasting";
static final String STOP_CASTING = "stopCasting";

static final String SET_PERMISSION = "setPermission";
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public class ChromiumDriverCommandExecutor extends DriverCommandExecutor {
new CommandInfo("/session/:sessionId/goog/cast/get_issue_message", HttpMethod.GET));
CHROME_COMMAND_NAME_TO_URL.put(ChromiumDriverCommand.STOP_CASTING,
new CommandInfo("/session/:sessionId/goog/cast/stop_casting", HttpMethod.POST));

CHROME_COMMAND_NAME_TO_URL.put(ChromiumDriverCommand.SET_PERMISSION,
new CommandInfo("/session/:sessionId/permissions", HttpMethod.POST));
}

public ChromiumDriverCommandExecutor(DriverService service) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// 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 org.junit.Test;
import org.openqa.selenium.testing.JUnit4TestBase;

import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

public class ChromeDriverFunctionalTest extends JUnit4TestBase {

private final String CLIPBOARD_READ = "clipboard-read";
private final String CLIPBOARD_WRITE = "clipboard-write";

private ChromeDriver driver = null;

@Test
public void canSetPermission() {
//Cast provided driver to enable ChromeSpecific calls
driver = (ChromeDriver) super.driver;

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

driver.setPermission(CLIPBOARD_READ, "denied");
driver.setPermission(CLIPBOARD_WRITE, "prompt");

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

@Test
public void canSetPermissionHeadless() {
ChromeOptions options = new ChromeOptions();
options.setHeadless(true);
//TestChromeDriver is not honoring headless request; using ChromeDriver instead
super.driver = new ChromeDriver(options);
driver = (ChromeDriver) super.driver;

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

driver.setPermission(CLIPBOARD_READ, "granted");
driver.setPermission(CLIPBOARD_WRITE, "granted");

assertThat(checkPermission(driver, CLIPBOARD_READ)).isEqualTo("granted");
assertThat(checkPermission(driver, CLIPBOARD_WRITE)).isEqualTo("granted");
}

public String checkPermission(ChromeDriver driver, String permission){
@SuppressWarnings("unchecked")
Map<String, Object> result = (Map<String, Object>) driver.executeAsyncScript(
"callback = arguments[arguments.length - 1];"
+ "callback(navigator.permissions.query({"
+ "name: arguments[0]"
+ "}));", permission);
return result.get("state").toString();
}
}

0 comments on commit 2260d41

Please sign in to comment.