Search code examples
ruby-on-railsseleniumrspecacceptance-testingcapybara

Can I run multiple RSpec/Selenium tests in order without resetting browser state?


So I just started using Steak, which in turn uses Capybara, which in turn uses Selenium.

So I've heard it's good RSpec practice to split testing into lots of little it-clauses, each testing a small piece of functionality. But then it makes a test run take a lot longer, because the same steps get repeated for each test.

Say I'm testing some ajax functionality. I want to test that:

  • a form appears when I click a certain link
  • error messages appear if I submit invalid input to that form
  • a confirmation message appears if I submit valid input, etc.

But if I have each of those things broken into its own test case, then Selenium has to start from scratch: it has to load the same page three times, click the link to make the form appear three times, etc. It strikes me as wasteful.

What I'd like to be able to do is specify a sequence of tests that preserve browser state, so each test continues where the previous one left off. Is that possible?


Solution

  • It is possible. One way is to override Capybara's visit method and keep track of the last url and only revisit when it changes.

    Something along the lines of:

    module Capybara
      alias original_visit visit
      def visit(url)
        original_visit url if url != $last_url
        $last_url = url
      end
    end
    

    but you should only do that if you really know what you are doing...