# 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