Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

https://github.com/RobinDev/Selenium-Chrome-HTTP-Private-Proxy 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.

Update

Checkout the Minimum Working Example.

git clone [email protected]:alexbiddle/selenium-chrome-http-basic-auth.git

Excerpt below

var config = {
    mode: "fixed_servers",
    rules: {
      singleProxy: {
        scheme: "https",
        host: "subdomain.example.com"
      },
      bypassList: ["foobar.com"]
    }
  };

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

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

chrome.webRequest.onAuthRequired.addListener(
        callbackFn,
        {urls: ["<all_urls>"]},
        ['blocking']
);

Loading it in Java using

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

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

I double-checked the docs at https://developer.chrome.com/extensions/proxy#type-ProxyServer in case there was a missing value of something, however when loading the test with the URL

https://subdomain.example.com

It fails with

ERR_TUNNEL_CONNECTION_FAILED

I'm using Chrome on Mac.


1 Answers

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

chrome.webRequest.onAuthRequired.addListener(
  function handler(details) {    
    if (username == null)
      return {cancel: true};

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

    return {authCredentials: authCredentials};
  },
  {urls: ["<all_urls>"]},
  ['blocking']
);
like image 56
Florent B. Avatar answered Sep 02 '26 05:09

Florent B.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!