msmith1114
msmith1114

Reputation: 3229

Capybara/Poltergeist HTTP Authentication

So im trying to run some automation testing on a Rails site that requires what seems to be a basic HTTP Authentication:

enter image description here

Im using localhost:3000 as my host.

Capybara.app_host = "http://localhost:3000"
Capybara.server_host = "localhost"
Capybara.server_port = "3000"

Im running this on firefox right now, and I tried doing http://username:password@localhost:3000 but it usually warns about "phishing sites" but I proceed anyways, but it doesn't work and the http popup still appears. (I've heard a lot of browsers disabled this ability).

I know Selenium has ways to get around it, but what about Poltergeist? or anything built into Capybara.

I saw a previous question mentioned here: HTTP basic auth for Capybara

but seeing as how the above url doesn't work this didn't seem to work either.

edit: Current Poltergeist driver setup:

options = {
       :timeout => 45,
       :phantomjs_options => ["--proxy-auth="]
    }
    Capybara.register_driver :poltergeist do |app|
      Capybara::Poltergeist::Driver.new(app, options)
    end
    Capybara.default_driver = :selenium
    Capybara.javascript_driver = :poltergeist

Upvotes: 0

Views: 772

Answers (2)

graysonwright
graysonwright

Reputation: 514

Tossing another solution up here, for those working with Selenium + Firefox:

When you visit a page that requires HTTP basic auth, Firefox will show you a dialog box asking for username and password.

enter image description here

Selenium doesn't give you many options for interacting with the dialog box, but it has an accept_alert method that takes optional keystrokes.

To enter text into both fields at once, you can use Selenium's tab keycode:

tab = Selenium::WebDriver::Keys::KEYS[:tab]
SESSION.accept_alert(with: username + tab + password)

This is the only solution I could get working - I'm using Selenium's remote driver, which doesn't have support for HTTP basic auth built-in.

If everything else fails, give this a shot.

Upvotes: 1

Thomas Walpole
Thomas Walpole

Reputation: 49870

You can try

page.driver.basic_authorize(username, password)

before visiting the page in Poltergeist. That will set the username and password in phantomjs and also the 'Authorization' header

Upvotes: 2

Related Questions