class Selenium::WebDriver::Firefox::Profile

Constants

DEFAULT_PREFERENCES
VALID_PREFERENCE_TYPES

Attributes

load_no_focus_lib[W]
log_file[R]
name[R]
secure_ssl[W]

Public Class Methods

decoded(json) click to toggle source
# File lib/selenium/webdriver/firefox/profile.rb, line 51
def decoded(json)
  JSON.parse(json)
end
from_name(name) click to toggle source
# File lib/selenium/webdriver/firefox/profile.rb, line 44
def from_name(name)
  profile = ini[name]
  return profile if profile

  raise Error::WebDriverError, "unable to find profile named: #{name.inspect}"
end
ini() click to toggle source
# File lib/selenium/webdriver/firefox/profile.rb, line 40
def ini
  @ini ||= ProfilesIni.new
end
new(model = nil) click to toggle source

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
# File lib/selenium/webdriver/firefox/profile.rb, line 68
def initialize(model = nil)
  @model = verify_model(model)

  @additional_prefs = read_model_prefs
  @extensions = {}
end

Public Instance Methods

[]=(key, value) click to toggle source

Set a preference for this particular profile.

@see kb.mozillazine.org/About:config_entries @see preferential.mozdev.org/preferences.html

# File lib/selenium/webdriver/firefox/profile.rb, line 94
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) && stringified?(value)
    raise ArgumentError, "preference values must be plain strings: #{key.inspect} => #{value.inspect}"
  end

  @additional_prefs[key.to_s] = value
end
add_extension(path, name = extension_name_for(path)) click to toggle source

Add the extension (directory, .zip or .xpi) at the given path to the profile.

# File lib/selenium/webdriver/firefox/profile.rb, line 119
def add_extension(path, name = extension_name_for(path))
  @extensions[name] = Extension.new(path)
end
layout_on_disk() click to toggle source
# File lib/selenium/webdriver/firefox/profile.rb, line 75
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
log_file=(file) click to toggle source
# File lib/selenium/webdriver/firefox/profile.rb, line 110
def log_file=(file)
  @log_file = file
  self[WEBDRIVER_PREFS[:log_file]] = file
end
port=(port) click to toggle source
# File lib/selenium/webdriver/firefox/profile.rb, line 106
def port=(port)
  self[WEBDRIVER_PREFS[:port]] = port
end
proxy=(proxy) click to toggle source
# File lib/selenium/webdriver/firefox/profile.rb, line 123
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

Private Instance Methods

delete_extensions_cache(directory) click to toggle source
# File lib/selenium/webdriver/firefox/profile.rb, line 174
def delete_extensions_cache(directory)
  FileUtils.rm_f File.join(directory, 'extensions.cache')
end
delete_lock_files(directory) click to toggle source
# File lib/selenium/webdriver/firefox/profile.rb, line 178
def delete_lock_files(directory)
  %w[.parentlock parent.lock].each do |name|
    FileUtils.rm_f File.join(directory, name)
  end
end
extension_name_for(path) click to toggle source
# File lib/selenium/webdriver/firefox/profile.rb, line 184
def extension_name_for(path)
  File.basename(path, File.extname(path))
end
install_extensions(directory) click to toggle source
# File lib/selenium/webdriver/firefox/profile.rb, line 159
def install_extensions(directory)
  destination = File.join(directory, 'extensions')

  @extensions.each do |name, extension|
    WebDriver.logger.debug({extenstion: name}.inspect)
    extension.write_to(destination)
  end
end
read_model_prefs() click to toggle source
# File lib/selenium/webdriver/firefox/profile.rb, line 168
def read_model_prefs
  return {} unless @model

  read_user_prefs(File.join(@model, 'user.js'))
end
read_user_prefs(path) click to toggle source
# File lib/selenium/webdriver/firefox/profile.rb, line 200
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
set_manual_proxy_preference(key, value) click to toggle source
# File lib/selenium/webdriver/firefox/profile.rb, line 150
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
stringified?(str) click to toggle source
# File lib/selenium/webdriver/firefox/profile.rb, line 225
def stringified?(str)
  /^".*"$/.match?(str)
end
update_user_prefs_in(directory) click to toggle source
# File lib/selenium/webdriver/firefox/profile.rb, line 188
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
write_prefs(prefs, path) click to toggle source
# File lib/selenium/webdriver/firefox/profile.rb, line 217
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