Class: ChromedriverUpdate
- Inherits:
-
Object
- Object
- ChromedriverUpdate
- 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.1'.freeze
Class Method Summary collapse
-
.auto_update_chromedriver(force: false) ⇒ Object
Update the installed version of chromedriver automatically fitting to the currently installed version of chrome.
-
.chromedriver_closest_link_for_version(version) ⇒ String
Get the download URL of the closest chromedriver version fitting the given chrome version.
- .chromedriver_link_for_version(version) ⇒ Object
-
.chromedriver_path ⇒ String
Get the path of the installed chromedriver.
-
.installed_chrome_version ⇒ String
Get the currently installed version of chrome.
-
.installed_chromedriver_version ⇒ String
Get the currently installed version of chromedriver.
-
.mac_platform ⇒ String
Detect macOS platform.
Class Method Details
.auto_update_chromedriver(force: false) ⇒ Object
Update the installed version of chromedriver automatically fitting to the currently installed version of chrome
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 59 60 |
# 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.(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) `chmod +x "#{original_chromedriver_path}"` rescue begin `chmod +x "#{original_chromedriver_path}"` rescue begin `sudo chmod +x "#{original_chromedriver_path}"` rescue end 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 |
.chromedriver_closest_link_for_version(version) ⇒ String
Get the download URL of the closest chromedriver version fitting the given chrome version
114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/chromedriver_update.rb', line 114 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_link_for_version(version) ⇒ Object
98 99 100 101 102 103 104 105 106 |
# File 'lib/chromedriver_update.rb', line 98 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_path ⇒ String
Get the path of the installed chromedriver
144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/chromedriver_update.rb', line 144 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_version ⇒ String
Get the currently installed version of chrome
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/chromedriver_update.rb', line 67 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_version ⇒ String
Get the currently installed version of chromedriver
94 95 96 |
# File 'lib/chromedriver_update.rb', line 94 def self.installed_chromedriver_version `#{chromedriver_path} --version`.scan(/([0-9\.]+)/).flatten.first end |
.mac_platform ⇒ String
Detect macOS platform
131 132 133 134 135 136 137 |
# File 'lib/chromedriver_update.rb', line 131 def self.mac_platform if RUBY_PLATFORM.include?("x86") || RUBY_PLATFORM.include?("x64") 'mac-x64' else 'mac-arm64' end end |