Today I learned

Pausing Capybara tests

Update (2019): This article was written in 2015. Consider avoiding Poltergeist and PhantomJS mentioned in this article; they have since been deprecated.

Want to use the Web Inspector in Capybara/Selenium tests? The first thing you'll probably try is to use pry-byebug to pause your tests. You'll probably find that this doesn't work as intended.

A bad example.rb
scenario 'visiting the home page' do
  visit root_path
  binding.pry # ✗
end

Using binding.pry will halt everything, making your browser not load anything because Rails can't respond to the request.

A better alternative

A better alternative is to use $stdin.gets. This is what Poltergeist uses to pause execution. That method is not available in Capybara/Selenium though, so you'll need to add it in yourself:

$stderr.write 'Press enter to continue'
$stdin.gets

Using with other libraries

Using with RSpec

If you're using Capybara with Rspec, you can turn this into a helper. You can then just use pause in your tests.

spec/support/pause_helpers.rb
module PauseHelpers
  def pause
    $stderr.write 'Press enter to continue'
    $stdin.gets
  end
end
spec/rails_helper.rb
RSpec.configure do
  config.include PauseHelpers, type: :feature
end

Using Poltergeist

When using Poltergeist (for PhantomJS support), just use its Remote Debugging feature with the inspector: true flag.

You have just read Pausing Capybara tests, written on November 17, 2015. This is Today I Learned, a collection of random tidbits I've learned through my day-to-day web development work. I'm Rico Sta. Cruz, @rstacruz on GitHub (and Twitter!).

← More articles