Skip to content

Instantly share code, notes, and snippets.

@xenjke
Forked from richseviora/override_driver.rb
Last active January 19, 2017 17:46
Show Gist options
  • Save xenjke/ac554d55d6ad2f1deb0dde77eea9c60e to your computer and use it in GitHub Desktop.
Save xenjke/ac554d55d6ad2f1deb0dde77eea9c60e to your computer and use it in GitHub Desktop.
Capybara/Selenium Fix for Rubymine Debugging w/ Ruby 2.3+
# for Watir usage
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(loggingPrefs: {
browser: "ALL",
performance: "ALL"
})
http_client = OverrideDriver.new
@browser = Watir::Browser.new(:chrome, desired_capabilities: capabilities, http_client: http_client)
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment