This page explains how to test websites or pages protected by Basic Authentication.
To execute a test on a page that has Basic Authentication configured, you need to pass the authentication credentials (username and password) to the Scenario. There are several ways to configure these credentials.
Method 1: Include Credentials in the URL
The easiest and most common method is to include the authentication credentials directly within the Scenario's URL. This method can be used for the Scenario's start URL or in a "Navigate" step.
Steps
Edit or create a new Scenario.
Specify the URL of the page that requires authentication in the following format:
https://<username>:<password>@<domain>/<path>
Replace
<username>
and<password>
with your actual credentials.
Example
For instance, if the username is taro
, the password is hunter2
, and the URL you want to access is https://website.example.com/some/page
, you would set the URL as follows:
https://taro:hunter2@website.example.com/some/page
Method 2: Use a "Playwright Code Step" (Deprecated)
You can also bypass Basic Authentication by using a "Playwright Code Step".
This method is mainly useful in special cases, such as:
When navigating to a page that requires Basic Authentication by clicking a link or button, rather than by directly visiting a URL.
When you need to test multiple sites with different authentication credentials within a single Scenario.
Steps
Add a "Playwright Code Step" immediately before the action that triggers the Basic Authentication prompt (e.g., a "Visit URL" step).
Paste the following code into the input field and edit the credentials.
await context.setHTTPCredentials({ username: "<username>", password: "<password>" });
Replace
<username>
and<password>
with your actual credentials.
Note: The use of
setHTTPCredentials
has been deprecated by Playwright. Unless you have a specific reason, we recommend using Method 1 or Method 3.
Method 3: Configure in Test Plan Advanced Options
You can also set Basic Authentication credentials for an entire Test Plan at once. Using this method, the same credentials will be applied to all Scenarios included in that Test Plan.
Steps
Open the target Test Plan.
Click on "Advanced Options".
Copy the following JSON code and paste it into the input field.
{ "httpCredentials": { "username": "<username>", "password": "<password>" } }
Replace
<username>
and<password>
with your actual credentials.
Click the "Done" button to save the settings.
For more details, please also refer to the official Playwright documentation: Playwright - testOptions.httpCredentials
If you have any questions, please feel free to contact support.