Class: Selenium::WebDriver::Safari::Extension

Inherits:
Object
  • Object
show all
Defined in:
lib/selenium/webdriver/safari/extension.rb

Constant Summary collapse

PLIST =
<<-XML
  <?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  <plist version="1.0">
  <dict>
    <key>Available Updates</key>
    <dict>
      <key>Last Update Check Time</key>
      <real>370125644.75941497</real>
      <key>Updates List</key>
      <array/>
    </dict>
    <key>Installed Extensions</key>
    <array>
      <dict>
        <key>Added Non-Default Toolbar Items</key>
        <array/>
        <key>Archive File Name</key>
        <string>WebDriver.safariextz</string>
        <key>Bundle Directory Name</key>
        <string>WebDriver.safariextension</string>
        <key>Enabled</key>
        <true/>
        <key>Hidden Bars</key>
        <array/>
        <key>Removed Default Toolbar Items</key>
        <array/>
      </dict>
    </array>
    <key>Version</key>
    <integer>1</integer>
  </dict>
  </plist>
XML

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Extension

Returns a new instance of Extension.



41
42
43
44
# File 'lib/selenium/webdriver/safari/extension.rb', line 41

def initialize(opts = {})
  @custom_data_dir = opts[:custom_data_dir]
  @installed       = false
end

Instance Method Details

#backup_directoryObject



84
85
86
# File 'lib/selenium/webdriver/safari/extension.rb', line 84

def backup_directory
  Pathname.new("#{install_directory.to_s}.bak")
end

#extension_destinationObject



80
81
82
# File 'lib/selenium/webdriver/safari/extension.rb', line 80

def extension_destination
  install_directory.join('WebDriver.safariextz')
end

#extension_sourceObject



76
77
78
# File 'lib/selenium/webdriver/safari/extension.rb', line 76

def extension_source
  Safari.resource_path.join('SafariDriver.safariextz')
end

#installObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/selenium/webdriver/safari/extension.rb', line 46

def install
  return if @installed

  if install_directory.exist?
    FileUtils.mv install_directory.to_s, backup_directory.to_s
  end

  install_directory.mkpath

  extension_destination.rmtree if extension_destination.exist?
  FileUtils.cp extension_source.to_s, extension_destination.to_s

  plist_destination.open('w') { |io| io << PLIST }

  at_exit { uninstall }
  @installed = true
end

#install_directoryObject



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/selenium/webdriver/safari/extension.rb', line 92

def install_directory
  @install_directory ||= (
    data_dir = Pathname.new(@custom_data_dir || safari_data_dir)

    unless data_dir.exist? && data_dir.directory?
      raise Errno::ENOENT, "Safari data directory not found at #{dir.to_s}"
    end

    data_dir.join('Extensions')
  )
end

#plist_destinationObject



88
89
90
# File 'lib/selenium/webdriver/safari/extension.rb', line 88

def plist_destination
  install_directory.join('Extensions.plist')
end

#safari_data_dirObject



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/selenium/webdriver/safari/extension.rb', line 104

def safari_data_dir
  current = Platform.os

  case current
  when :macosx
    Pathname.new(Platform.home).join('Library/Safari')
  when :windows
    Pathname.new(ENV['APPDATA']).join('Apple Computer/Safari')
  else
    raise Error::WebDriverError, "unsupported platform: #{current}"
  end
end

#uninstallObject



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/selenium/webdriver/safari/extension.rb', line 64

def uninstall
  return unless @installed

  install_directory.rmtree if install_directory.exist?

  if backup_directory.exist?
    FileUtils.mv backup_directory.to_s, install_directory.to_s
  end
ensure
  @installed = false
end