Skip to content

Commit

Permalink
[rb] implement the Firefox context endpoints and functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed Sep 24, 2021
1 parent 10bcce1 commit d88bb08
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 1 deletion.
1 change: 1 addition & 0 deletions rb/lib/selenium/webdriver/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
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/has_context'
require 'selenium/webdriver/common/driver_extensions/prints_page'
require 'selenium/webdriver/common/driver_extensions/uploads_files'
require 'selenium/webdriver/common/driver_extensions/full_page_screenshot'
Expand Down
45 changes: 45 additions & 0 deletions rb/lib/selenium/webdriver/common/driver_extensions/has_context.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# 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 HasContext

#
# Sets the context that Selenium commands are running in using
# a `with` statement. The state of the context on the server is
# saved before entering the block, and restored upon exiting it.
#
# @param [String] name which permission to set
# @param [String] value what to set the permission to
#

def context=(value)
@bridge.context = value
end

def context
@bridge.context
end

end # HasContext
end # DriverExtensions
end # WebDriver
end # Selenium
1 change: 1 addition & 0 deletions rb/lib/selenium/webdriver/firefox/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module Firefox
class Driver < WebDriver::Driver
EXTENSIONS = [DriverExtensions::HasAddons,
DriverExtensions::FullPageScreenshot,
DriverExtensions::HasContext,
DriverExtensions::HasDevTools,
DriverExtensions::HasLogEvents,
DriverExtensions::HasNetworkInterception,
Expand Down
9 changes: 9 additions & 0 deletions rb/lib/selenium/webdriver/firefox/features.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ module Firefox
module Features

FIREFOX_COMMANDS = {
get_context: [:get, 'session/:session_id/moz/context'],
set_context: [:post, 'session/:session_id/moz/context'],
install_addon: [:post, 'session/:session_id/moz/addon/install'],
uninstall_addon: [:post, 'session/:session_id/moz/addon/uninstall'],
full_page_screenshot: [:get, 'session/:session_id/moz/screenshot/full']
Expand All @@ -46,6 +48,13 @@ def full_screenshot
execute :full_page_screenshot
end

def context=(context)
execute :set_context, {}, {context: context}
end

def context
execute :get_context
end
end # Bridge
end # Firefox
end # WebDriver
Expand Down
14 changes: 14 additions & 0 deletions rb/spec/integration/selenium/webdriver/firefox/driver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ module Firefox
ensure
File.delete(path) if File.exist?(path)
end

it 'can get and set context' do
options = Options.new(prefs: {'browser.download.dir': 'foo/bar'})
create_driver!(capabilities: options) do |driver|
expect(driver.context).to eq 'content'

driver.context = 'chrome'
expect(driver.context).to eq 'chrome'

# This call can not be made when context is set to 'content'
dir = driver.execute_script("return Services.prefs.getStringPref('browser.download.dir')")
expect(dir).to eq 'foo/bar'
end
end
end
end
end # Firefox
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def initialize

extract_browser_from_bazel_target_name

@driver = (ENV['WD_SPEC_DRIVER'] || :chrome).to_sym
@driver = (ENV['WD_SPEC_DRIVER'] || :firefox).to_sym
@driver_instance = nil
end

Expand Down

0 comments on commit d88bb08

Please sign in to comment.