selenium_chrome_helper 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4d509320040ae664e9c2565e483f46d08467aa31ec987c450d98edeeb8d2b9df
4
+ data.tar.gz: f54dfa1bd8b8b1bdfa81d298195468c8437b65522d713a475318c6e5dc3de549
5
+ SHA512:
6
+ metadata.gz: '0449ea6af9e6d9795fd12f45d8b620cb7e7ce7625bc81d678a30df1ae7ad07ddc34cb0234af37657fff167d881af0bd6397548a783d792be0d70e725f95ce389'
7
+ data.tar.gz: 3360976fb82b04165e80cc229e4da018eb98172becb83ec553d40dbb37fb98b347e58c5715d4decf273c33826011beddbd25ae104f6094a9b98e805775324a64
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/railtie'
4
+ require 'capybara'
5
+ require 'selenium/webdriver'
6
+
7
+ module SeleniumChromeHelper
8
+ CHROME_VERSION = ENV.fetch('SELENIUM_CHROME_VERSION', '134.0.6998.165')
9
+
10
+ # This Railtie configures Capybara to use a specific version of Google Chrome for testing.
11
+ # It registers two drivers: one for headless testing and one for non-headless testing.
12
+ class Railtie < Rails::Railtie
13
+ initializer 'selenium_chrome_helper.configure_capybara' do
14
+ platform = determine_platform
15
+ base_path = Rails.root.join('.chrome-for-testing', CHROME_VERSION)
16
+ chrome_path = determine_chrome_path(base_path, platform)
17
+ driver_path = base_path.join("chromedriver-#{platform}", 'chromedriver').to_s
18
+
19
+ Capybara.register_driver :chrome_for_testing do |app|
20
+ Selenium::WebDriver::Chrome::Service.driver_path = driver_path
21
+
22
+ options = configure_browser_options(chrome_path)
23
+ Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
24
+ end
25
+
26
+ Capybara.register_driver :chrome_for_testing do |app|
27
+ Selenium::WebDriver::Chrome::Service.driver_path = driver_path
28
+
29
+ options = configure_browser_options(chrome_path, headless: false)
30
+ Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ def determine_platform
37
+ case RUBY_PLATFORM
38
+ when /darwin/
39
+ `uname -m`.strip == 'arm64' ? 'mac-arm64' : 'mac-x64'
40
+ when /linux/
41
+ 'linux64'
42
+ else
43
+ raise "Unsupported platform: #{RUBY_PLATFORM}"
44
+ end
45
+ end
46
+
47
+ def determine_chrome_path(base_path, platform)
48
+ if platform.start_with?('mac')
49
+ base_path.join("chrome-#{platform}", 'Google Chrome for Testing.app', 'Contents',
50
+ 'MacOS', 'Google Chrome for Testing').to_s
51
+ else
52
+ base_path.join("chrome-#{platform}", 'chrome').to_s
53
+ end
54
+ end
55
+
56
+ def configure_browser_options(chrome_path, headless: true)
57
+ options = Selenium::WebDriver::Chrome::Options.new
58
+ options.binary = chrome_path
59
+ options.add_argument('--headless=new') if headless
60
+ options.add_argument('--disable-gpu')
61
+ options.add_argument('--no-sandbox')
62
+ options.add_argument('--window-size=1400,1400')
63
+ options
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SeleniumChromeHelper
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,6 @@
1
+ require "selenium_chrome_helper/version"
2
+
3
+ # Auto-load rake tasks when included in a Rails app or via `require`
4
+ if defined?(Rake)
5
+ load File.expand_path("tasks/install.rake", __dir__)
6
+ end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'fileutils'
4
+ require 'open-uri'
5
+ require 'json'
6
+ require 'zip' unless defined?(Zip)
7
+
8
+ # rubocop:disable Metrics/BlockLength, Security/Open
9
+ namespace :chrome do
10
+ desc 'Download and extract a known good version of Chrome for Testing'
11
+ task :install, [:version] do |_, args|
12
+ version = args[:version] || '134.0.6998.165'
13
+ api_url = 'https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json'
14
+ install_base = File.expand_path("chrome-for-testing/\#{version}", Dir.pwd)
15
+
16
+ platform = case RUBY_PLATFORM
17
+ when /darwin/
18
+ `uname -m`.strip == 'arm64' ? 'mac-arm64' : 'mac-x64'
19
+ when /linux/
20
+ 'linux64'
21
+ else
22
+ raise "Unsupported platform: \#{RUBY_PLATFORM}"
23
+ end
24
+
25
+ puts "➡️ Platform: \#{platform}"
26
+ puts "📡 Fetching metadata for Chrome version \#{version}..."
27
+
28
+ json = URI.open(api_url).read
29
+ data = JSON.parse(json)
30
+
31
+ version_info = data['versions'].find { |v| v['version'] == version }
32
+
33
+ abort "❌ Version \#{version} not found in known-good list." unless version_info
34
+
35
+ chrome_url = version_info.dig('downloads', 'chrome')&.find { |d| d['platform'] == platform }&.dig('url')
36
+ driver_url = version_info.dig('downloads', 'chromedriver')&.find { |d| d['platform'] == platform }&.dig('url')
37
+
38
+ unless chrome_url && driver_url
39
+ abort "❌ No matching downloads found for platform \#{platform} and version \#{version}"
40
+ end
41
+
42
+ puts "⬇️ Chrome URL: \#{chrome_url}"
43
+ puts "⬇️ Chromedriver URL: \#{driver_url}"
44
+
45
+ FileUtils.mkdir_p(install_base)
46
+
47
+ download_and_unzip(chrome_url, File.join(install_base, 'chrome.zip'), install_base)
48
+ download_and_unzip(driver_url, File.join(install_base, 'chromedriver.zip'), install_base)
49
+
50
+ puts "✅ Chrome and Chromedriver installed to \#{install_base}"
51
+ end
52
+
53
+ def download_and_unzip(url, zip_path, extract_to)
54
+ download(url, zip_path)
55
+ unzip(zip_path, extract_to)
56
+
57
+ File.delete(zip_path)
58
+ end
59
+
60
+ def download(url, dest)
61
+ URI.open(url) do |remote_file|
62
+ File.open(dest, 'wb') { |file| file.write(remote_file.read) }
63
+ end
64
+ end
65
+
66
+ def unzip(zip_path, extract_to)
67
+ Zip::File.open(zip_path) do |zip_file|
68
+ zip_file.each do |entry|
69
+ entry_path = File.join(extract_to, entry.name)
70
+ FileUtils.mkdir_p(File.dirname(entry_path))
71
+ zip_file.extract(entry, entry_path) unless File.exist?(entry_path)
72
+ end
73
+ end
74
+ end
75
+ end
76
+ # rubocop:enable Metrics/BlockLength, Security/Open
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: selenium_chrome_helper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Gonzalo Robaina
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2025-03-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: zip
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Adds a rake task (`chrome:install`) to easily fetch a pinned version
42
+ of Chrome and Chromedriver locally or in CI.
43
+ email:
44
+ - gonzalor@gmail.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - lib/selenium_chrome_helper.rb
50
+ - lib/selenium_chrome_helper/railtie.rb
51
+ - lib/selenium_chrome_helper/version.rb
52
+ - lib/tasks/install.rake
53
+ homepage: https://github.com/yourname/selenium_chrome_helper
54
+ licenses:
55
+ - MIT
56
+ metadata: {}
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubygems_version: 3.4.10
73
+ signing_key:
74
+ specification_version: 4
75
+ summary: Downloads and installs Chrome for Testing for use with Selenium system specs.
76
+ test_files: []