Skip to content

Instantly share code, notes, and snippets.

@mytrile
Forked from afn/gist:c04ccfe71d648763b306
Created September 23, 2015 08:00
Show Gist options
  • Save mytrile/150c2d715c31d9562caf to your computer and use it in GitHub Desktop.
Save mytrile/150c2d715c31d9562caf to your computer and use it in GitHub Desktop.
Restart phantomjs when it hangs
# Borrowed from https://github.com/y310/rspec-retry/blob/master/lib/rspec/retry.rb
CAPYBARA_TIMEOUT_RETRIES = 3
RSpec.configure do |config|
config.around(:each, type: :feature) do |ex|
example = RSpec.current_example
CAPYBARA_TIMEOUT_RETRIES.times do |i|
example.instance_variable_set('@exception', nil)
self.instance_variable_set('@__memoized', nil) # clear let variables
ex.run
break unless example.exception.is_a?(Capybara::Poltergeist::TimeoutError)
puts("\nCapybara::Poltergeist::TimeoutError at #{example.location}\n Restarting phantomjs and retrying...")
restart_phantomjs
end
end
end
def restart_phantomjs
puts "-> Restarting phantomjs: iterating through capybara sessions..."
session_pool = Capybara.send('session_pool')
session_pool.each do |mode,session|
msg = " => #{mode} -- "
driver = session.driver
if driver.is_a?(Capybara::Poltergeist::Driver)
msg += "restarting"
driver.restart
else
msg += "not poltergeist: #{driver.class}"
end
puts msg
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment