← Articles

Capybara webkit and HTTP basic auth

Sometimes the things that you assume will be simple and no problem at all will give you a challenge. Here I thought that doing HTTP basic auth would be easy with the Capybara webkit driver. It isn’t as easy as you might expect. I would have expected a authorize method of some sort but you have to actually set the header yourself. Since it took me some time to figure out I thought I’d just share this little snippet and safe someone else the trouble.

Capybara webkit and HTTP basic auth

1
2
3
4
basic = ActionController::HttpAuthentication::Basic
credentials = basic.encode_credentials('username', 'password')
page.driver.header('Authorization', credentials)
visit '/some/http-basic-auth-path'

That’s it. I’ll leave it up to you to include it in a helper or a before filter :-)

Suggestions, tips or flames? Please leave a comment. howto testing rails capybara snippet http basic auth tdd web development javascript. web application ruby code webkit

Thomas Cremers google+facebooktwitterstack overflow