Selenium integration testing: optimize login
by ewout
Integration tests for highly dynamic web applications are currently bound to happen in the browser. There exist in-memory solutions that execute javascript, but none of them seem to have a fully accurate DOM implementation at the moment. Testing in the browser is slow and every optimization is welcome.
A logged in user is a prerequisite for many integration tests. Performing this step in the browser requires going to the login page, filling out the form, submitting and waiting for the initial page. Testing the login procedure is of course crucial, but it should not be tested a thousand times.
When using the cookie session store in rails, a logged in user will have a signed cookie containing his user id. On every page load, this cookie is sent back to the web application, where the signature is verified and the user is assumed to be logged in. Integration tests can be optimized by storing the contents of that cookie, and putting it back in the browser when a logged in user is required.
We implemented this optimization in our cucumber integration tests which use selenium. The code is below, hope it helps.
Given /^I am authenticated$/ do u = Factory(:user, :id => 1, :login => "uname", :password => "pass") c_name = Rails.configuration.action_controller.session[:key] if cookie = Thread.current[:selenium_cookie] selenium.create_cookie("#{c_name}=#{cookie}", :path => '/') else visit "/" selenium.wait_for_page 5 fill_in("login", :with => "uname") fill_in("password", :with => "pass") click_button("Inloggen") selenium.wait_for_page 5 response.should contain("Uitloggen") Thread.current[:selenium_cookie] = selenium.cookie(c_name) end end