Last Updated: January 28, 2019
·
1.808K
· paulelliott

Speeding up cucumber

You can bypass the page loads and form filling by implementing a sign in backdoor with a little bit of code.

This assumes your sign in is governed by a SessionsController and you can sign in by passing a User instance into a sign in method. Adjust to match your authentication system.

#features/support/sign_in_backdoor.rb

class SessionsController
  def backdoor
    sign_in(User.find_by_email(params[:email]))
    redirect_to :dashboard
  end
end

MyRailsApp::Application.routes.tap do |routes|
  routes.disable_clear_and_finalize = true
  routes.draw do
    match 'backdoor', to: 'sessions#backdoor'
  end
end

Now you can add a helper to your cucumber environment and use the sign_in function anywhere in your steps.

#features/step_definitions/session_steps.rb

module SessionStepMethods
  def sign_in(user)
    visit "/backdoor?email=#{user.email}"
  end
end
World(SessionStepMethods)

Don't worry, it is totally safe. Since the backdoor is in features/support, it gets monkey patched in when running cucumber but not in real server instances.

3 Responses
Add your response

Cool tip. I couldn't find documentation on #disable_clear_and_finalize though, what does it do?

It's also not clear from your writeup that the first snippet of code should be in 'features/support' until later on. You may want to include a file name in a comment to rectify that.

Excellent tip though. I'll definitely be using this on a project I've started.

over 1 year ago ·

#disable_clear_and_finalize basically unlocks the route set so you can add to it. Normally you can't declare a route outside of the routes.rb.

over 1 year ago ·

That's awesome! Helpful for integration tests with capybara.

over 1 year ago ·