Class: ChromedriverUpdate

Inherits:
Object
  • Object
show all
Defined in:
lib/chromedriver_update.rb,
lib/chromedriver_update/version.rb

Constant Summary collapse

CHROME_DOWNLOADS_LIST_URL =
'https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json'
VERSION =
'0.2.2'.freeze

Class Method Summary collapse

Class Method Details

.auto_update_chromedriver(force: false) ⇒ Object

Update the installed version of chromedriver automatically fitting to the currently installed version of chrome

Parameters:

  • force=false (Boolean)

    force the update, even if the version is already installed



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
# File 'lib/chromedriver_update.rb', line 21

def self.auto_update_chromedriver(force: false)
  if installed_chrome_version.split(".").first != installed_chromedriver_version.split(".").first || force
    original_chromedriver_version = installed_chromedriver_version
    original_chromedriver_path = chromedriver_path
    chromedriver_zip = HTTParty.get(chromedriver_link_for_version(installed_chrome_version))
    if chromedriver_zip.code == 404 # fallback to latest lower version
      chromedriver_zip = HTTParty.get(chromedriver_closest_link_for_version(installed_chrome_version))
    end
    destination_dir = File.expand_path(File.dirname(__FILE__) + "/../tmp")
    FileUtils.mkdir_p destination_dir
    Zip::File.open_buffer(chromedriver_zip.body) do |zip_files|
      zip_files.each do |entry|
        if (entry.name.end_with?("/chromedriver") || entry.name.end_with?("/chromedriver.exe"))
          download_path = File.join(destination_dir, File.basename(entry.name))
          FileUtils.rm_f(download_path)
          entry.extract(download_path)
          FileUtils.mv(download_path, chromedriver_path, force: true)
          unless OS.windows?
            begin
              FileUtils.chmod("+x", original_chromedriver_path)
            rescue
              begin
                `sudo chmod +x "#{original_chromedriver_path}"`
              rescue
              end
            end
          end
        end
      end
    end
    puts "Updated Chromedriver from '#{original_chromedriver_version}' to '#{installed_chromedriver_version}'! Chrome is '#{installed_chrome_version}'."
  else
    puts "Chromedriver is already up to date at major version! Chromedriver: '#{installed_chromedriver_version}' Chrome: '#{installed_chrome_version}'"
  end
end

Get the download URL of the closest chromedriver version fitting the given chrome version

Parameters:

  • version (String)

    of chrome to find the best chromedriver version for

Returns:

  • (String)

    link to the matching chromedriver download for the local platform



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/chromedriver_update.rb', line 109

def self.chromedriver_closest_link_for_version(version)
  platform = if OS.windows?
               "win64"
             elsif OS.mac?
               mac_platform
             else
               "linux64"
             end
  list = JSON.parse(HTTParty.get(CHROME_DOWNLOADS_LIST_URL).body)
  latest_match = list['versions'].filter { |el| el['version'].start_with?(version.split(".")[0...1].join(".")) && el['version'].split(".")[2].to_i < version.split(".")[2].to_i && el['downloads']['chromedriver'] }.last
  latest_match['downloads']['chromedriver'].filter { |el| el['platform'] == platform }.first['url']
end


93
94
95
96
97
98
99
100
101
# File 'lib/chromedriver_update.rb', line 93

def self.chromedriver_link_for_version(version)
  if OS.windows?
    "https://storage.googleapis.com/chrome-for-testing-public/#{version}/win64/chromedriver-win64.zip"
  elsif OS.mac?
    "https://storage.googleapis.com/chrome-for-testing-public/#{version}/#{mac_platform}/chromedriver-mac-arm64.zip"
  else
    "https://storage.googleapis.com/chrome-for-testing-public/#{version}/linux64/chromedriver-linux64.zip"
  end
end

.chromedriver_pathString

Get the path of the installed chromedriver

Returns:

  • (String)

    path to the installed chromedriver

Raises:



139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/chromedriver_update.rb', line 139

def self.chromedriver_path
  version = if OS.windows?
              `where chromedriver`.strip
            else
              `which chromedriver`.strip
            end
  if version == ""
    raise ChromedriverNotFoundError.new "Could not detect installed chromedriver!"
  else
    version
  end
end

.installed_chrome_versionString

Get the currently installed version of chrome

Returns:

  • (String)

    current installed chrome version



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/chromedriver_update.rb', line 62

def self.installed_chrome_version
  begin
    if OS.windows?
      version = `reg query "HKEY_CURRENT_USER\\Software\\Google\\Chrome\\BLBeacon" /v version`
      version.scan(/version[^0-9]*([0-9\.]+)/).flatten.first
    elsif OS.mac?
      version = `"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" --version`
      version.scan(/([0-9\.]+)/).flatten.first
    else
      version = ""
      begin
        version = `google-chrome --version`
      rescue Errno::ENOENT => e
        version = `google-chrome-stable --version`
      end
      version.scan(/([0-9\.]+)/).flatten.first
    end
  rescue => e
    raise ChromeNotFoundError.new "Could not detect a installed chrome version!"
  end
end

.installed_chromedriver_versionString

Get the currently installed version of chromedriver

Returns:

  • (String)

    current installed chromedriver version



89
90
91
# File 'lib/chromedriver_update.rb', line 89

def self.installed_chromedriver_version
  `#{chromedriver_path} --version`.scan(/([0-9\.]+)/).flatten.first
end

.mac_platformString

Detect macOS platform

Returns:

  • (String)

    platform for macos



126
127
128
129
130
131
132
# File 'lib/chromedriver_update.rb', line 126

def self.mac_platform
  if RUBY_PLATFORM.include?("x86") || RUBY_PLATFORM.include?("x64")
    'mac-x64'
  else
    'mac-arm64'
  end
end