Technology and Software

Keep the browser open with Capybara and Selenium

The combination of Capybara and Selenium is great for testing complex web applications but it’s really annoying that the browser window closes when there is an error. All is left for debugging is the Ruby error message on the console but all the information about the HTML page and JavaScript code is lost, and the cause of the error might be there.

There are a few threads about this problem (just google the title of this post) and this is the code that worked for me, built on the suggestions found in a couple of them.

Capybara::Selenium::Driver.class_eval do
  def quit
    puts "Press RETURN to quit the browser"
    $stdin.gets
    @browser.quit
  rescue Errno::ECONNREFUSED
    # Browser must have already gone
  end
end

It’s a monkey patch of what I found in gems/capybara-1.1.2/lib/capybara/selenium/driver.rb and I put it in a file loaded by spec_helper.rb

I just added the puts and the gets lines to the original code.

There is a last annoyance left: the browser doesn’t stop at the page that triggered the error but it stops on a blank page. I have to click the back button to get to the page with the error, which might not always work. If anybody knows how to stop on the page with the error please leave a comment. Thanks!

Advertisements
Standard