How to Test a Proxy in Playwright

Give Playwright a direct browser context and a second context with your proxy settings. Open the same IP lookup in each context to verify the route used by the browser.

Laptop with two browser maps and different location markers beside a stack of servers

Set up Playwright and get the provider’s proxy address

Run the check on the machine that runs your browser tests. Your laptop and your CI runner can have different public IPs, network policies and proxy allowlists. In an existing Playwright project, use its installed browser and dependency. In a new Node.js project, install the library and its Chromium build.

npm install --save-dev playwright
npx playwright install chromium

Find the proxy host, port and protocol in your provider’s connection details. An HTTP proxy address looks like http://gateway.example:8080 even when the page you open uses HTTPS. Chromium can ask that HTTP proxy to open a tunnel to the HTTPS destination. For a provider that requires a username and password, supply them as Playwright proxy credentials. For an IP-authorized proxy, allowlist the address of the machine running the script in your provider’s settings. Playwright’s proxy configuration guide.

Navigate from a baseline context and a proxy context

Save the example as check-proxy.mjs. Set PROXY_SERVER in the script’s environment or replace the example address. Set PROXY_USERNAME and PROXY_PASSWORD through your shell or CI secret settings when the HTTP proxy needs them. Leave both unset for a proxy with no username/password challenge. Then run node check-proxy.mjs.

import { isIP } from 'node:net';
import { chromium } from 'playwright';

const probeUrl = 'https://testmyproxies.com/api/my-ip';
const timeout = 20_000;

async function checkIp(browser, label, options = {}) {
  const context = await browser.newContext(options);
  try {
    const page = await context.newPage();
    const response = await page.goto(probeUrl, { timeout, waitUntil: 'load' });
    if (!response || !response.ok()) {
      throw new Error(response ? 'HTTP ' + response.status() : 'Missing response');
    }

    const body = await response.json();
    const ip = body?.ip;
    if (typeof ip !== 'string' || !isIP(ip)) {
      throw new Error('The IP lookup did not return a valid address');
    }
    return ip;
  } catch (error) {
    throw new Error(label + ': ' + error.message);
  } finally {
    await context.close();
  }
}

async function main() {
  const proxy = {
    server: process.env.PROXY_SERVER || 'http://gateway.example:8080',
  };
  const username = process.env.PROXY_USERNAME;
  const password = process.env.PROXY_PASSWORD;

  if (username !== undefined || password !== undefined) {
    if (!username || password === undefined) {
      throw new Error('Set both PROXY_USERNAME and PROXY_PASSWORD');
    }
    proxy.username = username;
    proxy.password = password;
  }

  const browser = await chromium.launch({ timeout });
  try {
    const direct = await checkIp(browser, 'Baseline');
    const proxied = await checkIp(browser, 'Proxy', { proxy });
    console.log('Baseline IP:', direct);
    console.log('Proxied IP:', proxied);
    if (direct === proxied) {
      console.warn('Both requests used the same public IP. Check the expected exit.');
    }
  } finally {
    await browser.close();
  }
}

main().catch(error => {
  console.error(error.message);
  process.exitCode = 1;
});

The first context uses the browser’s default network route with no proxy supplied by the script. It can still reflect a VPN or a system-managed proxy. The second context applies the supplied proxy only to pages created in that context. The script checks the status and JSON from the navigation response because a page can load an HTTP error document successfully. Playwright navigation reference.

Read the exit IP and diagnose a mismatch

Compare the proxied address with the exit or network your provider expects. A different address shows that the lookup saw a different public source for that request. A gateway hostname can resolve to the entry point even when the provider uses a different address for outbound traffic. A rotating provider can also give different exits to separate connections, so use the same documented session settings when you repeat the comparison.

  • Both IPs match. Check that you opened the page in the intended context. In an existing project, review proxy.bypass. It accepts a comma-separated list of hosts that should use the default route, so a matching lookup host makes that result a poor proxy check. Use a lookup allowed by your network policy.
  • The proxy asks for credentials or the tunnel fails. Check the scheme, host, port, username and password. On a connection to an HTTPS target, Chromium can report a failed proxy tunnel before the page receives an HTTP response. A proxy trace that shows a challenge points to HTTP 407 Proxy Authentication Required.
  • The baseline works but the proxied navigation times out. Check that your runner can reach the proxy host and that the provider supports connections to the HTTPS target. Repeat the check with cURL from the same runner to narrow down which client or setting differs.

Once the IP check shows the expected exit, try the page your test actually needs. If that page answers with an HTTP error, capture its status and URL separately from the IP lookup result. A 403 Forbidden on the target can be a destination permission issue. An exit check confirms the observed public address for one request. Site permissions and certificate checks still apply to later navigations.

Keep browser navigation and API requests separate

A Playwright test can make an HTTP call with page.request, which uses the browser context’s associated API request client and shares its cookies. A standalone client created with request.newContext() has its own proxy options and cookie storage. Use page.goto() in the same context as your browser automation to check the route the page uses. Playwright also documents SOCKS5 proxy URLs. The username and password options in this example are for HTTP proxy authentication. Playwright API request context reference.

For a manual comparison on the same machine, check your exit IP in the browser you normally use.

All proxy testing guides · Open the proxy tester