selenium_chrome_helper 0.1.1 → 0.1.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f69e817cd92bb8e8b77889af1ecb48b5648d34bdfbda30f1dcf59d4fad442ec9
4
- data.tar.gz: 1831693aeb8fb90debbf25a8103a1294b92dcbd4f8645288774f1bdecb4fb37c
3
+ metadata.gz: f8a3dc606169a2ead1a95947305d962a396049f2b9efccfbde7159acf34a7c3e
4
+ data.tar.gz: 9f9e3a441cd650e01dceb5d47bbdaa7e3e8360d4f2fc109532ffe5ab580e35ad
5
5
  SHA512:
6
- metadata.gz: c381a54d1165b9636c8d52a0cd0cbc07189164ffaa00a2618e0bd3e3b86eaeca900a19fd16c5543bb7875a210a878ef0bca2a151d5dbcbf14c8d1a50bfe97bb1
7
- data.tar.gz: 4fcba792fe8aa8f39d6342c99e8ae5745482523914dd4b50999b52a19906c0345b2993e7a6d741ca33c5abdf462d90aa88c74c4afa94649f952e903ed11aaaa8
6
+ metadata.gz: 7d7f88532fa87a1914bb543c5cbf4d54d739700ed7e25d2b0bbf3df5984a6851d2486b319c109adc1e0be4c17b05e8e0ccd7a2254ebff3c4a00fdc4ffa1f2211
7
+ data.tar.gz: c15546c7264ddb8abe4bc278b73a54ed1db0717a31b14580b0638d43f4df862dda5fd0869e6ef75f80a79821d30b0fcb1a95e2fb0152b09edcf16dfdd10f4c76
@@ -10,30 +10,21 @@ module SeleniumChromeHelper
10
10
  # This Railtie configures Capybara to use a specific version of Google Chrome for testing.
11
11
  # It registers two drivers: one for headless testing and one for non-headless testing.
12
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_gui do |app|
27
- Selenium::WebDriver::Chrome::Service.driver_path = driver_path
13
+ rake_tasks do
14
+ load File.expand_path('../tasks/install.rake', __dir__)
15
+ end
28
16
 
29
- options = configure_browser_options(chrome_path, headless: false)
30
- Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
31
- end
17
+ initializer 'selenium_chrome_helper.configure_capybara' do
18
+ SeleniumChromeHelper::Railtie.register_driver(:chrome_for_testing, headless: true)
19
+ SeleniumChromeHelper::Railtie.register_driver(:chrome_for_testing_gui, headless: false)
32
20
  end
33
21
 
34
- private
22
+ def self.base_path
23
+ custom = ENV['CHROME_FOR_TESTING_PATH']
24
+ Pathname.new(custom || Rails.root.join('.chrome-for-testing', CHROME_VERSION))
25
+ end
35
26
 
36
- def determine_platform
27
+ def self.platform
37
28
  case RUBY_PLATFORM
38
29
  when /darwin/
39
30
  `uname -m`.strip == 'arm64' ? 'mac-arm64' : 'mac-x64'
@@ -44,16 +35,26 @@ module SeleniumChromeHelper
44
35
  end
45
36
  end
46
37
 
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
38
+ def self.chrome_path
39
+ path = if platform.start_with?('mac')
40
+ base_path.join("chrome-#{platform}", 'Google Chrome for Testing.app', 'Contents',
41
+ 'MacOS', 'Google Chrome for Testing')
42
+ else
43
+ base_path.join("chrome-#{platform}", 'chrome')
44
+ end
45
+ warn "⚠️ Chrome binary not found at #{path}. Did you run `rake chrome:install`?" unless File.exist?(path)
46
+
47
+ path.to_s
48
+ end
49
+
50
+ def self.driver_path
51
+ path = base_path.join("chromedriver-#{platform}", 'chromedriver')
52
+ warn "⚠️ Chromedriver not found at #{path}. Did you run `rake chrome:install`?" unless File.exist?(path)
53
+
54
+ path.to_s
54
55
  end
55
56
 
56
- def configure_browser_options(chrome_path, headless: true)
57
+ def self.configure_browser_options(headless: true)
57
58
  options = Selenium::WebDriver::Chrome::Options.new
58
59
  options.binary = chrome_path
59
60
  options.add_argument('--headless=new') if headless
@@ -62,5 +63,13 @@ module SeleniumChromeHelper
62
63
  options.add_argument('--window-size=1400,1400')
63
64
  options
64
65
  end
