Handle HTTP Auth in Protractor with Headless Chrome
In this article
Automated browser testing with Protractor typically fails when encountering HTTP Basic Authentication prompts because headless Chrome does not display or allow interaction with the standard browser login dialog. To successfully navigate these pop-ups, you must bypass the dialog programmatically by embedding credentials directly in the URL or configuring Chrome options to suppress authentication prompts. This guide walks through reliable methods to handle HTTP authentication in Protractor tests when using headless Chrome, ensuring your end-to-end tests run smoothly without manual intervention.
What You'll Need
- Protractor installed and configured for your project
- ChromeDriver compatible with your Chrome version
- Access to the test environment protected by HTTP Basic Auth
- Valid username and password for the protected site
- Basic understanding of Protractor configuration (conf.js)
Step-by-Step
- Open your Protractor configuration file (usually
protractor.conf.jsorconf.js) in a code editor. - Locate the
capabilitiesobject within the configuration. If it doesn’t exist, create it under the main exports. - Add or modify the
chromeOptionssection insidecapabilitiesto include arguments that disable popup handling and enable silent credentials. - Insert the following flags into the
argsarray:
These flags suppress various Chrome behaviors that could interfere with automated authentication."--disable-web-security", "--allow-running-insecure-content", "--disable-popup-blocking", "--no-sandbox", "--disable-gpu", "--disable-translate", "--disable-extensions", "--disable-plugins-discovery", "--use-fake-ui-for-media-stream", "--use-fake-device-for-media-stream", "--disable-infobars" - Add the
--disable-features=VizDisplayCompositor"flag if you encounter rendering issues in headless mode. - Set the
headlessoption tonewortruedepending on your Chrome version. Use"--headless=new"for Chrome 112+. - Ensure your test navigates to URLs with embedded credentials using the format:
https://username:password@hostname/path. For example:
This bypasses the authentication dialog entirely by sending credentials in the initial request.await browser.get('https://admin:secret123@staging.example.com'); - If your environment forbids URL-embedded credentials (e.g., due to security policies), use an intercepting proxy or service worker to inject authentication headers. However, this is more complex and not supported natively in Protractor.
- In your test scripts, avoid hardcoded credentials. Instead, load them from environment variables:
This keeps sensitive data out of version control.const username = process.env.AUTH_USER; const password = process.env.AUTH_PASS; const url = `https://${username}:${password}@staging.example.com/app`; await browser.get(url); - Run your Protractor tests using
protractor protractor.conf.jsand verify that the browser loads the protected page without hanging on an auth dialog. - If tests fail, check ChromeDriver logs by enabling logging in the Protractor config:
This helps identify if authentication headers are being rejected or redirected.loggingPrefs: { browser: 'ALL', driver: 'ALL' } - Validate that your test environment accepts URL-based credentials. Some servers or proxies strip credentials from URLs for security reasons. If so, coordinate with your DevOps team to allow them in staging environments only.
Common Mistakes
- Forgetting to escape special characters in passwords: If your password contains
@,:, or#, encode it usingencodeURIComponent()before embedding in the URL. - Using outdated Chrome flags: Flags like
--headlesswithout=newmay not work in Chrome 112+. Always verify compatibility. - Hardcoding credentials in test files: This exposes secrets in logs and repositories. Always use environment variables.
- Assuming the auth dialog can be clicked: In headless mode, UI interactions with the browser's native auth popup are impossible. Relying on element clicks or browser alerts will fail.
- Not testing in headless mode locally: Always run and debug tests in headless mode before CI/CD deployment to catch issues early.
Tips
- Use a staging environment with predictable credentials to avoid flaky tests.
- Set up a shared
.envfile for local development, but exclude it from version control using.gitignore. - Combine
--disable-web-securitywith caution—it should never be used in production contexts. - Use
browser.waitForAngular()appropriately after navigation to ensure the page is fully loaded. - Log the constructed URL during debugging (without printing credentials) to confirm formatting.
Troubleshooting
If the test hangs or fails to load the page:
- Check ChromeDriver version: Mismatched versions can cause silent failures. Run
chromedriver --versionand ensure it matches your Chrome browser. - Verify URL encoding: Use
console.log(encodeURI(url))to inspect the final URL. A malformed URL due to special characters is a common cause. - Test the URL manually: Paste the full credential-included URL into a regular Chrome window to see if it works outside automation.
- Inspect server response: Use browser developer tools or
curl -vto confirm the server accepts the credentials and returns a 200 status. - Enable verbose logging: Add
SELENIUM_LOG_LEVEL=ALLand check Protractor output for authentication-related errors. - Check for redirects: Some servers redirect after auth; ensure your test waits for the final page.
Frequently Asked Questions
Can I use browser.wait() to handle the auth popup?
No. Native HTTP auth dialogs are not part of the DOM and cannot be interacted with via Protractor. You must bypass them programmatically.Why doesn’t sending keys to username/password fields work?
Because the HTTP auth dialog is a browser-level UI, not an HTML element. Protractor cannot access it.Is it safe to put credentials in the URL?
Only in controlled environments. Never use in production. Use environment-specific configs and avoid logging the URL.Does this work with Chrome in non-headless mode?
Yes, but the same method (URL embedding) is required. Headless mode just makes UI interaction impossible, so it’s more critical there.Can I use HTTP headers instead of URL credentials?
Not directly in Protractor. You’d need a proxy like BrowserMob, which adds complexity and isn’t natively supported.What if my password has special characters?
Encode the username and password usingencodeURIComponent() before inserting into the URL to prevent parsing errors.