-
-
Save futhr/83163548ce9c62991ece to your computer and use it in GitHub Desktop.
Restart and retry when Poltergeist fails randomly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Borrowed from https://github.com/y310/rspec-retry/blob/master/lib/rspec/retry.rb | |
# Drop this script into spec/support | |
# To test snippet just set timeout to 1 | |
RSpec.configure do |config| | |
Capybara.javascript_driver = :poltergeist | |
Capybara.register_driver(:poltergeist) do |app| | |
Capybara::Poltergeist::Driver.new app, | |
js_errors: true, # optional, i prefer to catch js stuff | |
timeout: 180, # ensure you tweak timeout first !! | |
phantomjs_logger: Puma::NullIO.new, # for puma | |
logger: nil, | |
phantomjs_options: | |
[ | |
'--load-images=no', | |
'--ignore-ssl-errors=yes' # only setting this can do the trick sometimes | |
] | |
end | |
config.around(:each, type: :feature) do |example| | |
current_example = RSpec.current_example | |
2.times do |index| | |
current_example.instance_variable_set('@exception', nil) | |
instance_variable_set('@__memoized', nil) # clear let variables | |
example.run | |
break unless current_example.exception.is_a?(Capybara::Poltergeist::TimeoutError) | |
restart_message_for current_example if index == 1 | |
restart_phantomjs | |
end | |
end | |
def restart_message_for(example) | |
msg = "\n#{example.exception} - restart and retry..\n" | |
msg += " # #{example.location}\n" | |
$stdout.puts msg.yellow | |
end | |
def restart_phantomjs | |
Capybara.send('session_pool').each do |_, session| | |
next unless session.driver.is_a?(Capybara::Poltergeist::Driver) | |
session.driver.restart | |
end | |
end | |
end |
Also, occasionally Poltergeist was throwing a different error type on disconnect. We're catching that as well, replacing:
break unless current_example.exception.is_a?(Capybara::Poltergeist::TimeoutError)
restart_message_for current_example if index == 1
restart_phantomjs
with:
# If Poltergeist state errors happens, restart
case current_example.exception
when Capybara::Poltergeist::StatusFailError, Capybara::Poltergeist::TimeoutError
restart_message_for current_example if index == 1
restart_phantomjs
end
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code has resolved several months of pain. THANK YOU!
Also, note that
instance_variable_set('@__memoized', nil)
should be replaced with__init_memoized
in rspec 3.3+