From d88bb08070a3ad7792e830f7f874cea1e669953b Mon Sep 17 00:00:00 2001 From: titusfortner Date: Fri, 24 Sep 2021 17:07:23 -0500 Subject: [PATCH] [rb] implement the Firefox context endpoints and functionality --- rb/lib/selenium/webdriver/common.rb | 1 + .../common/driver_extensions/has_context.rb | 45 +++++++++++++++++++ rb/lib/selenium/webdriver/firefox/driver.rb | 1 + rb/lib/selenium/webdriver/firefox/features.rb | 9 ++++ .../selenium/webdriver/firefox/driver_spec.rb | 14 ++++++ .../spec_support/test_environment.rb | 2 +- 6 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 rb/lib/selenium/webdriver/common/driver_extensions/has_context.rb diff --git a/rb/lib/selenium/webdriver/common.rb b/rb/lib/selenium/webdriver/common.rb index 780184cfc3ad3..352ce64711a3f 100644 --- a/rb/lib/selenium/webdriver/common.rb +++ b/rb/lib/selenium/webdriver/common.rb @@ -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' diff --git a/rb/lib/selenium/webdriver/common/driver_extensions/has_context.rb b/rb/lib/selenium/webdriver/common/driver_extensions/has_context.rb new file mode 100644 index 0000000000000..03f767302f149 --- /dev/null +++ b/rb/lib/selenium/webdriver/common/driver_extensions/has_context.rb @@ -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 diff --git a/rb/lib/selenium/webdriver/firefox/driver.rb b/rb/lib/selenium/webdriver/firefox/driver.rb index efe77371ad8b0..043ee4b7e8285 100644 --- a/rb/lib/selenium/webdriver/firefox/driver.rb +++ b/rb/lib/selenium/webdriver/firefox/driver.rb @@ -29,6 +29,7 @@ module Firefox class Driver < WebDriver::Driver EXTENSIONS = [DriverExtensions::HasAddons, DriverExtensions::FullPageScreenshot, + DriverExtensions::HasContext, DriverExtensions::HasDevTools, DriverExtensions::HasLogEvents, DriverExtensions::HasNetworkInterception, diff --git a/rb/lib/selenium/webdriver/firefox/features.rb b/rb/lib/selenium/webdriver/firefox/features.rb index 5022838c57382..a4c571340e2ec 100644 --- a/rb/lib/selenium/webdriver/firefox/features.rb +++ b/rb/lib/selenium/webdriver/firefox/features.rb @@ -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'] @@ -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 diff --git a/rb/spec/integration/selenium/webdriver/firefox/driver_spec.rb b/rb/spec/integration/selenium/webdriver/firefox/driver_spec.rb index 361bbf9ac9f32..97192d79c640f 100644 --- a/rb/spec/integration/selenium/webdriver/firefox/driver_spec.rb +++ b/rb/spec/integration/selenium/webdriver/firefox/driver_spec.rb @@ -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 diff --git a/rb/spec/integration/selenium/webdriver/spec_support/test_environment.rb b/rb/spec/integration/selenium/webdriver/spec_support/test_environment.rb index 84911d73c9ea0..0e49404cd1426 100644 --- a/rb/spec/integration/selenium/webdriver/spec_support/test_environment.rb +++ b/rb/spec/integration/selenium/webdriver/spec_support/test_environment.rb @@ -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