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:
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.