1

I want to restart my Phantomjs Driver when it hangs. I have an example of restarting poltergiest as below:

if driver.is_a?(Capybara::Poltergeist::Driver)
driver.restart
end

I am not using Poltergeist driver. I am using phantomjs driver object like this below:

Capybara::Selenium::Driver.new(app, :browser => :phantomjs)

I need to know how to restart phantomjs driver. I need something like this:

Capybara.page.driver.restart. 
SaeeK
  • 217
  • 1
  • 4
  • 12

2 Answers2

1

page.driver.quit should quit the selenium::webdriver instance which should then automatically start a new one the next time the session is used.

Thomas Walpole
  • 48,548
  • 5
  • 64
  • 78
  • Tom, It is quitting the driver and open again but immediately i got below error and quit the driver again. # – SaeeK Feb 04 '16 at 18:36
  • Also, if running less that selenium 2.50 - try updating – Thomas Walpole Feb 04 '16 at 18:48
  • Happening same thing using selenium 2.50 . Looks like when it quits the driver object, it lost the session. Just emailed you in details.... – SaeeK Feb 04 '16 at 18:53
  • ok -- so it's failing attempting to connect to phantomjs maybe? did you upgrade to the latest selenium? if so, then we really need a stacktrace to have any clue whats going on – Thomas Walpole Feb 04 '16 at 19:02
  • This is the logs i do have : **Connection refused - connect(2) for "127.0.0.1" port 9524 (Errno::ECONNREFUSED)** **/opt/chefdk/embedded/lib/ruby/2.1.0/net/http.rb:879:in `initialize' /opt/chefdk/embedded/lib/ruby/2.1.0/net/http.rb:879:in `open'** I have updated to gem 'selenium-webdriver', '=2.50.0' – SaeeK Feb 04 '16 at 19:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/102631/discussion-between-saeek-and-tom-walpole). – SaeeK Feb 04 '16 at 19:11
0

You can use below mentioned method to restart phantomjs

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
  module_function :restart_phantomjs
end

Hope this helps :)

Sarabjit Singh
  • 786
  • 4
  • 4
  • Thanks for reply. As i mentioned, i am not using Poltergiest driver. So this will not help. "Tom Walpole" already provided me a good solution which worked partially. Still encountering other issue though. – SaeeK Feb 05 '16 at 14:17