Philip Bradley bio photo

Philip Bradley

Just some guy with glasses.

Email Twitter Facebook Google+ LinkedIn Github

Even though the stuff I make generally is test driven, there always comes a time when you need to click around your web app like a real person would.

In development, I often use a rake task to populate the database with sample data to get a sense of how things will look and feel with “real” data. But I really loathe needing to log in and navigate to various pages that I’m inspecting. Especially when I need to do it again and again.

They say the best programmers are the lazy ones, and if that’s true I have a bright future, because using Capybara I find a pretty easy way to automate getting to the place I want to be in my web app.

It isn’t hard to get Capybara to navigate through a web path; the hard part is keeping Capybara from killing the browser when it finishes. But really all you have to do is override the “quit” method on the driver class, like this:

session = Capybara::Session.new(:selenium)
session.driver.class.class_eval { def quit; end }

I got this little tip from a strangely undervoted answer by Eliot Larson on Stackoverflow.

Now if you run a script that uses Capybara, it will fire up a browser, do what you tell it, and then exit, but the browser window stays open for you to take over.

I use the script below to log into one application as either a manager, a tutor, or a client, and I make some assumptions about the data in the app (usernames and passwords) but I’ve also used this approach to actually create a new account each time.

Here is the whole file, which I store in script/login, and use by typing % script/login tutor or % script/login manager etc.

#!/usr/bin/env ruby
require 'capybara'

role = ARGV[0]
exit unless %w(client manager tutor).include? role

session = Capybara::Session.new(:selenium)
session.driver.class.class_eval { def quit; end }
session.visit('http://127.0.0.1:3000')

case role
when 'tutor'
session.click_link "Sign in as a Tutor"
session.fill_in "Email", with: "tutor1@example.com"
session.fill_in "Password", with: "123123"
when 'manager'
session.click_link "Sign in as a Manager"
session.fill_in "Email", with: "manager1@example.com"
session.fill_in "Password", with: "123123"
when 'client'
session.click_link "Sign in as a Client"
session.fill_in "Email", with: "client1@example.com"
session.fill_in "Password", with: "123123"
end

session.click_button "Log in"