Idan E
Idan E

Reputation: 1319

How to handle HTTP Basic authentication on Chrome by Protractor?

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:[email protected]).

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:[email protected]');
    expect(browser.getCurrentUrl()).toEqual("http://wwww.meet2know.com/");
});

var login = require ('../login.js');
});

Upvotes: 1

Views: 6345

Answers (5)

bosscube
bosscube

Reputation: 189

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:[email protected]/');

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

Upvotes: 0

xam
xam

Reputation: 408

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:[email protected]');

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:[email protected]'
  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.

Upvotes: 1

Johnny
Johnny

Reputation: 15413

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"));

Upvotes: 1

aholt
aholt

Reputation: 2971

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

Upvotes: 0

Michal M.
Michal M.

Reputation: 1065

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.

Upvotes: 1

Related Questions