Class: Selenium::WebDriver::Firefox::Profile

Inherits:
Object
  • Object
show all
Defined in:
lib/selenium/webdriver/firefox/profile.rb

Constant Summary collapse

ANONYMOUS_PROFILE_NAME =
"WEBDRIVER_ANONYMOUS_PROFILE"
EXTENSION_NAME =
"[email protected]"
WEBDRIVER_EXTENSION_PATH =
File.expand_path("#{WebDriver.root}/selenium/webdriver/firefox/extension/webdriver.xpi")
WEBDRIVER_PREFS =
{
  :native_events    => 'webdriver_enable_native_events',
  :untrusted_certs  => 'webdriver_accept_untrusted_certs',
  :untrusted_issuer => 'webdriver_assume_untrusted_issuer',
  :port             => 'webdriver_firefox_port',
  :log_file         => 'webdriver.log.file'
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model = nil) ⇒ Profile

Create a new Profile instance

Examples:

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


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/selenium/webdriver/firefox/profile.rb', line 42

def initialize(model = nil)
  @model = verify_model(model)

  model_prefs = read_model_prefs

  if model_prefs.empty?
    @native_events     = DEFAULT_ENABLE_NATIVE_EVENTS
    @secure_ssl        = DEFAULT_SECURE_SSL
    @untrusted_issuer  = DEFAULT_ASSUME_UNTRUSTED_ISSUER
    @load_no_focus_lib = DEFAULT_LOAD_NO_FOCUS_LIB

    @additional_prefs  = {}
  else
    # TODO: clean this up
    @native_events     = model_prefs.delete(WEBDRIVER_PREFS[:native_events]) == "true"
    @secure_ssl        = model_prefs.delete(WEBDRIVER_PREFS[:untrusted_certs]) != "true"
    @untrusted_issuer  = model_prefs.delete(WEBDRIVER_PREFS[:untrusted_issuer]) == "true"
    @load_no_focus_lib = model_prefs.delete(WEBDRIVER_PREFS[:load_no_focus_lib]) == "true" # not stored in profile atm, so will always be false.

    @additional_prefs  = model_prefs
  end

  @extensions        = {}
end

Instance Attribute Details

#load_no_focus_lib=(value) ⇒ Object (writeonly)

Sets the attribute load_no_focus_lib

Parameters:

  • value

    the value to set the attribute load_no_focus_lib to.



18
19
20
# File 'lib/selenium/webdriver/firefox/profile.rb', line 18

def load_no_focus_lib=(value)
  @load_no_focus_lib = value
end

#log_fileObject

Returns the value of attribute log_file.



17
18
19
# File 'lib/selenium/webdriver/firefox/profile.rb', line 17

def log_file
  @log_file
end

#nameObject (readonly)

Returns the value of attribute name.



17
18
19
# File 'lib/selenium/webdriver/firefox/profile.rb', line 17

def name
  @name
end

#native_events=(value) ⇒ Object (writeonly)

Sets the attribute native_events

Parameters:

  • value

    the value to set the attribute native_events to.



18
19
20
# File 'lib/selenium/webdriver/firefox/profile.rb', line 18

def native_events=(value)
  @native_events = value
end

#secure_ssl=(value) ⇒ Object (writeonly)

Sets the attribute secure_ssl

Parameters:

  • value

    the value to set the attribute secure_ssl to.



18
19
20
# File 'lib/selenium/webdriver/firefox/profile.rb', line 18

def secure_ssl=(value)
  @secure_ssl = value
end

Class Method Details

.from_json(json) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/selenium/webdriver/firefox/profile.rb', line 87

def self.from_json(json)
  data = JSON.parse(json).fetch('zip')

  # can't use Tempfile here since it doesn't support File::BINARY mode on 1.8
  # can't use Dir.mktmpdir(&blk) because of http://jira.codehaus.org/browse/JRUBY-4082
  tmp_dir = Dir.mktmpdir
  begin
    zip_path = File.join(tmp_dir, "webdriver-profile-duplicate-#{json.hash}")
    File.open(zip_path, "wb") { |zip_file| zip_file << Base64.decode64(data) }

    new Zipper.unzip(zip_path)
  ensure
    FileUtils.rm_rf tmp_dir
  end
end

.from_name(name) ⇒ Object



25
26
27
# File 'lib/selenium/webdriver/firefox/profile.rb', line 25

def from_name(name)
  ini[name]
end

.iniObject



21
22
23
# File 'lib/selenium/webdriver/firefox/profile.rb', line 21

def ini
  @ini ||= ProfilesIni.new
end

Instance Method Details

#[]=(key, value) ⇒ Object

Set a preference for this particular profile.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/selenium/webdriver/firefox/profile.rb', line 108

def []=(key, value)
  case value
  when String
    if Util.stringified?(value)
      raise ArgumentError, "preference values must be plain strings: #{key.inspect} => #{value.inspect}"
    end

    value = %{"#{value}"}
  when TrueClass, FalseClass, Integer, Float
    value = value.to_s
  else
    raise TypeError, "invalid preference: #{value.inspect}:#{value.class}"
  end

  @additional_prefs[key.to_s] = value
end

#add_extension(path, name = extension_name_for(path)) ⇒ Object

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



144
145
146
# File 'lib/selenium/webdriver/firefox/profile.rb', line 144

def add_extension(path, name = extension_name_for(path))
  @extensions[name] = Extension.new(path)
end

#add_webdriver_extensionObject



134
135
136
137
138
# File 'lib/selenium/webdriver/firefox/profile.rb', line 134

def add_webdriver_extension
  unless @extensions.has_key?(:webdriver)
    add_extension(WEBDRIVER_EXTENSION_PATH, :webdriver)
  end
end

#as_json(opts = nil) ⇒ Object



79
80
81
# File 'lib/selenium/webdriver/firefox/profile.rb', line 79

def as_json(opts = nil)
  {'zip' => Zipper.zip(layout_on_disk)}
end

#assume_untrusted_certificate_issuer=(bool) ⇒ Object



164
165
166
# File 'lib/selenium/webdriver/firefox/profile.rb', line 164

def assume_untrusted_certificate_issuer=(bool)
  @untrusted_issuer = bool
end

#assume_untrusted_certificate_issuer?Boolean

Returns:

  • (Boolean)


160
161
162
# File 'lib/selenium/webdriver/firefox/profile.rb', line 160

def assume_untrusted_certificate_issuer?
  @untrusted_issuer == true
end

#layout_on_diskObject



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/selenium/webdriver/firefox/profile.rb', line 67

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

#load_no_focus_lib?Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/selenium/webdriver/firefox/profile.rb', line 152

def load_no_focus_lib?
  @load_no_focus_lib == true
end

#native_events?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/selenium/webdriver/firefox/profile.rb', line 148

def native_events?
  @native_events == true
end

#port=(port) ⇒ Object



125
126
127
# File 'lib/selenium/webdriver/firefox/profile.rb', line 125

def port=(port)
  self[WEBDRIVER_PREFS[:port]] = port
end

#proxy=(proxy) ⇒ Object



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
# File 'lib/selenium/webdriver/firefox/profile.rb', line 168

def proxy=(proxy)
  unless proxy.kind_of? Proxy
    raise TypeError, "expected #{Proxy.name}, got #{proxy.inspect}:#{proxy.class}"
  end

  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

    if proxy.no_proxy
      self["network.proxy.no_proxies_on"] = proxy.no_proxy
    else
      self["network.proxy.no_proxies_on"] = ""
    end
  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

  proxy
end

#secure_ssl?Boolean

Returns:

  • (Boolean)


156
157
158
# File 'lib/selenium/webdriver/firefox/profile.rb', line 156

def secure_ssl?
  @secure_ssl == true
end

#to_json(*args) ⇒ Object



83
84
85
# File 'lib/selenium/webdriver/firefox/profile.rb', line 83

def to_json(*args)
  as_json.to_json(*args)
end