Path: blob/trunk/rb/lib/selenium/webdriver/firefox/options.rb
1865 views
# frozen_string_literal: true12# Licensed to the Software Freedom Conservancy (SFC) under one3# or more contributor license agreements. See the NOTICE file4# distributed with this work for additional information5# regarding copyright ownership. The SFC licenses this file6# to you under the Apache License, Version 2.0 (the7# "License"); you may not use this file except in compliance8# with the License. You may obtain a copy of the License at9#10# http://www.apache.org/licenses/LICENSE-2.011#12# Unless required by applicable law or agreed to in writing,13# software distributed under the License is distributed on an14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15# KIND, either express or implied. See the License for the16# specific language governing permissions and limitations17# under the License.1819module Selenium20module WebDriver21module Firefox22class Options < WebDriver::Options23attr_accessor :debugger_address2425KEY = 'moz:firefoxOptions'2627# see: https://developer.mozilla.org/en-US/docs/Web/WebDriver/Capabilities/firefoxOptions28CAPABILITIES = {binary: 'binary',29args: 'args',30log: 'log',31prefs: 'prefs',32env: 'env',33android_package: 'androidPackage',34android_activity: 'androidActivity',35android_device_serial: 'androidDeviceSerial',36android_intent_arguments: 'androidIntentArguments'}.freeze37BROWSER = 'firefox'3839# NOTE: special handling of 'profile' to validate when set instead of when used40attr_reader :profile4142#43# Create a new Options instance, only for W3C-capable versions of Firefox.44#45# @example46# options = Selenium::WebDriver::Firefox::Options.new(args: ['--host=127.0.0.1'])47# driver = Selenium::WebDriver.for :firefox, options: options48#49# @param [Hash] opts the pre-defined options to create the Firefox::Options with50# @option opts [String] :binary Path to the Firefox executable to use51# @option opts [Array<String>] :args List of command-line arguments to use when starting geckodriver52# @option opts [Profile, String] :profile Encoded profile string or Profile instance53# @option opts [String, Symbol] :log_level Log level for geckodriver54# @option opts [Hash] :prefs A hash with each entry consisting of the key of the preference and its value55# @option opts [Hash] :options A hash for raw options56#5758def initialize(log_level: nil, **opts)59@debugger_address = opts.delete(:debugger_address) { true }60opts[:accept_insecure_certs] = true unless opts.key?(:accept_insecure_certs)6162super(**opts)6364@options[:args] ||= []65@options[:prefs] ||= {}66# https://fxdx.dev/deprecating-cdp-support-in-firefox-embracing-the-future-with-webdriver-bidi/.67# Enable BiDi only68@options[:prefs]['remote.active-protocols'] = 169@options[:env] ||= {}70@options[:log] ||= {level: log_level} if log_level7172process_profile(@options.delete(:profile))73end7475#76# Add a command-line argument to use when starting Firefox.77#78# @example Start geckodriver on a specific host79# options = Selenium::WebDriver::Firefox::Options.new80# options.add_argument('--host=127.0.0.1')81#82# @param [String] arg The command-line argument to add83#8485def add_argument(arg)86@options[:args] << arg87end8889#90# Add a preference that is only applied to the user profile in use.91#92# @example Set the default homepage93# options = Selenium::WebDriver::Firefox::Options.new94# options.add_preference('browser.startup.homepage', 'http://www.seleniumhq.com/')95#96# @param [String] name Key of the preference97# @param [Boolean, String, Integer] value Value of the preference98#99100def add_preference(name, value)101@options[:prefs][name] = value102end103104#105# Sets Firefox profile.106#107# @example Set the custom profile108# profile = Selenium::WebDriver::Firefox::Profile.new109# options = Selenium::WebDriver::Firefox::Options.new110# options.profile = profile111#112# @example Use existing profile113# options = Selenium::WebDriver::Firefox::Options.new114# options.profile = 'myprofile'115#116# @param [Profile, String] profile Profile to be used117#118119def profile=(profile)120process_profile(profile)121end122123def log_level124@options.dig(:log, :level)125end126127def log_level=(level)128@options[:log] = {level: level}129end130131#132# Enables mobile browser use on Android.133#134# @see https://developer.mozilla.org/en-US/docs/Web/WebDriver/Capabilities/firefoxOptions#android135#136# @param [String] package The package name of the Chrome or WebView app.137# @param [String] serial_number The serial number of the device on which to launch the application.138# If not specified and multiple devices are attached, an error will be returned.139# @param [String] activity The fully qualified class name of the activity to be launched.140# @param [Array] intent_arguments Arguments to launch the intent with.141#142143def enable_android(package: 'org.mozilla.firefox', serial_number: nil, activity: nil, intent_arguments: nil)144@options[:android_package] = package145@options[:android_activity] = activity unless activity.nil?146@options[:android_device_serial] = serial_number unless serial_number.nil?147@options[:android_intent_arguments] = intent_arguments unless intent_arguments.nil?148end149150private151152def process_browser_options(browser_options)153browser_options['moz:debuggerAddress'] = true if @debugger_address154options = browser_options[KEY]155options['binary'] ||= Firefox.path if Firefox.path156options['profile'] = @profile if @profile157end158159def process_profile(profile)160@profile = case profile161when nil162nil163when Profile164profile165else166Profile.from_name(profile)167end168end169170def camelize?(key)171key != 'prefs'172end173end # Options174end # Firefox175end # WebDriver176end # Selenium177178179