Skip to content

Commit

Permalink
Drop support for Ruby 2.4 which went EOL last week
Browse files Browse the repository at this point in the history
  • Loading branch information
twalpole committed Apr 5, 2020
1 parent 5ef8cd9 commit f347909
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 118 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ require:

AllCops:
DisabledByDefault: false
TargetRubyVersion: 2.4
TargetRubyVersion: 2.5
Exclude:
- 'capybara.gemspec'

Expand Down
2 changes: 1 addition & 1 deletion apparition.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
6 changes: 2 additions & 4 deletions lib/capybara/apparition/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {})
Expand Down Expand Up @@ -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
Expand Down
10 changes: 3 additions & 7 deletions lib/capybara/apparition/driver/chrome_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
28 changes: 13 additions & 15 deletions lib/capybara/apparition/driver/launcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/capybara/apparition/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
16 changes: 7 additions & 9 deletions lib/capybara/apparition/node/drag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
146 changes: 66 additions & 80 deletions spec/integration/driver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f347909

Please sign in to comment.