66
+
67
+ def self.register_driver(name, headless:)
68
+ Capybara.register_driver name do |app|
69
+ Selenium::WebDriver::Chrome::Service.driver_path = driver_path
70
+ options = configure_browser_options(headless: headless)
71
+ Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
72
+ end
73
+ end
65
74
  end
66
75
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SeleniumChromeHelper
4
- VERSION = '0.1.1'
4
+ VERSION = '0.1.4'
5
5
  end
@@ -1,6 +1,4 @@
1
- require "selenium_chrome_helper/version"
1
+ # frozen_string_literal: true
2
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
3
+ require 'selenium_chrome_helper/version'
4
+ require 'selenium_chrome_helper/railtie' if defined?(Rails::Railtie)
@@ -3,7 +3,7 @@
3
3
  require 'fileutils'
4
4
  require 'open-uri'
5
5
  require 'json'
6
- require 'zip' unless defined?(Zip)
6
+ require 'zip'
7
7
 
8
8
  # rubocop:disable Metrics/BlockLength, Security/Open
9
9
  namespace :chrome do
@@ -11,7 +11,7 @@ namespace :chrome do
11
11
  task :install, [:version] do |_, args|
12
12
  version = args[:version] || '134.0.6998.165'
13
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)
14
+ install_base = File.expand_path(".chrome-for-testing/#{version}", Dir.pwd)
15
15
 
16
16
  platform = case RUBY_PLATFORM
17
17
  when /darwin/
@@ -19,35 +19,35 @@ namespace :chrome do
19
19
  when /linux/
20
20
  'linux64'
21
21
  else
22
- raise "Unsupported platform: \#{RUBY_PLATFORM}"
22
+ raise "Unsupported platform: #{RUBY_PLATFORM}"
23
23
  end
24
24
 
25
- puts "➡️ Platform: \#{platform}"
26
- puts "📡 Fetching metadata for Chrome version \#{version}..."
25
+ puts "➡️ Platform: #{platform}"
26
+ puts "📡 Fetching metadata for Chrome version #{version}..."
27
27
 
28
28
  json = URI.open(api_url).read
29
29
  data = JSON.parse(json)
30
30
 
31
31
  version_info = data['versions'].find { |v| v['version'] == version }
32
32
 
33
- abort "❌ Version \#{version} not found in known-good list." unless version_info
33
+ abort "❌ Version #{version} not found in known-good list." unless version_info
34
34
 
35
35
  chrome_url = version_info.dig('downloads', 'chrome')&.find { |d| d['platform'] == platform }&.dig('url')
36
36
  driver_url = version_info.dig('downloads', 'chromedriver')&.find { |d| d['platform'] == platform }&.dig('url')
37
37
 
38
38
  unless chrome_url && driver_url
39
- abort "❌ No matching downloads found for platform \#{platform} and version \#{version}"
39
+ abort "❌ No matching downloads found for platform #{platform} and version #{version}"
40
40
  end
41
41
 
42
- puts "⬇️ Chrome URL: \#{chrome_url}"
43
- puts "⬇️ Chromedriver URL: \#{driver_url}"
42
+ puts "⬇️ Chrome URL: #{chrome_url}"
43
+ puts "⬇️ Chromedriver URL: #{driver_url}"
44
44
 
45
45
  FileUtils.mkdir_p(install_base)
46
46
 
47
47
  download_and_unzip(chrome_url, File.join(install_base, 'chrome.zip'), install_base)
48
48
  download_and_unzip(driver_url, File.join(install_base, 'chromedriver.zip'), install_base)
49
49
 
50
- puts "✅ Chrome and Chromedriver installed to \#{install_base}"
50
+ puts "✅ Chrome and Chromedriver installed to #{install_base}"
51
51
  end
52
52
 
53
53
  def download_and_unzip(url, zip_path, extract_to)
@@ -66,6 +66,8 @@ namespace :chrome do
66
66
  def unzip(zip_path, extract_to)
67
67
  Zip::File.open(zip_path) do |zip_file|
68
68
  zip_file.each do |entry|
69
+ next if entry.symlink? # silently skip symlinks
70
+
69
71
  entry_path = File.join(extract_to, entry.name)
70
72
  FileUtils.mkdir_p(File.dirname(entry_path))
71
73
  zip_file.extract(entry, entry_path) unless File.exist?(entry_path)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: selenium_chrome_helper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gonzalo Robaina
@@ -25,19 +25,19 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: zip
28
+ name: rubyzip
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: '2.0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: '2.0'
41
41
  description: Adds a rake task (`chrome:install`) to easily fetch a pinned version
42
42
  of Chrome and Chromedriver locally or in CI.
43
43
  email: