Skip to content

[Question] Does playwright support browser network emulation? #3393

Closed

Description

I guess Puppeteer supports network emulation / throttling. I am looking for something similar using Playwright. I checked the API reference but did not found anything in Network section. Thanks for such a great library.

Activity

  1. aslushnikov commented on Aug 12, 2020

    @aslushnikov
    Contributor

    Venkatesh Devale (@devalevenkatesh) Puppeteer's network throttling is done using Chrome DevTool Protocol; you can do the same with chromium in Playwright using context.newCDPSession():

    const {chromium} = require('playwright');
    
    (async () => {
      const browser = await chromium.launch();
      const context = await browser.newContext();
      const page = await context.newPage();
      const cdp = await context.newCDPSession(page);
      // Set throttling property
      await cdp.send('Network.emulateNetworkConditions', {
        'offline': false,
        'downloadThroughput': 200 * 1024 / 8,
        'uploadThroughput': 200 * 1024 / 8,
        'latency': 20
      });
      // Go on with the throttled page...
      await page.goto('https://example.com');
    })();

    This method, however, has a few downsides:

    • will not work in other browsers in Playwright since there's no CDP
    • does not work ideally in Chromium either since webrtc and websockets are not throttled

    To throttle "for real", we'd recommend user-land solutions:

    • Charles Proxy is quite popular and works across OS'es.
    • On Linux, network shaping could be configured per network interface with tools like tc. wondershaper is a convenience wrapper atop of tc and might be a good starting point.
    • On Mac, there's Network Link Conditioner.

    Hope this helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions