For AI agents and LLMs: a machine-readable index is available at llms.txt. A plain-Markdown version of any documentation page is available by appending .md to its URL.
Skip to main content

HTTP Basic Authentication


HTTP basic authentication represents a challenge and response mechanism to help the server make a request authentication from a client, passing the User ID and password in the URL as the standard HTTP "Authorization" header.

For example: https://username:password@www.example.com/example-page.php

Browser Support For HTTP Basic Authentication​


HTTP authentication is incompatible among the majority of the web browser. It is supported by only Google Chrome and Mozilla Firefox.

BROWSERSUPPORT FOR BASIC HTTP AUTHENTICATION
Google ChromeSupported
Mozilla FireFoxAlthough supported, Mozilla Firefox may throw a prompt confirmation. Mozilla Firefox performs a check to evaluate whether basic HTTP authentication is mandatory to access the website. In cases, where it isn’t necessary, Firefox will throw a similar warning: "You are about to log in to the site "www.example.com" with the username "username", but the website does not require authentication. This may be an attempt to trick you." For more information, refer to the MDN document.
SafariUnsupported
EdgeUnsupported
Internet ExplorerUnsupported. Internet Explorer stopped handling user names and passwords in URLs with security update 832894, and the retired Internet Explorer 11 desktop app is out of support. For more information, refer to the Microsoft Learn article.

Test Basic Auth Protected Pages in Selenium and Playwright​


On TestMu AI, you can reach a page behind HTTP basic authentication in three ways: put the credentials in the URL, send an Authorization header, or use the credentials option of your framework. The examples below use the public demo page https://the-internet.herokuapp.com/basic_auth, whose user name and password are both admin.

To build the header value, join the user name and password with a colon, Base64-encode the result and prefix it with Basic . The Java and Playwright examples below build the header in code from admin:admin. The free Basic Auth Header Generator builds this header from any user name and password.

Selenium: Credentials in the URL​

Open the page with the user name and password in the URL. This works in Google Chrome and Mozilla Firefox, the two browsers the table above lists as supported.

BasicAuthUrlTest.java
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.net.URL;
import java.util.HashMap;

public class BasicAuthUrlTest {
public static void main(String[] args) throws Exception {
ChromeOptions browserOptions = new ChromeOptions();
browserOptions.setPlatformName("Windows 11");
browserOptions.setBrowserVersion("latest");

HashMap<String, Object> ltOptions = new HashMap<>();
ltOptions.put("username", System.getenv("LT_USERNAME"));
ltOptions.put("accessKey", System.getenv("LT_ACCESS_KEY"));
ltOptions.put("project", "Basic Auth Test");
browserOptions.setCapability("LT:Options", ltOptions);

RemoteWebDriver driver = new RemoteWebDriver(
new URL("https://hub.lambdatest.com/wd/hub"), browserOptions);
driver.get("https://admin:admin@the-internet.herokuapp.com/basic_auth");
System.out.println(driver.getTitle());
driver.quit();
}
}

Selenium: Authorization Header​

To keep the credentials out of the URL, send the Authorization header with the customHeaders capability, then open the page without credentials. The capability is described in custom HTTP headers in Selenium.

String token = Base64.getEncoder().encodeToString("admin:admin".getBytes(StandardCharsets.UTF_8));

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("customHeaders", new HashMap<String, String>() {{
put("Authorization", "Basic " + token);
}});

// After the session starts:
driver.get("https://the-internet.herokuapp.com/basic_auth");

Playwright: httpCredentials​

Playwright answers the authentication challenge itself when you create the browser context with httpCredentials. Set origin so the credentials go only to that site.

basic-auth.js
const { chromium } = require('playwright')

const capabilities = {
'browserName': 'Chrome',
'browserVersion': 'latest',
'LT:Options': {
'platform': 'Windows 10',
'build': 'Playwright Basic Auth',
'name': 'Basic Auth Test',
'user': process.env.LT_USERNAME,
'accessKey': process.env.LT_ACCESS_KEY
}
}

;(async () => {
const browser = await chromium.connect({
wsEndpoint: `wss://cdp.lambdatest.com/playwright?capabilities=${encodeURIComponent(JSON.stringify(capabilities))}`
})
const context = await browser.newContext({
httpCredentials: {
username: 'admin',
password: 'admin',
origin: 'https://the-internet.herokuapp.com'
}
})
const page = await context.newPage()
await page.goto('https://the-internet.herokuapp.com/basic_auth')
console.log(await page.title())
await browser.close()
})()

To keep the credentials out of the session logs, add setHTTPCredentials to the lambdaMaskCommands capability, listed in Playwright test environment setup.

Playwright: Authorization Header​

You can also send the header on every request of the context with extraHTTPHeaders. Unlike httpCredentials, this sends the header to every host the page calls.

const context = await browser.newContext({
extraHTTPHeaders: {
Authorization: 'Basic ' + Buffer.from('admin:admin').toString('base64')
}
})

Safari on iOS Real Devices​

Safari on iOS real devices needs a different route: a lambda hook for Selenium and the customHeaders capability for Playwright. Both are covered in Basic Authentication for Safari Web Automation.

Help and Support

Related Articles