REPL for Capybara Tests

When writing feature tests which uses capybara, I have to do some amount of trial and error until finally ends up with a "correct" capybara matcher.

It's not caused by the obscurity of the capybara's documentation (which their docs are great btw!), but it's more of the page it self may not be so obvious before we run and see the result, specially if you're working with pages containing complex custom elements.

Doing the trial and error process, requires me to:

  1. Write the test
  2. Run the test
  3. Review the outcome
  4. Back to 1st step to fix any errors found or to improve anything I found during the review

These are pretty acceptable steps to run the tests, if only the 2nd step isn't too time consuming, that even with spring, the time taken to load the test on middle-to-large project may not be acceptable.

I recently worked on a project which took around 30 to 40 seconds after I hit enter in the terminal until I'm able to see the Firefox (selenium) window opened and the page shown. The project already have spring loaded.
That is for one single test in one run. Say, if in average I have to improve the code 10 times for every single test until it's acceptable and I have to write 20 tests for certain features. This means the times I spent for waiting is around 100 to 133 minutes. Wow, this gets old fast.

pry to the rescue!

From pry's github page:

"Pry also aims to be more than an IRB replacement; it is an attempt to bring REPL driven programming to the Ruby language."

I'm not going to discuss about the details of pry in this post, but basically pry allow us to fire up REPL session from within ruby code.

The steps required are very simple:

  1. Add pry to the Gemfile.

      gem 'pry', group: :test
    
  2. Open your test file, and add binding.pry line to the scenario you want to fire up REPL session.

    scenario "I'm able to do what I want" do
      click_link "To This Page"
      binding.pry
      click_link "Logout"
    end
    
  3. Run your test (in my case it's rspec).

    bin/rspec spec/features/the_feature_spec.rb:6
    
  4. When the Firefox window opened, you'll be presented with a pry console in your terminal.

    From /Users/tanto/code/my_project/spec/features/the_feature_spec.rb @ line 8 :
        5: 
        6:   scenario "I'm able to do what I want" do
        7:     click_link "To This Page"
    =>  8:     binding.pry
        9:     click_link "Exit"
       10:
    [1] pry(#<RSpec::Core::ExampleGroup::Nested_1>)> _
    
  5. Notice the last line, there you'll be able to type ruby code, and in this case, capybara's related functions.

    [1] pry(...)> find('.something')
    *** Capybara::ElementNotFound Exception: Unable to find css ".something"
    [2] pry(...)> _
    
  6. If you type "action" related to the page, you can see that the page is changing. Live in the Firefox window.

    [2] pry(...)> click_link "To This Page"
    "ok"
    [3] pry(...)> _
    

This speeds up my workflow considerably. Now everytime I deal with complex page (usually the ones with custom elements), I can easily write capybara's code, see the instant outcome and fix it in one go, without having to wait for the test to fail and re-run it.

Hope this helps someone!
Let me know your thoughts by leaving your comments below.