1

Can someone help me handle "HTTP basic

authentication prompt" on Chrome?

I can't access the authentication for our staging environment by the regular

method (http://user:password@domain.com).

Do you have any idea how can handle it?
. . .

Technical info:

Programming lang: JavaScript

Framework: Jasmine JS

Browser: Google Chrome (latest)

Platforms: Selenium Webdriver, Node.JS

Runner: Protractor


My code is below:

//First Demo for E2E Automation testing by Protractor for AngularJS

describe ('Do this before every test case', function() {
beforeEach(function() {
    browser.get("http://user:password@domain.com');
    expect(browser.getCurrentUrl()).toEqual("http://wwww.meet2know.com/");
});

var login = require ('../login.js');
});
ravenblizzard
  • 326
  • 3
  • 12
Idan E
  • 1,299
  • 4
  • 17
  • 42
  • Can this help you? http://stackoverflow.com/questions/16900091/how-to-pass-through-window-asking-for-basic-auth-credentials-that-appears-when-c – Johnny Apr 07 '15 at 12:06
  • Unfortunately no.. I need some guide for "Google chrome".. :/ – Idan E Apr 07 '15 at 12:33
  • Possible duplicate of [How to handle basic authentication with protractor?](https://stackoverflow.com/questions/27234785/how-to-handle-basic-authentication-with-protractor) – Jan Molak Mar 19 '19 at 14:15

5 Answers5

1

There does not seem to be an easy way around:

How to handle basic authentication with protractor?.

I'd suggest disabling the basic authentication for the IP address from which you are running your tests. It makes sense as the basic auth login does not need to be tested and won't be present in live environment.

Community
  • 1
  • 1
Michal M.
  • 1,072
  • 1
  • 11
  • 14
1

It's possible to handle this issue using Proxy (BrowserMob GitHub, with guide), as suggested in the other answer.

Alternatively, you can try to handle the alert box and insert you credential information (It's Java code example, you should have the something similar in JS):

WebDriverWait wait = new WebDriverWait(driver, 3);
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
alert.authenticateUsing(new UserAndPassword("USER", "PASS"));
Johnny
  • 14,397
  • 15
  • 77
  • 118
  • Thanks, but JS doesn't support in a native browser actions as I heard.. In FireFox it possible handle this issue by installation of autoAuth add-on. But, I need a similar solution for Google Chrome as well.. – Idan E Apr 07 '15 at 13:18
  • It's Selenium libraries, you should have something similar in JS Binding of Selenium as well (IMO). – Johnny Apr 07 '15 at 14:05
1

So I ran into this issue. The problem is that your code and the expect that you do will never resolve as the same thing.

browser.get("http://user:password@domain.com');

expect(browser.getCurrentUrl()).toEqual("http://wwww.meet2know.com/");

for some reason (I'm lost as to why) when the browser.get() HTTP Auth resolves, it no longer thinks the URL is valid.

To handle this, the easiest solution is to set up your before step, to hit the Auth URL then use a Given I navigate to the "/" url step definition at the beginning of each scenario. The below code is gherkin, cucumber and webdriver but you'll be able to see how it works in your case.

Gherkin .feature file

  Scenario: Open the homepage
    Given I navigate to the url "/"

Cucumber/Webdriver Step Definition

  // Set the two url's as constants to use in the steps below 
  const authUrl = 'http://user:password@domain.com'
  const url = 'http://wwww.meet2know.com'

  this.Before(() => {
    // resolve the HTTP authentication
    browser.url(authUrl)
  })

  this.Given(/^I am on the url "([^"]*)"$/, function (path) {
    // use the non auth URL to test normally
    browser.url(url + path)
    expect(browser.getUrl()).toEqual(url + path)
  })

The advantage here is if you suddenly need to drop the requirement for the HTTP auth you can just comment out the before step. Additionally, it is a good idea to define the path first in your scenario or feature as it gives you context as to the location of the feature or scenario.

xam
  • 408
  • 5
  • 11
0

BrowserMob Proxy might help. I have had luck with it in the past when dealing with basic authentication.

aholt
  • 2,829
  • 2
  • 10
  • 13
0

If you're reading this in 2021 and you don't want to pull in BrowserMob or other software just to do something as trivial as basic authentication, I gotchu. I spent some time looking into this, and while it's true that Chrome no longer allows username:password@domain in the URL, it's also true that it does still support it. The implementation is simple and requires zero additional software. All you need to do is add a Chrome command line switch to disable phishing detection:

const caps = Capabilities.chrome();

caps.set('goog:chromeOptions', {
  args: ['--disable-client-side-phishing-detection']
});

const driver = await new Builder().forBrowser('chrome').withCapabilities(caps).build();

Now you can add your credentials in the URL:

await driver.get('https://username:password@domain.com/');

This is precisely how Katalon - and perhaps other testing frameworks - achieves it.

bosscube
  • 189
  • 10