Last active
April 3, 2024 12:58
-
-
Save gangelo/1e3388bf895d644b0bdf9d5c394483fa to your computer and use it in GitHub Desktop.
Downloads and installs chromedriver for use in rails tests, using capybara.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'fileutils' | |
require 'json' | |
require 'net/http' | |
require 'tmpdir' | |
class ChromedriverDownloader | |
def self.download | |
new.download | |
end | |
def download | |
return if File.exist?("~/bin/chromedriver-#{major_version}") | |
Dir.mktmpdir do |dir| | |
Dir.chdir(dir) do | |
fetch | |
unzip | |
install | |
symlink | |
end | |
end | |
end | |
private | |
def fetch | |
`curl #{latest_platform_uri} > chromedriver.zip` | |
end | |
def unzip | |
`unzip chromedriver.zip` | |
end | |
def install | |
FileUtils.mv("./chromedriver-#{platform}/chromedriver", install_file) | |
end | |
def symlink | |
File.unlink(symlink_name) if File.exist?(symlink_name) | |
File.symlink(install_file.to_s, symlink_name) | |
end | |
def symlink_name | |
"#{ENV["HOME"]}/bin/chromedriver" | |
end | |
def chrome_version | |
@chrome_version ||= (`'/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome' --version`).gsub(/[^0-9\.]/, '') | |
end | |
def platform | |
if RUBY_PLATFORM.include?("arm") || RUBY_PLATFORM.include?("aarch64") | |
"mac-arm64" | |
elsif RUBY_PLATFORM.include?("x86") || RUBY_PLATFORM.include?("amd64") | |
"mac-x64" | |
else | |
raise "Unsupported platform: #{RUBY_PLATFORM}" | |
end | |
end | |
def major_version | |
chrome_version.split('.')[0] | |
end | |
def latest_platform_uri | |
response = Net::HTTP.get(URI(download_json_api_url)) | |
parsed = JSON.parse(response) | |
urls = parsed.dig("milestones", "#{major_version}", "downloads", "chromedriver") | |
urls.find { |i| i["platform"] == platform }["url"] | |
end | |
def download_json_api_url | |
"https://googlechromelabs.github.io/chrome-for-testing/latest-versions-per-milestone-with-downloads.json" | |
end | |
def install_file | |
"#{ENV["HOME"]}/bin/chromedriver-#{major_version}" | |
end | |
end | |
ChromedriverDownloader.download if __FILE__ == $PROGRAM_NAME |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment