A proxy in Playwright routes browser requests through a chosen network connection. It is useful for checking your own application from another network, reproducing connectivity problems or giving separate tests different routes. This guide uses JavaScript and Chromium, with credentials supplied through environment variables.
Decide what the test needs
A mobile proxy is useful when the carrier network is part of the test. For example, an application may load from an office connection but fail for visitors using a particular mobile route. Running the same browser scenario through that route helps isolate the network variable.
The proxy does not make desktop Chromium a physical phone. Viewport, language and browser emulation are separate settings. A routine button or form test with no network requirement may work perfectly well without a proxy.
Both mobile and datacenter HTTP proxies use the same connection mechanism in this example. Choose the route for the test you actually need to run. Our mobile and datacenter proxy comparison explains the difference between the connection types.
Prepare a small project
Use a Node.js version supported by Playwright and obtain the proxy host, port and authentication details. The server value includes its scheme, such as http://proxy.example:3128; credentials are separate fields.
In a new example directory, run:
npm init -y
npm install playwright
npx playwright install chromium
Keep package-lock.json with the project. On another machine, use npm ci followed by npx playwright install chromium to install the corresponding dependencies and browser. See the Playwright installation guide for supported environments.
Connect and inspect the browser’s exit IP
Save this as check-proxy.mjs. It connects through an authenticated HTTP proxy and navigates to an HTTPS IP-checking service. An optional CHECK_URL can point to your own endpoint returning a JSON object with an ip field.
import { chromium } from 'playwright';
import { isIP } from 'node:net';
const server = process.env.PROXY_SERVER;
if (!server) throw new Error('Set PROXY_SERVER');
const browser = await chromium.launch({
proxy: {
server,
username: process.env.PROXY_USERNAME,
password: process.env.PROXY_PASSWORD,
},
});
try {
const context = await browser.newContext();
const page = await context.newPage();
const response = await page.goto(
process.env.CHECK_URL ?? 'https://api.ipify.org?format=json',
{ waitUntil: 'domcontentloaded', timeout: 30000 },
);
if (!response?.ok()) throw new Error('IP check failed');
const result = await response.json();
if (!isIP(result.ip)) throw new Error('Invalid IP response');
console.log('Browser exit IP:', result.ip);
await context.close();
} finally {
await browser.close();
}
Set PROXY_SERVER, PROXY_USERNAME and PROXY_PASSWORD using your shell or your CI secret store. Omit the credential variables if the proxy does not require authentication. Keep production credentials out of source files and version control. Run:
node check-proxy.mjs
The script prints Browser exit IP: and the address observed by the endpoint. Compare it with the address observed from your normal connection. An identical result is not automatically a failure if your machine already uses the same upstream proxy.
This checks the route of that request. Follow it with the actual application scenario: load the relevant page, wait for the required element and assert the result. An IP lookup alone cannot prove that a checkout, login or file upload succeeds. Geographic databases can also disagree about the city associated with an address.
Keep proxy credentials separate from website credentials
Proxy authentication belongs inside the proxy object. Playwright’s httpCredentials setting handles HTTP authentication on the destination website. Putting the proxy password there does not configure proxy authentication.
The example uses an HTTP proxy. SOCKS support is browser-dependent when authentication is involved, so verify your exact browser and protocol combination before switching schemes. The official proxy documentation describes the connection settings and HTTP proxy credentials.
Choose browser-wide or context-level configuration
The complete example sets proxy in chromium.launch(), which suits a single route. If different scenarios need different connections, set it when creating a context:
const context = await browser.newContext({
proxy: {
server: process.env.PROXY_SERVER,
username: process.env.PROXY_USERNAME,
password: process.env.PROXY_PASSWORD,
},
});
This fragment assumes an already launched browser. Create and close a context for each scenario. Contexts separate browser sessions, while the provider determines the external IP. Two contexts using the same gateway and port do not necessarily receive different exit addresses. See Browser.newContext for the API options.
Rotate between scenarios
Keep the route stable while a test completes a login or a multi-step operation. Changing the IP during an upload or form submission can interrupt a connection; the exact result depends on the application and proxy.
If the next test requires a fresh IP, finish the current work, request rotation using the provider’s supported mechanism and check the new address before continuing. Creating a context does not itself tell the provider to rotate. Our mobile proxy rotation guide covers the network side.
After a connection failure, check whether a state-changing operation succeeded before retrying it. A server may have accepted a request before the connection closed. Use a controlled test environment and test records for operations that change data.
Troubleshoot one request at a time
| Symptom | What to check |
|---|---|
407 Proxy Authentication Required |
Proxy credentials and the provider’s authentication method. |
| Proxy connection fails | Host, port, scheme, channel availability and IP allowlists. |
| IP check succeeds but the application fails | The application’s response status and failed request; connectivity to one endpoint does not guarantee access to another. |
| Certificate error | Certificate trust and channel configuration rather than disabling TLS verification as a general fix. |
| Failure immediately after rotation | Move rotation outside the scenario and verify connectivity before starting again. |
Start with one browser, one context and one request. Increase concurrency after checking connection stability and channel limits. For a public-data collection workflow, continue with proxies for web scraping.



