Skip to content

Commit

Permalink
[rb] implement endpoint for adding permissions in Chrome
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed Sep 24, 2021
1 parent 5eaa6e4 commit 39dec02
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 12 deletions.
1 change: 1 addition & 0 deletions rb/lib/selenium/webdriver/chrome/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Driver < WebDriver::Driver
DriverExtensions::HasWebStorage,
DriverExtensions::HasLaunching,
DriverExtensions::HasLocation,
DriverExtensions::HasPermissions,
DriverExtensions::DownloadsFiles,
DriverExtensions::HasDevTools,
DriverExtensions::HasAuthentication,
Expand Down
5 changes: 5 additions & 0 deletions rb/lib/selenium/webdriver/chrome/features.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions rb/lib/selenium/webdriver/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Symbol, Boolean>] permissions
#

def permissions=(permissions)
@bridge.permissions = permissions
end

end # HasPermissions
end # DriverExtensions
end # WebDriver
end # Selenium
Original file line number Diff line number Diff line change
Expand Up @@ -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<Symbol, Boolean>] 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
Expand Down
2 changes: 1 addition & 1 deletion rb/lib/selenium/webdriver/safari/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module Safari

class Driver < WebDriver::Driver
EXTENSIONS = [DriverExtensions::HasDebugger,
DriverExtensions::HasPermissions,
DriverExtensions::HasApplePermissions,
DriverExtensions::HasWebStorage].freeze

def browser
Expand Down
30 changes: 30 additions & 0 deletions rb/spec/integration/selenium/webdriver/chrome/driver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 39dec02

Please sign in to comment.