Alex Biddle Alex Biddle -4 years ago 279
Java Question

Create HTTP Basic auth Chrome extension for Selenium (MWE available)

I am trying to run a Selenium test with Google Chrome. I'd like this to login using HTTP basic authentication. This is not implemented in Selenium, so the advice is to load an extension. I'm using code from and the answer to "How to override basic authentication in selenium2 with Java using chrome driver?"

I have tried to adapt it to my needs.


Checkout the Minimum Working Example.

git clone

Excerpt below

var config = {
mode: "fixed_servers",
rules: {
singleProxy: {
scheme: "https",
host: ""
bypassList: [""]

chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});

function callbackFn(details) {
return {
authCredentials: {
username: "example",
password: "abc123"

{urls: ["<all_urls>"]},

Loading it in Java using

ChromeOptions chromeOptions = new ChromeOptions();
File proxyPath = new ClassPathResource("").getFile();

DesiredCapabilities capability =;
capability.setCapability(CAPABILITY, chromeOptions);
webDriver = new ChromeDriver(capability);

I double-checked the docs at in case there was a missing value of something, however when loading the test with the URL

It fails with


I'm using Chrome on Mac.

Answer Source

The error is likely due to the proxy defined by your extension.

You should build the extension without the proxy and define the proxy in the capabilities if you need one different from the system.

To create the extension, simply zip the files below with your credentials defined in username and password:

manifest.json :

  "manifest_version": 2,
  "name": "Authentication for ...",
  "version": "1.0.0",
  "permissions": ["<all_urls>", "webRequest", "webRequestBlocking"],
  "background": {
    "scripts": ["background.js"]

background.js :

var username = "my-username";
var password = "my-password";

  function handler(details) {    
    if (username == null)
      return {cancel: true};

    var authCredentials = {username:username, password: username};
    username = password = null;

    return {authCredentials: authCredentials};
  {urls: ["<all_urls>"]},
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download