Skip to content

Instantly share code, notes, and snippets.

@gangelo
Last active April 3, 2024 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gangelo/1e3388bf895d644b0bdf9d5c394483fa to your computer and use it in GitHub Desktop.
Save gangelo/1e3388bf895d644b0bdf9d5c394483fa to your computer and use it in GitHub Desktop.
Downloads and installs chromedriver for use in rails tests, using capybara.
#!/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