Đang đăng nhập từ app DHN Proxy...

Playwright Proxy Setup: Browser Context, Authentication & Testing

Try DHN Proxy for free
Playwright Proxy Setup: Browser Context, Authentication & Testing

Configure a proxy in Playwright for legitimate browser testing, add credentials, verify the connection and troubleshoot common proxy errors.

Using a Playwright proxy allows developers to route browser traffic through a proxy server instead of connecting directly from the test machine.

This is useful for localization QA, regional website testing, debugging location-dependent behavior, verifying redirects, and reviewing publicly available content across different markets.

Playwright supports proxy configuration when launching a browser, and compatible setups can also assign proxies to browser contexts.

Quick answer: To configure a proxy in Playwright Node.js, add a proxy object to chromium.launch(). Set server to an endpoint such as http://host:port or socks5://host:port. For authenticated HTTP proxies, include the username and password provided by your proxy service.

const { chromium } = require('playwright');

(async () => {
  const browser = await chromium.launch({
    proxy: {
      server: 'http://proxy.example.com:8080',
      username: 'proxy_user',
      password: 'proxy_password'
    }
  });

  const page = await browser.newPage();
  await page.goto('https://example.com');
  await browser.close();
})();

These credentials are placeholders. In production, use your actual proxy endpoint and store authentication details securely.

How Do You Configure a Proxy in Playwright?

The simplest approach is to specify a proxy when launching the browser.

Here is a basic HTTP proxy configuration without authentication:

const { chromium } = require('playwright');

(async () => {
  const browser = await chromium.launch({
    proxy: {
      server: 'http://proxy.example.com:8080'
    }
  });

  const page = await browser.newPage();

  await page.goto('https://example.com', {
    waitUntil: 'domcontentloaded'
  });

  console.log(await page.title());
  await browser.close();
})();

Proxy endpoints generally follow this format:

http://proxy.example.com:8080
https://proxy.example.com:8443
socks5://proxy.example.com:1080

Choose the protocol supported by your endpoint. HTTPS proxy support and behavior should be checked against your Playwright version and browser engine.

The request path is straightforward:

Playwright → Proxy Server → Target Website

Instead of connecting directly, the browser sends applicable traffic through the configured proxy.

A proxy changes the network route, but it does not automatically change every browser setting or override website access restrictions.

Quick Playwright Proxy Reference

Task

Configuration

Best for

HTTP proxy

server: 'http://host:port'

General browser testing

HTTPS proxy

server: 'https://host:port'

TLS connection to supported proxy

SOCKS5

server: 'socks5://host:port'

SOCKS routing

Authentication

Add username and password

Commercial proxies

Bypass hosts

Add bypass

Local or internal services

Verify proxy

Open an IP-check endpoint

Connection testing

HTTP proxies are a practical starting point for many browser automation workflows when the provider supports them.

You can explore DHN Proxy's proxy options when selecting an endpoint for testing.

When Is a Browser Automation Proxy Useful?

Localization QA

Localization QA checks whether a website delivers the expected experience in different geographic markets.

For example, a team may test storefronts from the United States, Germany, and Singapore.

Common checks include:

  • Display language.

  • Currency formatting.

  • Regional landing pages.

  • Shipping information.

  • Product availability.

  • Public regional promotions.

A location-based proxy helps reproduce a particular network location.

However, a proxy alone does not guarantee an accurate localized experience. Browser language, cookies, account preferences, and shipping details can also influence the result.

Regional Bug Reproduction

Some website issues affect only certain markets.

Examples include unexpected redirects, CDN responses, and region-specific pages.

Testing through a proxy associated with the affected location can help developers compare behavior and reproduce the problem.

Public Regional Content Testing

Playwright proxies can also support authorized checks of geo-based redirects, campaign pages, navigation, CDN behavior, and country-specific storefronts.

Always follow the target website's access conditions and applicable automation policies.

How to Configure Proxy Authentication

Cấu hình Proxy Authentication

Commercial proxy services often require username and password authentication.

For supported endpoints, include these properties in Playwright:

