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
66.07k stars 3.61k forks source link

[Feature]: Use the spec-compliant fetch in `request` #31518

Open illright opened 3 months ago

illright commented 3 months ago

🚀 Feature Request

In our application, we have a package that abstracts away API calls to the backend. It's a convenient way to setup up the test data. This package allows customizing the fetch function, and it expects a spec-compliant fetch implementation. However, the Playwright fetch differs quite a lot from the conventional fetch provided by browsers.

Currently, the differences are the following:

All of the above makes writing a custom adapter complicated.

Example

This would allow me to create fixtures like the following:

export const test = base.extend({
  callApi: ({ request }, use) =>
    use(async (method, ...args) => {
      const originalFetch = Base.fetch;

      try {
        Base.fetch = request.fetch.bind(request);
        return await method(...args);
      } finally {
        Base.fetch = originalFetch;
      }
    }),
});

Motivation

It will make Playwright's APIs easier to learn because the knowledge will be transferable. It will also allow the freedom of integrating Playwright's request making functionality with anything that is fetch-compatible.

mxschmitt commented 3 months ago

our application, we have a package that abstracts away API calls to the backend.

Would using Node.js fetch() work for you?

illright commented 3 months ago

I don't think so, because I would like to reuse the auth cookies obtained by playwright when logging in. Basically, I would like to take actions as the current user