3

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 git@github.com: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.

  • if we go to certain webpages, we will be getting some authentication popup which asks username and password to proceed further. Is the question regarding to handle this scenario? – santhosh kumar Jun 13 '17 at 12:43
  • If the question is regarding the handling of http authorization, these line will suffice. driver.get("http://UserName:Password@Example.com"); – santhosh kumar Jun 14 '17 at 04:53
  • @santhosh kumar yes, unfortunately Chrome does not accept this way of authentication any more. (They changed it recently). –  Jun 14 '17 at 13:19

3 Answers3

2

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']
);
Florent B.
  • 41,537
  • 7
  • 86
  • 101
  • It worked fine, thank you. I have updated my GitHub repository as it might serve as a useful example. –  Jun 14 '17 at 13:35
-1

Suggestion#1:

If your internet connection has some proxy settings, that restricts to load the website, then you can receive the error 111: Net::ERR_TUNNEL_CONNECTION_FAILED in chrome. You have to disable proxy settings to resolve this error.

The full process is given step by step with pictures in this tutorial: http://troubleshooter.xyz/wiki/fix-err_tunnel_connection_failed-error-google-chrome/

Suggestion#2:

Yakub K also would suggest you to disable Internet Explorer proxy setting and check.

Follow the steps to disable proxy:

a. Open Internet Explorer.

b. Click the Tools button, and then click Internet Options.

c. Click the Connections tab, and then click LAN settings.

d. Uncheck the box which says Use a Proxy Server for your LAN.

e. Click on OK.

Resource Link: https://answers.microsoft.com/en-us/windows/forum/windows_7-ecoms/error-errtunnelconnectionfailed-when-trying-to/8af1b8ed-86fe-461f-a629-9a6f23ce857e

SkyWalker
  • 28,384
  • 14
  • 74
  • 132
-1
    System.setProperty("webdriver.chrome.driver", "C:\\Users\\singh\\OneDrive\\Documents\\Selenium\\chromedriver.exe");
    Robot rb = new Robot();
    ChromeOptions options = new ChromeOptions();
    options.addExtensions(new File("C:\\Users\\singh\\OneDrive\\Documents\\Selenium\\Extension\\extension.crx"));
    WebDriver driver = new ChromeDriver(options);
    rb.keyPress(KeyEvent.VK_F5);
    driver.navigate().to("https://stage.creativememories.com");

After a few days of rigorous research this is the definite answer that worked for me. Hope this helps you.

Steps:

  1. Create extension as defined here : manifest.json and background.js have to be zipped and then packed through //chrome:extensions.
  2. Then link your extension as given in the code.
  3. Use the refresh key using Robot package(as my url wasn't loading after the extension was triggered).
  4. Voila! you have a workaround. Cheers.