Class: Selenium::WebDriver::Chrome::Profile

Inherits:
Object
  • Object
show all
Includes:
ProfileHelper
Defined in:
build/rb/lib/selenium/webdriver/chrome/profile.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ProfileHelper

included, #to_json

Constructor Details

#initialize(model = nil) ⇒ Profile

Returns a new instance of Profile



30
31
32
33
34
# File 'build/rb/lib/selenium/webdriver/chrome/profile.rb', line 30

def initialize(model = nil)
  @model = verify_model(model)
  @extensions = []
  @encoded_extensions = []
end

Instance Attribute Details

#directoryObject (readonly)

Returns the value of attribute directory



28
29
30
# File 'build/rb/lib/selenium/webdriver/chrome/profile.rb', line 28

def directory
  @directory
end

Instance Method Details

#[](key) ⇒ Object



59
60
61
62
# File 'build/rb/lib/selenium/webdriver/chrome/profile.rb', line 59

def [](key)
  parts = key.split('.')
  parts.inject(prefs) { |a, e| a.fetch(e) }
end

#[]=(key, value) ⇒ Object

Set a preference in the profile.

See src.chromium.org/svn/trunk/src/chrome/common/pref_names.cc



54
55
56
57
# File 'build/rb/lib/selenium/webdriver/chrome/profile.rb', line 54

def []=(key, value)
  parts = key.split('.')
  parts[0..-2].inject(prefs) { |a, e| a[e] ||= {} }[parts.last] = value
end

#add_encoded_extension(encoded) ⇒ Object



44
45
46
# File 'build/rb/lib/selenium/webdriver/chrome/profile.rb', line 44

def add_encoded_extension(encoded)
  @encoded_extensions << encoded
end

#add_extension(path) ⇒ Object



36
37
38
39
40
41
42
# File 'build/rb/lib/selenium/webdriver/chrome/profile.rb', line 36

def add_extension(path)
  unless File.file?(path)
    raise Error::WebDriverError, "could not find extension at #{path.inspect}"
  end

  @extensions << path
end

#as_jsonObject



73
74
75
76
77
78
79
80
81
82
83
# File 'build/rb/lib/selenium/webdriver/chrome/profile.rb', line 73

def as_json(*)
  extensions = @extensions.map do |crx_path|
    File.open(crx_path, 'rb') { |crx_file| Base64.strict_encode64 crx_file.read }
  end

  extensions.concat(@encoded_extensions)

  opts = {directory: directory || layout_on_disk}
  opts[:extensions] = extensions if extensions.any?
  opts
end

#layout_on_diskObject



64
65
66
67
68
69
70
71
# File 'build/rb/lib/selenium/webdriver/chrome/profile.rb', line 64

def layout_on_disk
  @directory = @model ? create_tmp_copy(@model) : Dir.mktmpdir('webdriver-chrome-profile')
  FileReaper << @directory

  write_prefs_to @directory

  @directory
end