const browser = await chromium.launch({
  proxy: {
    server: 'http://proxy.example.com:8080',
    username: 'proxy_user',
    password: 'proxy_password'
  }
});

These credentials authenticate the connection to the proxy server.

They do not log you into the target website. Website authentication must be handled separately.

Avoid hard-coding real credentials in your source code.

Instead, use environment variables:

const browser = await chromium.launch({
  proxy: {
    server: process.env.PROXY_SERVER,
    username: process.env.PROXY_USERNAME,
    password: process.env.PROXY_PASSWORD
  }
});

Environment variables are useful during local development. For CI/CD pipelines, a dedicated secrets manager is generally preferable.

If you need to review connection details before testing, see the  proxy configuration checklist for antidetect browsers.

Browser-Level vs Context-Level Proxy

Playwright offers different ways to organize proxy configurations.

A browser-level proxy is configured through chromium.launch(). It is usually the simplest approach when one worker uses one network location.

A context-level proxy can be configured through browser.newContext() in supported environments.

Example:

const browser = await chromium.launch();

const context = await browser.newContext({
  proxy: {
    server: 'http://proxy.example.com:8080',
    username: 'proxy_user',
    password: 'proxy_password'
  }
});

const page = await context.newPage();
await page.goto('https://example.com');

await context.close();
await browser.close();

Approach

Advantage

Good for

Browser-level proxy

Simple and predictable

One region per worker

Context-level proxy

More flexible isolation

Multiple test environments

Separate browser per region

Strong separation

Production localization QA

For most projects, browser-level configuration is the easiest starting point.

If your test suite depends on context-level proxy support, verify its behavior with the exact Playwright version and browser engine used in your environment.

How to Use SOCKS5 with Playwright

If your provider supplies a SOCKS5 endpoint, configure it using the socks5:// prefix.

const browser = await chromium.launch({
  proxy: {
    server: 'socks5://proxy.example.com:1080'
  }
});

SOCKS5 authentication support can vary by browser engine and implementation. Test the required authentication method in your actual environment rather than assuming username/password works identically everywhere.

HTTP proxies are suitable for many web-testing workflows.

SOCKS5 is useful when your provider offers SOCKS-based routing or your network architecture requires it.

How to Bypass Local or Internal Hosts

Some test suites need public website traffic routed through a proxy while local services remain directly accessible.

Playwright supports a bypass setting:

const browser = await chromium.launch({
  proxy: {
    server: 'http://proxy.example.com:8080',
    bypass: 'localhost,127.0.0.1,.internal.example.com'
  }
});

This can be useful when an automated browser accesses an external website and an API running in the local development environment.

Verify the hostname matching behavior against your setup to ensure the intended requests bypass the proxy.

How to Verify That Playwright Is Using the Proxy

A browser launching successfully does not prove that the intended proxy connection is working.

A practical check is to request your public IP address from inside Playwright:

const { chromium } = require('playwright');

(async () => {
  const browser = await chromium.launch({
    proxy: {
      server: 'http://proxy.example.com:8080',
      username: 'proxy_user',
      password: 'proxy_password'
    }
  });

  const page = await browser.newPage();

  await page.goto('https://api.ipify.org?format=json');

  console.log(await page.textContent('body'));

  await browser.close();
})();

Compare the returned IP address with the proxy endpoint you intended to use.

For localization QA, also check the reported country and the actual regional content displayed by the website.

A useful verification sequence is:

Connection succeeds → Correct exit IP → Correct region → Expected website experience

A changed IP alone is not sufficient evidence that the localized experience is correct.

Playwright Proxy Setup with Python

Playwright proxy setup with Python for browser automation and web access

Playwright for Python uses a similar proxy configuration structure.

The following example uses the synchronous Python API:

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch(
        proxy={
            "server": "http://proxy.example.com:8080",
            "username": "proxy_user",
            "password": "proxy_password"
        }
    )

    page = browser.new_page()
    page.goto("https://example.com")

    print(page.title())

    browser.close()

Define the environment variables before running the script.

The testing workflow remains consistent across languages: configure the proxy, launch the browser, verify the external IP, and run the intended test.

