Leon's notes

Jul 16

Test basic auth with Cucumber

The senario

Scenario: admin dashboard
  Given I perform HTTP authentication as "admin/secret"
    And I am on "/admin"
  Then I should see "Dashboard"

The step

When /^I perform HTTP authentication as "([^\"]*)\/([^\"]*)"$/ do |name, password|
  if page.driver.respond_to?(:basic_auth)
    page.driver.basic_auth(name, password)
  elsif page.driver.respond_to?(:basic_authorize)
    page.driver.basic_authorize(name, password)
  elsif page.driver.respond_to?(:browser) && page.driver.browser.respond_to?(:basic_authorize)
    page.driver.browser.basic_authorize(name, password)
  else
    raise "I don't know how to log in!"
  end
end

from the discussion here:
http://stackoverflow.com/questions/4329985/http-basic-auth-for-capybara

When using selenium driver, according to their wiki

How do I use Selenium to login to sites that require HTTP basic authentication (where the browser makes a modal dialog asking for credentials)?

Use a username and password in the URL, as described in RFC 1738:

open  http://myusername:myuserpassword@myexample.com/blah/blah/blah

Note that on Internet Explorer this won’t work, since Microsoft has disabled usernames/passwords in URLs in IE. However, you can add that functionality back in by modifying your registry, as described in the linked KB article. Set an “iexplore.exe” DWORD to 0 in HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_HTTP_USERNAME_PASSWORD_DISABLE.

If you don’t want to modify the registry yourself, you can always just use Selenium Remote Control, which automatically sets that that registry key for you as of version 0.9.2.

Also the discussion on github:

iast opened this issue January 02, 2011

HTTP Basic Authentication for Selenium driver

No milestone

No one is assigned

Hi, I’m using Cucumber+Capybara+Selenium to test the app that relies on HTTP Basic Authentication. There is a way to use this kind of auth by calling page.driver.basic_authorize(user, pass) method.

But when I mark the Scenario with @javascript tag (enabling Selenium driver) I get an error:
undefined method `basic_authorize’ for #Capybara::Driver::Selenium:0x000000041ea880 (NoMethodError)

 jnicklas commented

January 09, 2011

You’re accessing a private API and calling through to a method which is specific to the rack-test driver. Selenium doesn’t support basic authentication other than through the URL afaik, which is why we don’t have an API for it in Capybara in the first place.

 iast commented

January 12, 2011

It’s a pity. It would be nice to have a cross-driver way of using basic auth.

But, anyway, I solved the problem in my case by using URL to identify current user and session to persist current user between requests (when Selenium is active only).

Thanks for the reply.

The only way would be something like “http://myusername:myuserpassword@myexample.com/blah/blah/blah”