1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
# 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 Firefox
class Profile
include ProfileHelper
VALID_PREFERENCE_TYPES = [TrueClass, FalseClass, Integer, Float, String].freeze
WEBDRIVER_PREFS = {
port: 'webdriver_firefox_port',
log_file: 'webdriver.log.file'
}.freeze
DEFAULT_PREFERENCES = {
'browser.newtabpage.enabled' => false,
'browser.startup.homepage' => 'about:blank',
'browser.usedOnWindows10.introURL' => 'about:blank',
'network.captive-portal-service.enabled' => false,
'security.csp.enable' => false
}.freeze
LOCK_FILES = %w[.parentlock parent.lock lock].freeze
attr_reader :name, :log_file
attr_writer :secure_ssl, :load_no_focus_lib
class << self
def ini
@ini ||= ProfilesIni.new
end
def from_name(name)
profile = ini[name]
return profile if profile
raise Error::WebDriverError, "unable to find profile named: #{name.inspect}"
end
def decoded(json)
JSON.parse(json)
end
end
#
# Create a new Profile instance
#
# @example User configured profile
#
# profile = Selenium::WebDriver::Firefox::Profile.new
# profile['network.proxy.http'] = 'localhost'
# profile['network.proxy.http_port'] = 9090
#
# driver = Selenium::WebDriver.for :firefox, :profile => profile
#
def initialize(model = nil)
@model = verify_model(model)
@additional_prefs = read_model_prefs
@extensions = {}
end
def layout_on_disk
profile_dir = @model ? create_tmp_copy(@model) : Dir.mktmpdir('webdriver-profile')
FileReaper << profile_dir
install_extensions(profile_dir)
delete_lock_files(profile_dir)
delete_extensions_cache(profile_dir)
update_user_prefs_in(profile_dir)
profile_dir
end
#
# Set a preference for this particular profile.
#
# @see http://kb.mozillazine.org/About:config_entries
# @see http://preferential.mozdev.org/preferences.html
#
def []=(key, value)
unless VALID_PREFERENCE_TYPES.any? { |e| value.is_a? e }
raise TypeError, "expected one of #{VALID_PREFERENCE_TYPES.inspect}, got #{value.inspect}:#{value.class}"
end
if value.is_a?(String) && Util.stringified?(value)
raise ArgumentError, "preference values must be plain strings: #{key.inspect} => #{value.inspect}"
end
@additional_prefs[key.to_s] = value
end
def port=(port)
self[WEBDRIVER_PREFS[:port]] = port
end
def log_file=(file)
@log_file = file
self[WEBDRIVER_PREFS[:log_file]] = file
end
#
# Add the extension (directory, .zip or .xpi) at the given path to the profile.
#
def add_extension(path, name = extension_name_for(path))
@extensions[name] = Extension.new(path)
end
def proxy=(proxy)
raise TypeError, "expected #{Proxy.name}, got #{proxy.inspect}:#{proxy.class}" unless proxy.is_a? Proxy
case proxy.type
when :manual
self['network.proxy.type'] = 1
set_manual_proxy_preference 'ftp', proxy.ftp
set_manual_proxy_preference 'http', proxy.http
set_manual_proxy_preference 'ssl', proxy.ssl
set_manual_proxy_preference 'socks', proxy.socks
self['network.proxy.no_proxies_on'] = proxy.no_proxy || ''
when :pac
self['network.proxy.type'] = 2
self['network.proxy.autoconfig_url'] = proxy.pac
when :auto_detect
self['network.proxy.type'] = 4
else
raise ArgumentError, "unsupported proxy type #{proxy.type}"
end
end
alias as_json encoded
private
def set_manual_proxy_preference(key, value)
return unless value
host, port = value.to_s.split(':', 2)
self["network.proxy.#{key}"] = host
self["network.proxy.#{key}_port"] = Integer(port) if port
end
def install_extensions(directory)
destination = File.join(directory, 'extensions')
@extensions.each do |name, extension|
WebDriver.logger.debug({extension: name}.inspect, id: :firefox_profile)
extension.write_to(destination)
end
end
def read_model_prefs
return {} unless @model
read_user_prefs(File.join(@model, 'user.js'))
end
def delete_extensions_cache(directory)
FileUtils.rm_f File.join(directory, 'extensions.cache')
end
def delete_lock_files(directory)
LOCK_FILES.each do |name|
FileUtils.rm_f File.join(directory, name)
end
end
def extension_name_for(path)
File.basename(path, File.extname(path))
end
def update_user_prefs_in(directory)
path = File.join(directory, 'user.js')
prefs = read_user_prefs(path)
prefs.merge! self.class::DEFAULT_PREFERENCES
prefs.merge!(@additional_prefs)
# If the user sets the home page, we should also start up there
prefs['startup.homepage_welcome_url'] ||= prefs['browser.startup.homepage']
write_prefs prefs, path
end
def read_user_prefs(path)
prefs = {}
return prefs unless File.exist?(path)
File.read(path).split("\n").each do |line|
next unless line =~ /user_pref\("([^"]+)"\s*,\s*(.+?)\);/
key = Regexp.last_match(1)&.strip
value = Regexp.last_match(2)&.strip
# wrap the value in an array to make it a valid JSON string.
prefs[key] = JSON.parse("[#{value}]").first
end
prefs
end
def write_prefs(prefs, path)
File.open(path, 'w') do |file|
prefs.each do |key, value|
file.puts %{user_pref("#{key}", #{value.to_json});}
end
end
end
end
# Profile
end # Firefox
end # WebDriver
end # Selenium
|