diff --git a/.rubocop.yml b/.rubocop.yml index b265aa7..300bc07 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -4,7 +4,7 @@ require: AllCops: DisabledByDefault: false - TargetRubyVersion: 2.4 + TargetRubyVersion: 2.5 Exclude: - 'capybara.gemspec' diff --git a/apparition.gemspec b/apparition.gemspec index a61f2d9..52b55ed 100644 --- a/apparition.gemspec +++ b/apparition.gemspec @@ -19,7 +19,7 @@ Gem::Specification.new do |s| s.require_paths = ['lib'] s.files = Dir.glob('{lib}/**/*') + %w[LICENSE README.md] - s.required_ruby_version = '>= 2.4.0' + s.required_ruby_version = '>= 2.5.0' s.add_runtime_dependency 'capybara', '~> 3.13', '< 4' s.add_runtime_dependency 'websocket-driver', '>=0.6.5' diff --git a/lib/capybara/apparition/driver.rb b/lib/capybara/apparition/driver.rb index 9db660c..7051f2e 100644 --- a/lib/capybara/apparition/driver.rb +++ b/lib/capybara/apparition/driver.rb @@ -221,9 +221,7 @@ def add_header(name, value, options = {}) alias_method :header, :add_header def response_headers - browser.response_headers.each_with_object({}) do |(key, value), hsh| - hsh[key.split('-').map(&:capitalize).join('-')] = value - end + browser.response_headers.transform_keys { |key| key.split('-').map(&:capitalize).join('-') } end def set_cookie(name, value = nil, options = {}) @@ -446,7 +444,7 @@ def process_browser_options(options) end end when Hash - options.each_with_object({}) { |(option, val), hsh| hsh[option.to_s.tr('_', '-')] = val } + options.transform_keys { |option| option.to_s.tr('_', '-') } else raise ArgumentError, 'browser_options must be an Array or a Hash' end diff --git a/lib/capybara/apparition/driver/chrome_client.rb b/lib/capybara/apparition/driver/chrome_client.rb index 81fda18..6854640 100644 --- a/lib/capybara/apparition/driver/chrome_client.rb +++ b/lib/capybara/apparition/driver/chrome_client.rb @@ -206,9 +206,7 @@ def process_handlers(handlers, event) event_name = event['method'] handlers[event_name].each do |handler| puts "Calling handler for #{event_name}" if ENV['DEBUG'] == 'V' - # TODO: Update this to use transform_keys when we dump Ruby 2.4 - # handler.call(event['params'].transform_keys(&method(:snake_sym))) - handler.call(**event['params'].each_with_object({}) { |(k, v), hash| hash[snake_sym(k)] = v }) + handler.call(**event['params'].transform_keys(&method(:snake_sym))) end end @@ -224,10 +222,8 @@ def start_threads @async_response_handler.abort_on_exception = true @listener = Thread.new do - begin - listen - rescue EOFError # rubocop:disable Lint/SuppressedException - end + listen + rescue EOFError # rubocop:disable Lint/SuppressedException end # @listener.abort_on_exception = true end diff --git a/lib/capybara/apparition/driver/launcher.rb b/lib/capybara/apparition/driver/launcher.rb index 3628324..bdae4cd 100644 --- a/lib/capybara/apparition/driver/launcher.rb +++ b/lib/capybara/apparition/driver/launcher.rb @@ -13,24 +13,22 @@ def self.start(*args, **options) def self.process_killer(pid) proc do - begin - sleep 1 - if Capybara::Apparition.windows? + sleep 1 + if Capybara::Apparition.windows? + ::Process.kill('KILL', pid) + else + ::Process.kill('USR1', pid) + timer = Capybara::Helpers.timer(expire_in: KILL_TIMEOUT) + while ::Process.wait(pid, ::Process::WNOHANG).nil? + sleep 0.05 + next unless timer.expired? + ::Process.kill('KILL', pid) - else - ::Process.kill('USR1', pid) - timer = Capybara::Helpers.timer(expire_in: KILL_TIMEOUT) - while ::Process.wait(pid, ::Process::WNOHANG).nil? - sleep 0.05 - next unless timer.expired? - - ::Process.kill('KILL', pid) - ::Process.wait(pid) - break - end + ::Process.wait(pid) + break end - rescue Errno::ESRCH, Errno::ECHILD # rubocop:disable Lint/SuppressedException end + rescue Errno::ESRCH, Errno::ECHILD # rubocop:disable Lint/SuppressedException end end diff --git a/lib/capybara/apparition/node.rb b/lib/capybara/apparition/node.rb index fcc7edb..5baabd3 100644 --- a/lib/capybara/apparition/node.rb +++ b/lib/capybara/apparition/node.rb @@ -496,7 +496,7 @@ def set_text(value, clear: nil, delay: 0, rapid: nil, **_unused) evaluate_on CLEAR_ELEMENT_JS else focus - if ((rapid && (value.length >= 6)) || ((value.length > 30) && rapid != false)) + if (rapid && (value.length >= 6)) || ((value.length > 30) && rapid != false) _send_keys(*keys_to_send(value[0..2], clear), delay: delay) driver.execute_script <<~JS, self, value[0...-3] arguments[0].value = arguments[1] diff --git a/lib/capybara/apparition/node/drag.rb b/lib/capybara/apparition/node/drag.rb index e0ac4c4..1f1a56e 100644 --- a/lib/capybara/apparition/node/drag.rb +++ b/lib/capybara/apparition/node/drag.rb @@ -17,15 +17,13 @@ def drag_to(other, delay: 0.1, html5: nil, drop_modifiers: []) m.up(**other.visible_center) else @page.keyboard.with_keys(drop_modifiers) do - begin - other.scroll_if_needed - sleep delay - m.move_to(**other.visible_center) - sleep delay - ensure - m.up - sleep delay - end + other.scroll_if_needed + sleep delay + m.move_to(**other.visible_center) + sleep delay + ensure + m.up + sleep delay end end end diff --git a/spec/integration/driver_spec.rb b/spec/integration/driver_spec.rb index 0e88dfb..ede4dcc 100644 --- a/spec/integration/driver_spec.rb +++ b/spec/integration/driver_spec.rb @@ -73,22 +73,20 @@ def session_url(path) end it 'allows custom maximization size' do - begin - Capybara.register_driver :apparition_with_custom_screen_size do |app| - Capybara::Apparition::Driver.new( - app, - logger: TestSessions.logger, - screen_size: [800, 600] - ) - end - session = Capybara::Session.new(:apparition_with_custom_screen_size, TestApp) - session.visit(session_url('/')) - session.current_window.resize_to(400, 400) - session.current_window.maximize - expect(session.current_window.size).to eq([800, 600]) - ensure - session&.driver&.quit + Capybara.register_driver :apparition_with_custom_screen_size do |app| + Capybara::Apparition::Driver.new( + app, + logger: TestSessions.logger, + screen_size: [800, 600] + ) end + session = Capybara::Session.new(:apparition_with_custom_screen_size, TestApp) + session.visit(session_url('/')) + session.current_window.resize_to(400, 400) + session.current_window.maximize + expect(session.current_window.size).to eq([800, 600]) + ensure + session&.driver&.quit end end @@ -103,22 +101,20 @@ def session_url(path) end it 'supports specifying viewport size with an option' do - begin - Capybara.register_driver :apparition_with_custom_window_size do |app| - Capybara::Apparition::Driver.new( - app, - logger: TestSessions.logger, - window_size: [800, 600] - ) - end - driver = Capybara::Session.new(:apparition_with_custom_window_size, TestApp).driver - driver.visit(session_url('/')) - expect( - driver.evaluate_script('[window.innerWidth, window.innerHeight]') - ).to eq([800, 600]) - ensure - driver&.quit + Capybara.register_driver :apparition_with_custom_window_size do |app| + Capybara::Apparition::Driver.new( + app, + logger: TestSessions.logger, + window_size: [800, 600] + ) end + driver = Capybara::Session.new(:apparition_with_custom_window_size, TestApp).driver + driver.visit(session_url('/')) + expect( + driver.evaluate_script('[window.innerWidth, window.innerHeight]') + ).to eq([800, 600]) + ensure + driver&.quit end shared_examples 'render screen' do @@ -581,17 +577,15 @@ def create_screenshot(file, *args) end it 'errors when extension is unavailable' do - begin - @failing_driver = Capybara::Apparition::Driver.new( - @session.app, - logger: TestSessions.logger, - inspector: !ENV['DEBUG'].nil?, - extensions: %W[#{File.expand_path '../support/non_existent.js', __dir__}] - ) - expect { @failing_driver.visit '/' }.to raise_error(Capybara::Apparition::BrowserError, /Unable to load extension: .*non_existent\.js/) - ensure - @failing_driver.quit - end + @failing_driver = Capybara::Apparition::Driver.new( + @session.app, + logger: TestSessions.logger, + inspector: !ENV['DEBUG'].nil?, + extensions: %W[#{File.expand_path '../support/non_existent.js', __dir__}] + ) + expect { @failing_driver.visit '/' }.to raise_error(Capybara::Apparition::BrowserError, /Unable to load extension: .*non_existent\.js/) + ensure + @failing_driver.quit end end @@ -626,28 +620,24 @@ def create_screenshot(file, *args) end it 'does not propagate a Javascript error to ruby if error raising disabled' do - begin - driver = Capybara::Apparition::Driver.new(@session.app, js_errors: false, logger: TestSessions.logger) - driver.visit session_url('/apparition/js_error') - driver.execute_script 'setTimeout(function() { omg }, 0)' - sleep 0.1 - expect(driver.body).to include('hello') - ensure - driver&.quit - end + driver = Capybara::Apparition::Driver.new(@session.app, js_errors: false, logger: TestSessions.logger) + driver.visit session_url('/apparition/js_error') + driver.execute_script 'setTimeout(function() { omg }, 0)' + sleep 0.1 + expect(driver.body).to include('hello') + ensure + driver&.quit end it 'does not propagate a Javascript error to ruby if error raising disabled and client restarted' do - begin - driver = Capybara::Apparition::Driver.new(@session.app, js_errors: false, logger: TestSessions.logger) - driver.restart - driver.visit session_url('/apparition/js_error') - driver.execute_script 'setTimeout(function() { omg }, 0)' - sleep 0.1 - expect(driver.body).to include('hello') - ensure - driver&.quit - end + driver = Capybara::Apparition::Driver.new(@session.app, js_errors: false, logger: TestSessions.logger) + driver.restart + driver.visit session_url('/apparition/js_error') + driver.execute_script 'setTimeout(function() { omg }, 0)' + sleep 0.1 + expect(driver.body).to include('hello') + ensure + driver&.quit end end @@ -949,33 +939,29 @@ def create_screenshot(file, *args) end it 'allows the driver to have a fixed port' do - begin - driver = Capybara::Apparition::Driver.new(@driver.app, port: 12345) - driver.visit session_url('/') + driver = Capybara::Apparition::Driver.new(@driver.app, port: 12345) + driver.visit session_url('/') - expect { TCPServer.new('127.0.0.1', 12345) }.to raise_error(Errno::EADDRINUSE) - ensure - driver.quit - end + expect { TCPServer.new('127.0.0.1', 12345) }.to raise_error(Errno::EADDRINUSE) + ensure + driver.quit end it 'allows the driver to have a custom host' do - begin - # Use custom host "pointing" to localhost, specified by APPARITION_TEST_HOST env var. - # Use /etc/hosts or iptables for this: https://superuser.com/questions/516208/how-to-change-ip-address-to-point-to-localhost - # A custom host and corresponding env var for Travis is specified in .travis.yml - # If var is unspecified, skip test - host = ENV['APPARITION_TEST_HOST'] + # Use custom host "pointing" to localhost, specified by APPARITION_TEST_HOST env var. + # Use /etc/hosts or iptables for this: https://superuser.com/questions/516208/how-to-change-ip-address-to-point-to-localhost + # A custom host and corresponding env var for Travis is specified in .travis.yml + # If var is unspecified, skip test + host = ENV['APPARITION_TEST_HOST'] - skip 'APPARITION_TEST_HOST not set' if host.nil? + skip 'APPARITION_TEST_HOST not set' if host.nil? - driver = Capybara::Apparition::Driver.new(@driver.app, host: host, port: 12_345) - driver.visit session_url('/') + driver = Capybara::Apparition::Driver.new(@driver.app, host: host, port: 12_345) + driver.visit session_url('/') - expect { TCPServer.new(host, 12_345) }.to raise_error(Errno::EADDRINUSE) - ensure - driver&.quit - end + expect { TCPServer.new(host, 12_345) }.to raise_error(Errno::EADDRINUSE) + ensure + driver&.quit end it 'lists the open windows' do