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.3.0'.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
56
57
58
# 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))
      puts "Could not find same chromedriver version for chrome version '#{installed_chrome_version}'. Fallback to closest version '#{installed_chrome_version}'"
    else
      puts "Found same chromedriver version '#{installed_chrome_version}' for chrome 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



135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/chromedriver_update.rb', line 135

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

.chromedriver_closest_version_for_version(version) ⇒ Object



148
149
150
151
152
# File 'lib/chromedriver_update.rb', line 148

def self.chromedriver_closest_version_for_version(version)
  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['version']
end


96
97
98
99
100
101
102
103
104
# File 'lib/chromedriver_update.rb', line 96

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:



171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/chromedriver_update.rb', line 171

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



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

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



92
93
94
# File 'lib/chromedriver_update.rb', line 92

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

.mac_platformString

Detect macOS platform

Returns:

  • (String)

    platform for macos



158
159
160
161
162
163
164
# File 'lib/chromedriver_update.rb', line 158

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


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/chromedriver_update.rb', line 107

def self.print_version_status
  puts "Installed versions"
  puts "- chrome: #{installed_chrome_version}"
  puts "- chromedriver: #{installed_chromedriver_version} @ #{chromedriver_path}"
  puts
  puts "Available versions of chromedriver"
  exact = if remote_file_exists? chromedriver_link_for_version(installed_chrome_version)
            installed_chrome_version
          else
            "not found"
          end
  closest = if remote_file_exists? chromedriver_closest_link_for_version(installed_chrome_version)
              chromedriver_closest_version_for_version(installed_chrome_version)
            else
              "not found"
            end
  puts "- exact match: #{exact}"
  puts "  -> #{chromedriver_link_for_version(installed_chrome_version)}" if exact != "not found"
  puts "- closest lower match: #{closest}"
  puts "  -> #{chromedriver_closest_link_for_version(installed_chrome_version)}" if closest != "not found"
end

.remote_file_exists?(url) ⇒ Boolean

Check if a remote file exists, without downloading it, by checking header (OPTIONS)

Parameters:

  • url (String)

    to check

Returns:

  • (Boolean)


187
188
189
190
# File 'lib/chromedriver_update.rb', line 187

def self.remote_file_exists?(url)
  response = HTTParty.head(url)
  response.success?
end