From 39dec028673260e8a9518898fe40a910426adc85 Mon Sep 17 00:00:00 2001 From: titusfortner Date: Fri, 24 Sep 2021 12:28:43 -0500 Subject: [PATCH] [rb] implement endpoint for adding permissions in Chrome --- rb/lib/selenium/webdriver/chrome/driver.rb | 1 + rb/lib/selenium/webdriver/chrome/features.rb | 5 ++ rb/lib/selenium/webdriver/common.rb | 1 + .../has_apple_permissions.rb | 51 +++++++++++++++++++ .../driver_extensions/has_permissions.rb | 22 ++++---- rb/lib/selenium/webdriver/safari/driver.rb | 2 +- .../selenium/webdriver/chrome/driver_spec.rb | 30 +++++++++++ 7 files changed, 100 insertions(+), 12 deletions(-) create mode 100644 rb/lib/selenium/webdriver/common/driver_extensions/has_apple_permissions.rb diff --git a/rb/lib/selenium/webdriver/chrome/driver.rb b/rb/lib/selenium/webdriver/chrome/driver.rb index 341d639c843c5..e1239a3223c0e 100644 --- a/rb/lib/selenium/webdriver/chrome/driver.rb +++ b/rb/lib/selenium/webdriver/chrome/driver.rb @@ -34,6 +34,7 @@ class Driver < WebDriver::Driver DriverExtensions::HasWebStorage, DriverExtensions::HasLaunching, DriverExtensions::HasLocation, + DriverExtensions::HasPermissions, DriverExtensions::DownloadsFiles, DriverExtensions::HasDevTools, DriverExtensions::HasAuthentication, diff --git a/rb/lib/selenium/webdriver/chrome/features.rb b/rb/lib/selenium/webdriver/chrome/features.rb index 05161a42d8115..22dfddccf0b07 100644 --- a/rb/lib/selenium/webdriver/chrome/features.rb +++ b/rb/lib/selenium/webdriver/chrome/features.rb @@ -32,6 +32,7 @@ module Features get_network_conditions: [:get, 'session/:session_id/chromium/network_conditions'], set_network_conditions: [:post, 'session/:session_id/chromium/network_conditions'], delete_network_conditions: [:delete, 'session/:session_id/chromium/network_conditions'], + set_permission: [:post, 'session/:session_id/permissions'], send_command: [:post, 'session/:session_id/goog/cdp/execute'], get_available_log_types: [:get, 'session/:session_id/se/log/types'], get_log: [:post, 'session/:session_id/se/log'] @@ -65,6 +66,10 @@ def stop_casting(name) execute :stop_casting, {}, {sinkName: name} end + def set_permission(name, value) + execute :set_permission, {}, {descriptor: {name: name}, state: value} + end + def network_conditions execute :get_network_conditions end diff --git a/rb/lib/selenium/webdriver/common.rb b/rb/lib/selenium/webdriver/common.rb index 5cd421e86d7e0..780184cfc3ad3 100644 --- a/rb/lib/selenium/webdriver/common.rb +++ b/rb/lib/selenium/webdriver/common.rb @@ -58,6 +58,7 @@ require 'selenium/webdriver/common/driver_extensions/has_network_conditions' require 'selenium/webdriver/common/driver_extensions/has_network_connection' require 'selenium/webdriver/common/driver_extensions/has_network_interception' +require 'selenium/webdriver/common/driver_extensions/has_apple_permissions' require 'selenium/webdriver/common/driver_extensions/has_permissions' require 'selenium/webdriver/common/driver_extensions/has_debugger' require 'selenium/webdriver/common/driver_extensions/prints_page' diff --git a/rb/lib/selenium/webdriver/common/driver_extensions/has_apple_permissions.rb b/rb/lib/selenium/webdriver/common/driver_extensions/has_apple_permissions.rb new file mode 100644 index 0000000000000..771c86e92bd94 --- /dev/null +++ b/rb/lib/selenium/webdriver/common/driver_extensions/has_apple_permissions.rb @@ -0,0 +1,51 @@ +# frozen_string_literal: true + +# 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. + +module Selenium + module WebDriver + module DriverExtensions + module HasApplePermissions + + # + # Returns permissions. + # + # @return [Hash] + # + + def permissions + @bridge.permissions + end + + # + # Sets permissions. + # + # @example + # driver.permissions = {'getUserMedia' => true} + # + # @param [Hash] permissions + # + + def permissions=(permissions) + @bridge.permissions = permissions + end + + end # HasPermissions + end # DriverExtensions + end # WebDriver +end # Selenium diff --git a/rb/lib/selenium/webdriver/common/driver_extensions/has_permissions.rb b/rb/lib/selenium/webdriver/common/driver_extensions/has_permissions.rb index bbe27ab0e873a..f7ad603f400a3 100644 --- a/rb/lib/selenium/webdriver/common/driver_extensions/has_permissions.rb +++ b/rb/lib/selenium/webdriver/common/driver_extensions/has_permissions.rb @@ -23,26 +23,26 @@ module DriverExtensions module HasPermissions # - # Returns permissions. + # Set one permission. # - # @return [Hash] + # @param [String] name which permission to set + # @param [String] value what to set the permission to # - def permissions - @bridge.permissions + def add_permission(name, value) + @bridge.set_permission(name, value) end # - # Sets permissions. + # Set multiple permissions. # - # @example - # driver.permissions = {'getUserMedia' => true} - # - # @param [Hash] permissions + # @param [Hash] opt key/value pairs to set permissions # - def permissions=(permissions) - @bridge.permissions = permissions + def add_permissions(opt) + opt.each do |key, value| + @bridge.set_permission(key, value) + end end end # HasPermissions diff --git a/rb/lib/selenium/webdriver/safari/driver.rb b/rb/lib/selenium/webdriver/safari/driver.rb index 4d3e8fd95a442..f2c55c1cf6cd3 100644 --- a/rb/lib/selenium/webdriver/safari/driver.rb +++ b/rb/lib/selenium/webdriver/safari/driver.rb @@ -28,7 +28,7 @@ module Safari class Driver < WebDriver::Driver EXTENSIONS = [DriverExtensions::HasDebugger, - DriverExtensions::HasPermissions, + DriverExtensions::HasApplePermissions, DriverExtensions::HasWebStorage].freeze def browser diff --git a/rb/spec/integration/selenium/webdriver/chrome/driver_spec.rb b/rb/spec/integration/selenium/webdriver/chrome/driver_spec.rb index 3d351e3a16297..dfe92fac67a5b 100644 --- a/rb/spec/integration/selenium/webdriver/chrome/driver_spec.rb +++ b/rb/spec/integration/selenium/webdriver/chrome/driver_spec.rb @@ -151,6 +151,36 @@ module Chrome driver.stop_casting(device_name) end end + + def get_permission(name) + driver.execute_async_script("callback = arguments[arguments.length - 1];" \ + "callback(navigator.permissions.query({name: arguments[0]}));", name)['state'] + end + + it 'can set single permissions' do + driver.navigate.to url_for('xhtmlTest.html') + + expect(get_permission('clipboard-read')).to eq('prompt') + expect(get_permission('clipboard-write')).to eq('granted') + + driver.add_permission('clipboard-read', 'denied') + driver.add_permission('clipboard-write', 'prompt') + + expect(get_permission('clipboard-read')).to eq('denied') + expect(get_permission('clipboard-write')).to eq('prompt') + end + + it 'can set multiple permissions' do + driver.navigate.to url_for('xhtmlTest.html') + + expect(get_permission('clipboard-read')).to eq('prompt') + expect(get_permission('clipboard-write')).to eq('granted') + + driver.add_permissions('clipboard-read' => 'denied','clipboard-write' => 'prompt') + + expect(get_permission('clipboard-read')).to eq('denied') + expect(get_permission('clipboard-write')).to eq('prompt') + end end end # Chrome end # WebDriver