In any application that tracks users, I’m guessing that the vast majority of features will require that a user is logged in. This however can add a huge amount of overhead to your features at run time.
To speed things up, you can create a backdoor route to allow your features to bypass the login page and form submission, and simply hit a url to create the session.
First, define the route scoped by a condition – you don’t want this in production mode!
if %w( development test cucumber ).include?(Rails.env.to_s) map.backdoor '/backdoor', :controller => :user_sessions, :action => :backdoor end
Then define the controller action. Bear in mind we are using authlogic. Adjust this to suite your own requirements.
# for cucumber testing only def backdoor @user_session = UserSession.new(User.find_by_email(params[:email])) @user_session.save head 'ok' end
Finally, your cucumber step goes from something like this…
Given /^I authenticate successfully$/ do Given 'I am on the login page' And 'I fill in "Email" with "firstname.lastname@example.org"' And 'I fill in "Password" with "secret"' When 'I press "Login"' Then 'I should see "Welcome, Service Account (AD)"' end
…to something like this…
Given /^I am already logged in as a user$/ do visit "/email@example.com" end
We shaved nearly 2 minutes off our 12-minute build!
Thanks to this post for the original idea.