Rspec Tips & Tricks

Debugging Flaky Specs with Capybara and Chrome in RSpec

Rspec and testing using capybara:

  • Running my tests and seeing it run live can be super helpful for several reasons but especially when I am encountering a flaky spec. If I am running through the code and the logic makes sense or I have a binding.pry in the code and it is coming back as expected but STILL failing, I will run the test again within a chrome browser. This allows me to visually see the test running and verify that nothing else has gotten in the way of a passing spec. It has happened to me before where the test is running too fast or there is an element over the top of the element I am testing and the spec is failing but shouldn’t be. To do this, I run:

    CHROME=true bin/rspec ./spec/whatever_you_spec_file_structure_is

    Under the hood in the spec/rails_helper.rb there is something that looks like this:

    config.before(:each, js: true) do

    if ENV['CHROME']

    driven_by :selenium_chrome

    else

    driven_by :selenium_chrome_headless

    end

    end

    This finds if there is an environment variable set and if so, it uses the selenium_chrome driver for capybara. Using the CHROME=true sets the environment variable and tells rails that we want the test to run in a browser. Chrome should open up and you will see your test running live. Pretty handy!

    Additionally, if you want to pause and see what the browser looks like, you can add a binding.irb or binding.pry depending on your setup. Once your test hits the binding, it will pause and prevent the test from finishing. Then you can see what things look like on your own time. Once you are done looking in the browser, you can exit the pry (ctrl+c) and let the test finish. But just a heads up, sometimes this can actually prevent the flaky test from failing due to things slowing down (if that is the issue). But, no matter how you spin it, this is helpful information to have.

  • If running in the browser seems overkill for you, these next two tricks I have been taught and learned to use, might be a better option. One of these tricks is : save_and_open_screenshot. This nifty trick can capture a snapshot of your application’s state at any point during a test, providing invaluable visual feedback. All you need to do is add that method to your test and bam! you will get a screenshot of the test at that moment in time. It should open the screenshot automatically, but if not, you can find the screenshot in this file structure: tmp/screenshots. Here is an example of how to use it in code:
    it 'displays the homepage in a screenshot' do
    visit '/'
    save_and_open_screenshot
    expect(page).to have_content('Welcome')
    end

  • The next and last useful tip I have is the use of save_and_open_page. Within your RSpec tests, you can use save_and_open_page to save the current HTML of the page and open it in your default browser.
    it 'displays the homepage in the browser' do
    visit '/'
    expect(page).to have_content('Welcome')
    save_and_open_page
    end

###

If you’re looking for a team to help you discover the right thing to build and help you build it, get in touch.

Published on September 24, 2025