Skip to content

Instantly share code, notes, and snippets.

@richseviora
Created December 15, 2016 22:26
Show Gist options
  • Save richseviora/24622a90b4a2993cf3db9075643b4b94 to your computer and use it in GitHub Desktop.
Save richseviora/24622a90b4a2993cf3db9075643b4b94 to your computer and use it in GitHub Desktop.
Capybara/Selenium Fix for Rubymine Debugging w/ Ruby 2.3+
# This OverrideDriver class is used to override the +open_timeout+ property Selenium's default HTTP driver.
# This is necessary to support debugging in Rubymine because when the process is stopped on the breakpoint,
# any new threads are not scheduled.
# This is an issue in Ruby 2.3+ because the Net::HTTP#initialize method now defaults the open_timeout property
# to 60 (seconds), as opposed to 0 previously. A non-zero/nil value executes the connection in a new thread
# (see +Net::HTTP#connect+).
# === Change Notes
#
# * Setting timeout directly does not work, the read_timeout period needs to remain at its current setting.
# A setting of 0 prevented pages from opening successfully.
#
# == Related
# https://youtrack.jetbrains.com/issue/RUBY-18301 - RM issue identifying root cause.
# https://github.com/ruby/ruby/blob/v2_3_0/NEWS#L297 - Change to Net::HTTP#open_timeout default.
#
class OverrideDriver < Selenium::WebDriver::Remote::Http::Default
def http
client = super
client.open_timeout = 0
client
end
end
# Unrelated content excluded for brevity.
# Enables Capybara debugging for Ruby 2.3 to fix
Capybara.register_driver :selenium_config do |app|
# @type [Selenium::WebDriver::Remote::Http::Default] client
client = OverrideDriver.new
# noinspection RubyArgCount
Capybara::Selenium::Driver.new(app, {http_client: client})
end
# Changing javascript_driver is required as the default driver is overriden when js: true.
Capybara.javascript_driver = :selenium_config
@xenjke
Copy link

xenjke commented Jan 19, 2017

Thank you for this!

For those using Watir:

browser = Watir::Browser.new(:chrome, http_client: OverrideDriver.new)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment