Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/lib/selenium/webdriver/firefox/options.rb
1865 views
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 Firefox
23
class Options < WebDriver::Options
24
attr_accessor :debugger_address
25
26
KEY = 'moz:firefoxOptions'
27
28
# see: https://developer.mozilla.org/en-US/docs/Web/WebDriver/Capabilities/firefoxOptions
29
CAPABILITIES = {binary: 'binary',
30
args: 'args',
31
log: 'log',
32
prefs: 'prefs',
33
env: 'env',
34
android_package: 'androidPackage',
35
android_activity: 'androidActivity',
36
android_device_serial: 'androidDeviceSerial',
37
android_intent_arguments: 'androidIntentArguments'}.freeze
38
BROWSER = 'firefox'
39
40
# NOTE: special handling of 'profile' to validate when set instead of when used
41
attr_reader :profile
42
43
#
44
# Create a new Options instance, only for W3C-capable versions of Firefox.
45
#
46
# @example
47
# options = Selenium::WebDriver::Firefox::Options.new(args: ['--host=127.0.0.1'])
48
# driver = Selenium::WebDriver.for :firefox, options: options
49
#
50
# @param [Hash] opts the pre-defined options to create the Firefox::Options with
51
# @option opts [String] :binary Path to the Firefox executable to use
52
# @option opts [Array<String>] :args List of command-line arguments to use when starting geckodriver
53
# @option opts [Profile, String] :profile Encoded profile string or Profile instance
54
# @option opts [String, Symbol] :log_level Log level for geckodriver
55
# @option opts [Hash] :prefs A hash with each entry consisting of the key of the preference and its value
56
# @option opts [Hash] :options A hash for raw options
57
#
58
59
def initialize(log_level: nil, **opts)
60
@debugger_address = opts.delete(:debugger_address) { true }
61
opts[:accept_insecure_certs] = true unless opts.key?(:accept_insecure_certs)
62
63
super(**opts)
64
65
@options[:args] ||= []
66
@options[:prefs] ||= {}
67
# https://fxdx.dev/deprecating-cdp-support-in-firefox-embracing-the-future-with-webdriver-bidi/.
68
# Enable BiDi only
69
@options[:prefs]['remote.active-protocols'] = 1
70
@options[:env] ||= {}
71
@options[:log] ||= {level: log_level} if log_level
72
73
process_profile(@options.delete(:profile))
74
end
75
76
#
77
# Add a command-line argument to use when starting Firefox.
78
#
79
# @example Start geckodriver on a specific host
80
# options = Selenium::WebDriver::Firefox::Options.new
81
# options.add_argument('--host=127.0.0.1')
82
#
83
# @param [String] arg The command-line argument to add
84
#
85
86
def add_argument(arg)
87
@options[:args] << arg
88
end
89
90
#
91
# Add a preference that is only applied to the user profile in use.
92
#
93
# @example Set the default homepage
94
# options = Selenium::WebDriver::Firefox::Options.new
95
# options.add_preference('browser.startup.homepage', 'http://www.seleniumhq.com/')
96
#
97
# @param [String] name Key of the preference
98
# @param [Boolean, String, Integer] value Value of the preference
99
#
100
101
def add_preference(name, value)
102
@options[:prefs][name] = value
103
end
104
105
#
106
# Sets Firefox profile.
107
#
108
# @example Set the custom profile
109
# profile = Selenium::WebDriver::Firefox::Profile.new
110
# options = Selenium::WebDriver::Firefox::Options.new
111
# options.profile = profile
112
#
113
# @example Use existing profile
114
# options = Selenium::WebDriver::Firefox::Options.new
115
# options.profile = 'myprofile'
116
#
117
# @param [Profile, String] profile Profile to be used
118
#
119
120
def profile=(profile)
121
process_profile(profile)
122
end
123
124
def log_level
125
@options.dig(:log, :level)
126
end
127
128
def log_level=(level)
129
@options[:log] = {level: level}
130
end
131
132
#
133
# Enables mobile browser use on Android.
134
#
135
# @see https://developer.mozilla.org/en-US/docs/Web/WebDriver/Capabilities/firefoxOptions#android
136
#
137
# @param [String] package The package name of the Chrome or WebView app.
138
# @param [String] serial_number The serial number of the device on which to launch the application.
139
# If not specified and multiple devices are attached, an error will be returned.
140
# @param [String] activity The fully qualified class name of the activity to be launched.
141
# @param [Array] intent_arguments Arguments to launch the intent with.
142
#
143
144
def enable_android(package: 'org.mozilla.firefox', serial_number: nil, activity: nil, intent_arguments: nil)
145
@options[:android_package] = package
146
@options[:android_activity] = activity unless activity.nil?
147
@options[:android_device_serial] = serial_number unless serial_number.nil?
148
@options[:android_intent_arguments] = intent_arguments unless intent_arguments.nil?
149
end
150
151
private
152
153
def process_browser_options(browser_options)
154
browser_options['moz:debuggerAddress'] = true if @debugger_address
155
options = browser_options[KEY]
156
options['binary'] ||= Firefox.path if Firefox.path
157
options['profile'] = @profile if @profile
158
end
159
160
def process_profile(profile)
161
@profile = case profile
162
when nil
163
nil
164
when Profile
165
profile
166
else
167
Profile.from_name(profile)
168
end
169
end
170
171
def camelize?(key)
172
key != 'prefs'
173
end
174
end # Options
175
end # Firefox
176
end # WebDriver
177
end # Selenium
178
179