Skip to content

Instantly share code, notes, and snippets.

@florentbr
Last active December 5, 2023 08:49
  • Star 22 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save florentbr/25246cd9337cebc07e2bbb0b9bf0de46 to your computer and use it in GitHub Desktop.
Chrome extension to automatically set the credentials.
To build the extension, update the username/password and zip `background.js` and `manifest.json` in a single archive.
var username = "my-username";
var password = "my-password";
var retry = 3;
chrome.webRequest.onAuthRequired.addListener(
function handler(details) {
if (--retry < 0)
return {cancel: true};
return {authCredentials: {username: username, password: password}};
},
{urls: ["<all_urls>"]},
['blocking']
);
{
"manifest_version": 2,
"name": "Authentication for ...",
"version": "1.0.0",
"permissions": ["<all_urls>", "webRequest", "webRequestBlocking"],
"background": {
"scripts": ["background.js"]
}
}
@dannyfinks
Copy link

Thank you, this is really helpful!

@sumaiyakidwai
Copy link

Hi, how can I customize it to provide different username and password for different specs?

@SridharRamachandran
Copy link

When we execute the test scripts suddenly windows authentication popup is opened which disables the background . In this case how protractor will recognize that there is a popup that needs to be handled with credentials

@skouny
Copy link

skouny commented Dec 13, 2018

Thank you!

@mgrtomasjurik
Copy link

Wonderfull solution, thank you!

@asamuday
Copy link

could you please provide solution how to add this code to selenium.

@rbhattarai
Copy link

Thanks for the solution. I've used Chrome Extension to handle authentication popup and working fine when selenium test is executed in 'normal' mode. When running Chrome in 'headless' mode with Chrome Extension enabled, it throws an exception. Later I came to know that it's Known Issue and ChromeOptions doesn't support chromeExtension in headless:

I need help on finding out solution to run chrome in headless mode but should take care of login authentication without using chrome extension. Appreciate your help/input.

@syoungfooty
Copy link

syoungfooty commented Dec 31, 2019

An existing project I am working on has this solution working fine in JavaScript but I am now migrating that project over to Java - can you provide some more details on how to get this work (OSX)

especially as AddListener has now been deprecated:
chrome.webRequest.onAuthRequired.addListener(

@anserzhang
Copy link

Thank you, this extension works perfectly.

@abid71
Copy link

abid71 commented Feb 19, 2021

Is it possible if I can use the same solution for chrome android?

@sonxiaopeng
Copy link

it is amazing

@SonNH70
Copy link

SonNH70 commented Dec 5, 2023

Thanks @florentbr , how can we handle if we have more than 1 url need to authenticate with different user name and password?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment