microsoft / playwright

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
https://playwright.dev
Apache License 2.0
67.21k stars 3.7k forks source link

[Feature]: Allow route fulfillment with a readable stream body #33564

Open zakhenry opened 1 week ago

zakhenry commented 1 week ago

🚀 Feature Request

Currently, Route.fulfill only accepts string|Buffer as body type. Add nodejs type ReadableStream to permit streamed calls.

Example

const bodyStream = new Readable();

await page.route('**/*', async route => {
 route.fulfill({
    status: 200,
    contentType: 'application/grpc-web+proto',
    body: bodyStream
  });
});

// @todo interact with page

bodyStream.push(chunk);
// @todo  assert on DOM state

bodyStream.push(chunk);
// @todo assert on updated DOM state

bodyStream.destroy();
// @todo assert request completed

Motivation

There are several use-cases where this is useful:

  1. stubbing grpc-web calls that are server streaming - the test script could push events into the stream and assert the expected in-progress result is displayed in the webview
  2. stubbing server-sent events
  3. stubbing streams of other kinds (live video?)
gurkohli commented 19 hours ago

This would be very useful for our workflow as well. Would appreciate if this could get onto the roadmap