Skip to content

Instantly share code, notes, and snippets.

@futhr
Forked from afn/gist:c04ccfe71d648763b306
Last active April 21, 2016 19:14
Show Gist options
  • Save futhr/83163548ce9c62991ece to your computer and use it in GitHub Desktop.
Save futhr/83163548ce9c62991ece to your computer and use it in GitHub Desktop.
Restart and retry when Poltergeist fails randomly.
# 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
@KurtPreston
Copy link

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