0

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
Community
  • 1
  • 1
msmith1114
  • 2,717
  • 3
  • 33
  • 84

2 Answers2

2

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

Thomas Walpole
  • 48,548
  • 5
  • 64
  • 78
  • I was hoping it would be as simple as then, unfortunately I get a "failed to reach server". This app runs really slow on local due to the DB...but as long as the username/password it set before it shouldn't matter right? I made sure to set my timeout high as well. (It works on selenium, when I manually enter the username/password while it's running) – msmith1114 Jul 28 '16 at 05:01
  • Actually, I think the problem is that it's running as a proxy site, so the normal http headers don't work. – msmith1114 Jul 28 '16 at 05:39
  • @msmith1114 With poltergeist there are page.driver.headers=/add_headers/add_header so you can set any headers you need - although "failed to reach server" doesn't really sound like an auth denied - what timeout are you setting? (Poltergeists timeout option is set in the driver registration) – Thomas Walpole Jul 28 '16 at 05:55
  • The timeout is set to 30 seconds (with Capybara.default_max_wait_time = 30), but I think whats happening is it's sitting on the HTTP auth without the password/username working...so it just eventually times out. I don't have the timeout specifically set in the poltergeist driver (but it's default of 30 "should" be enough i'd think). Running it as selenium (and letting it timeout) gives a timeout error of a different nature) – msmith1114 Jul 28 '16 at 06:03
  • Added an edit to show my current poltergeist driver setup to question. – msmith1114 Jul 28 '16 at 06:05
  • Welp, it looks like it was all due to timeout, I completely REMOVED the --proxy-auth=username:password settings and it's working, which almost doesn't make sense?....Does Poltergeist just skip proxy authentications usually or something? (I do have the Username/Password saved in firefox, but it didn't appear to stay saved when running as selenium). – msmith1114 Jul 28 '16 at 06:31
  • @msmith1114 Capybara.default_max_wait_time is the time finders/matchers will wait for elements to appear, the timeout in the poltergeist driver registration is a network communications timeout, so they different things. The --proxy-auth setting wouldn't have been used without a --proxy setting, so it probably wasn't doing anything anyway. I assume you still have the `basic_authorize` call in your code and thats whats working now. – Thomas Walpole Jul 28 '16 at 07:38
  • Weirdly I actually do NOT have the basic_authorize, so I imagine it's maybe remembering the saved username/password within firefox. – msmith1114 Jul 28 '16 at 16:07
1

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.

graysonwright
  • 514
  • 2
  • 8