User Agents

Web browsers and everyday web tech, explained

Handle HTTP Auth in Protractor with Headless Chrome

· 4 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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

  1. Open your Protractor configuration file (usually protractor.conf.js or conf.js) in a code editor.
  2. Locate the capabilities object within the configuration. If it doesn’t exist, create it under the main exports.
  3. Add or modify the chromeOptions section inside capabilities to include arguments that disable popup handling and enable silent credentials.
  4. Insert the following flags into the args array:
    "--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"
    
    These flags suppress various Chrome behaviors that could interfere with automated authentication.
  5. Add the --disable-features=VizDisplayCompositor" flag if you encounter rendering issues in headless mode.
  6. Set the headless option to new or true depending on your Chrome version. Use "--headless=new" for Chrome 112+.
  7. Ensure your test navigates to URLs with embedded credentials using the format: https://username:password@hostname/path. For example:
    await browser.get('https://admin:secret123@staging.example.com');
    This bypasses the authentication dialog entirely by sending credentials in the initial request.
  8. 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.
  9. In your test scripts, avoid hardcoded credentials. Instead, load them from environment variables:
    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);
    
    This keeps sensitive data out of version control.
  10. Run your Protractor tests using protractor protractor.conf.js and verify that the browser loads the protected page without hanging on an auth dialog.
  11. If tests fail, check ChromeDriver logs by enabling logging in the Protractor config:
    loggingPrefs: {
      browser: 'ALL',
      driver: 'ALL'
    }
    This helps identify if authentication headers are being rejected or redirected.
  12. 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 using encodeURIComponent() before embedding in the URL.
  • Using outdated Chrome flags: Flags like --headless without =new may 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 .env file for local development, but exclude it from version control using .gitignore.
  • Combine --disable-web-security with 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 --version and 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 -v to confirm the server accepts the credentials and returns a 200 status.
  • Enable verbose logging: Add SELENIUM_LOG_LEVEL=ALL and 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 using encodeURIComponent() before inserting into the URL to prevent parsing errors.