Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@gtd

This comment has been minimized.

Copy link

@gtd gtd commented Jun 12, 2014

Very nice, thanks for sharing.

@mscottford

This comment has been minimized.

Copy link

@mscottford mscottford commented Jun 18, 2014

Very handy! Thanks for sharing, and thanks for taking such a deep and public dive into trying to fix this issue.

@shime

This comment has been minimized.

Copy link

@shime shime commented Jul 11, 2014

Thanks!

@LeeXGreen

This comment has been minimized.

Copy link

@LeeXGreen LeeXGreen commented Sep 8, 2014

Thanks!

I was able to re-use the restart_phantomjs method to fix the same issue when running jasmine specs over HTTP.

@vaneyckt

This comment has been minimized.

Copy link

@vaneyckt vaneyckt commented Feb 27, 2015

This is amazing. It's like a hidden gem of source code.

@rdavila

This comment has been minimized.

Copy link

@rdavila rdavila commented Dec 28, 2015

It is not working for me with RSpec 3.3.0, I got the following error when running ex.run

     Failure/Error: build
     NoMethodError:
       undefined method `fetch_or_store' for nil:NilClass
@jimryan

This comment has been minimized.

Copy link

@jimryan jimryan commented Dec 31, 2015

@rdavila Change self.instance_variable_set('@__memoized', nil) to __init_memoized

Thanks for this @afn, these timeouts were killing me, and this is definitely helping!

@iRet

This comment has been minimized.

Copy link

@iRet iRet commented Jan 27, 2016

Thanks guys!

@anupamkakade

This comment has been minimized.

Copy link

@anupamkakade anupamkakade commented Feb 12, 2016

I pasted the above code in support directoryThe above code didnt work. Still I can see Timed out waiting for response to {"name":"visit","args":["http://ankakade-portal.dev.bluecloud.ibm.com:3000/server_requests/new"]}. It's possible that this happened because something took a very long time (for example a page load was slow). If so, setting the Poltergeist :timeout option to a higher value will help (see the docs for details). If increasing the timeout does not help, this is probably a bug in Poltergeist - please report it to the issue tracker. (Capybara::Poltergeist::TimeoutError)

I have set the CAPYBARA_TIMEOUT_RETRIES = 3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment