I'm writing some RSpec tests for my Rails 3 application and trying to switch from Webrat to Capybara. So far so good but the application uses HTTP basic auth to authorize my admin user, any idea how I can test that with Capybara?
Here is my current Webrat step:
it 'should authenticate for admin' do
basic_auth('user', 'secret')
visit '/admin'
response.status.should eql 200
response.status.should_not eql 401
end
How do I do this with Capybara? Thanks!
The default Capybara driver, rack-test, has a
basic_authorize
method (with aliasauthorize
) for Basic HTTP Auth, anddigest_authorize
for Digest HTTP Auth, here you can find them: https://github.com/brynary/rack-test/blob/master/lib/rack/test.rbSo you can do:
Or you can write a simple helper for Basic HTTP Auth:
Man, none of these solutions worked for me.
Pistos' solution came close and worked for feature specs with
js: true
but failed when headless.This below solution works for me for both headless and
js: true
specs.spec/support/when_authenticated.rb
Then, in your spec:
I got it to work using
page.driver.basic_authorize(name, password)
insteadUpdate:
At the moment, after a Capybara upgrade, I'm using this pile of workarounds:
This has changed in recent versions of cucumber-rails (I am using 1.0.2).
cucumber-rails uses the Rack/Test driver by default, so if you have not changed that, the following instructions will work.
Create features/step_definitions/authorize.rb:
Now you can use this in your features:
None of the
page.driver.*
solutions worked for me. I'm using Poltergeist, not Selenium, so that might have something to do with it. Here's what did work:Then, in your spec:
I had to do this horrible hack to get it work worth headless and with javascript