Skip to content

Commit d88bb08

Browse files
committed
[rb] implement the Firefox context endpoints and functionality
1 parent 10bcce1 commit d88bb08

File tree

6 files changed

+71
-1
lines changed

6 files changed

+71
-1
lines changed

rb/lib/selenium/webdriver/common.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
require 'selenium/webdriver/common/driver_extensions/has_apple_permissions'
6262
require 'selenium/webdriver/common/driver_extensions/has_permissions'
6363
require 'selenium/webdriver/common/driver_extensions/has_debugger'
64+
require 'selenium/webdriver/common/driver_extensions/has_context'
6465
require 'selenium/webdriver/common/driver_extensions/prints_page'
6566
require 'selenium/webdriver/common/driver_extensions/uploads_files'
6667
require 'selenium/webdriver/common/driver_extensions/full_page_screenshot'
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# frozen_string_literal: true
2+
3+
# Licensed to the Software Freedom Conservancy (SFC) under one
4+
# or more contributor license agreements. See the NOTICE file
5+
# distributed with this work for additional information
6+
# regarding copyright ownership. The SFC licenses this file
7+
# to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance
9+
# with the License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing,
14+
# software distributed under the License is distributed on an
15+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
# KIND, either express or implied. See the License for the
17+
# specific language governing permissions and limitations
18+
# under the License.
19+
20+
module Selenium
21+
module WebDriver
22+
module DriverExtensions
23+
module HasContext
24+
25+
#
26+
# Sets the context that Selenium commands are running in using
27+
# a `with` statement. The state of the context on the server is
28+
# saved before entering the block, and restored upon exiting it.
29+
#
30+
# @param [String] name which permission to set
31+
# @param [String] value what to set the permission to
32+
#
33+
34+
def context=(value)
35+
@bridge.context = value
36+
end
37+
38+
def context
39+
@bridge.context
40+
end
41+
42+
end # HasContext
43+
end # DriverExtensions
44+
end # WebDriver
45+
end # Selenium

rb/lib/selenium/webdriver/firefox/driver.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ module Firefox
2929
class Driver < WebDriver::Driver
3030
EXTENSIONS = [DriverExtensions::HasAddons,
3131
DriverExtensions::FullPageScreenshot,
32+
DriverExtensions::HasContext,
3233
DriverExtensions::HasDevTools,
3334
DriverExtensions::HasLogEvents,
3435
DriverExtensions::HasNetworkInterception,

rb/lib/selenium/webdriver/firefox/features.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ module Firefox
2323
module Features
2424

2525
FIREFOX_COMMANDS = {
26+
get_context: [:get, 'session/:session_id/moz/context'],
27+
set_context: [:post, 'session/:session_id/moz/context'],
2628
install_addon: [:post, 'session/:session_id/moz/addon/install'],
2729
uninstall_addon: [:post, 'session/:session_id/moz/addon/uninstall'],
2830
full_page_screenshot: [:get, 'session/:session_id/moz/screenshot/full']
@@ -46,6 +48,13 @@ def full_screenshot
4648
execute :full_page_screenshot
4749
end
4850

51+
def context=(context)
52+
execute :set_context, {}, {context: context}
53+
end
54+
55+
def context
56+
execute :get_context
57+
end
4958
end # Bridge
5059
end # Firefox
5160
end # WebDriver

rb/spec/integration/selenium/webdriver/firefox/driver_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@ module Firefox
4949
ensure
5050
File.delete(path) if File.exist?(path)
5151
end
52+
53+
it 'can get and set context' do
54+
options = Options.new(prefs: {'browser.download.dir': 'foo/bar'})
55+
create_driver!(capabilities: options) do |driver|
56+
expect(driver.context).to eq 'content'
57+
58+
driver.context = 'chrome'
59+
expect(driver.context).to eq 'chrome'
60+
61+
# This call can not be made when context is set to 'content'
62+
dir = driver.execute_script("return Services.prefs.getStringPref('browser.download.dir')")
63+
expect(dir).to eq 'foo/bar'
64+
end
65+
end
5266
end
5367
end
5468
end # Firefox

rb/spec/integration/selenium/webdriver/spec_support/test_environment.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def initialize
2929

3030
extract_browser_from_bazel_target_name
3131

32-
@driver = (ENV['WD_SPEC_DRIVER'] || :chrome).to_sym
32+
@driver = (ENV['WD_SPEC_DRIVER'] || :firefox).to_sym
3333
@driver_instance = nil
3434
end
3535

0 commit comments

Comments
 (0)