HTTP basic auth for Capybara

后端 未结 6 991
伪装坚强ぢ
伪装坚强ぢ 2020-12-05 00:06

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 m

6条回答
  •  死守一世寂寞
    2020-12-05 00:47

    The default Capybara driver, rack-test, has a basic_authorize method (with alias authorize) for Basic HTTP Auth, and digest_authorize for Digest HTTP Auth, here you can find them: https://github.com/brynary/rack-test/blob/master/lib/rack/test.rb

    So you can do:

    page.driver.browser.authorize 'login', 'password'
    

    Or you can write a simple helper for Basic HTTP Auth:

    def basic_auth(user, password)
      encoded_login = ["#{user}:#{password}"].pack("m*")
      page.driver.header 'Authorization', "Basic #{encoded_login}"
    end
    

提交回复
热议问题