Common Playwright Proxy Errors

Problem

Likely cause

What to check

Browser cannot connect

Wrong host or port

Proxy endpoint and firewall

Authentication fails

Incorrect credentials

Username and password

Request times out

Slow or unavailable proxy

Endpoint health and latency

Wrong country

Incorrect exit node

Proxy location

SOCKS fails

Wrong protocol

http:// vs socks5://

Localhost stops working

Local traffic is proxied

Add bypass

HTTPS fails

TLS or proxy issue

Certificates and proxy support

One useful troubleshooting technique is to test the same proxy outside Playwright using cURL:

curl \
  -x http://proxy.example.com:8080 \
  -U "proxy_user:proxy_password" \
  https://api.ipify.org

If both cURL and Playwright fail, the issue may involve the proxy endpoint or network.

If cURL works but Playwright fails, investigate the browser engine, Playwright version, authentication support, protocol, and launch configuration.

Avoid changing several settings simultaneously.

Keep One Proxy Stable During a Browser Session

Browser tests often involve several connected page loads.

For example, a test may open a homepage, navigate to a category, and inspect a product page.

If the IP changes during this sequence, it can introduce additional variables into the result.

For localization and regression testing, maintaining one proxy throughout a logical browser session generally helps keep test conditions consistent.

Rotate or change the proxy between independent test jobs when necessary, rather than during a workflow that requires continuity.

For more information on IP stability, read static versus rotating residential proxies.

Security and Compliance

A browser automation proxy should be used for authorized development, testing, and quality assurance.

Using a proxy does not override website terms, access permissions, privacy requirements, or automation restrictions.

Proxy credentials should be treated as sensitive infrastructure information.

Avoid exposing credentials in:

  • Public repositories.

  • CI logs.

  • Playwright traces.

  • Screenshots.

  • Issue trackers.

Review debugging artifacts before sharing them because they may contain access tokens, credentials, or account information.

Use controlled credential storage and appropriate permissions for team environments.

Set Up a Test Proxy Before Scaling

Before introducing proxies across a large Playwright test suite, begin with one endpoint and one representative workflow.

Verify:

  • Proxy authentication.

  • External IP address.

  • Target geographic location.

  • Page-loading reliability.

  • Latency.

  • Session consistency.

  • The complete browser flow.

If you need an endpoint for testing, explore DHN Proxy and configure Playwright using the server and authentication details supplied for your proxy.

Once the configuration has been tested successfully, move credentials into a secrets manager and expand to additional regions or test workers.

Frequently Asked Questions

How do I configure a proxy in Playwright?

Pass a proxy object to chromium.launch() and set its server property to the appropriate proxy URL, including protocol, hostname, and port.

How do I add proxy authentication?

Include username and password in the proxy configuration when the endpoint and browser engine support that authentication method. Store real credentials securely.

Does Playwright support SOCKS5?

Yes, Playwright supports SOCKS5 proxy endpoints. Authentication compatibility should be tested with your chosen browser engine and version.

How can I check whether my Playwright proxy works?

Navigate to an IP-check endpoint such as https://api.ipify.org?format=json, read the response, and compare the outgoing IP address with your proxy configuration.

Should I rotate proxies during a Playwright session?

Usually not for multi-step tests that require consistent conditions. Keep the proxy stable during the logical session unless IP changes are part of the test.

Is HTTP or SOCKS5 better for Playwright?

HTTP is sufficient for many browser testing workflows. SOCKS5 is appropriate when the provider or network architecture requires it.

Why does Playwright time out only when a proxy is enabled?

Possible causes include an unavailable endpoint, incorrect settings, high latency, network restrictions, or problems reaching the destination. Test the same endpoint with cURL to narrow down the issue.

Should I store proxy credentials directly in Playwright code?

No. Use environment variables for local development and a secrets manager for CI/CD or production systems.

Why does the website show the wrong region even when the proxy IP changes?

Websites may also consider cookies, browser locale, account settings, previous sessions, or shipping addresses. Localization testing should control these factors alongside the proxy.