playwright-test-commons - v0.8.0
    Preparing search index...

    Class ApiBase

    Base class for one-client-per-API-area. Subclass it, add one method per endpoint, and export a single instance.

    Name each response body's type in the method (this.get<ProductList>(...)), as Playwright's own request methods allow. getBody, pollUntil and response.json() then return that type without the spec repeating it.

    class ProductsClient extends ApiBase {
    constructor() {
    super({ baseUrl: "https://example.com/api", bodyType: "form" });
    }

    list() {
    return this.get<ProductList>("productsList");
    }
    }

    export const Products = new ProductsClient();
    Index
    baseUrl: string
    bodyType: BodyType
    debug: boolean
    headers: Headers | (() => Headers)
    • Wraps a payload as data (JSON) or form, depending on the client's bodyType.

      Parameters

      • payload: unknown

      Returns {
          data?: any;
          failOnStatusCode?: boolean;
          form?: FormData | { [key: string]: string | number | boolean };
          headers?: { [key: string]: string };
          ignoreHTTPSErrors?: boolean;
          maxRedirects?: number;
          maxRetries?: number;
          method?: string;
          multipart?:
              | FormData
              | {
                  [key: string]: | string
                  | number
                  | boolean
                  | ReadStream
                  | { buffer: Buffer; mimeType: string; name: string };
              };
          params?: | string
          | URLSearchParams
          | { [key: string]: string
          | number
          | boolean };
          signal?: AbortSignal;
          timeout?: number;
      }

      • Optionaldata?: any

        Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string and content-type header will be set to application/json if not explicitly set. Otherwise the content-type header will be set to application/octet-stream if not explicitly set.

      • OptionalfailOnStatusCode?: boolean

        Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes.

      • Optionalform?: FormData | { [key: string]: string | number | boolean }

        Provides an object that will be serialized as html form using application/x-www-form-urlencoded encoding and sent as this request body. If this parameter is specified content-type header will be set to application/x-www-form-urlencoded unless explicitly provided.

      • Optionalheaders?: { [key: string]: string }

        Allows to set HTTP headers. These headers will apply to the fetched request as well as any redirects initiated by it.

      • OptionalignoreHTTPSErrors?: boolean

        Whether to ignore HTTPS errors when sending network requests. Defaults to false.

      • OptionalmaxRedirects?: number

        Maximum number of request redirects that will be followed automatically. An error will be thrown if the number is exceeded. Defaults to 20. Pass 0 to not follow redirects.

      • OptionalmaxRetries?: number

        Maximum number of times network errors should be retried. Currently only ECONNRESET error is retried. Does not retry based on HTTP response codes. An error will be thrown if the limit is exceeded. Defaults to 0 - no retries.

      • Optionalmethod?: string

        If set changes the fetch method (e.g. PUT or POST). If not specified, GET method is used.

      • Optionalmultipart?:
            | FormData
            | {
                [key: string]: | string
                | number
                | boolean
                | ReadStream
                | { buffer: Buffer; mimeType: string; name: string };
            }

        Provides an object that will be serialized as html form using multipart/form-data encoding and sent as this request body. If this parameter is specified content-type header will be set to multipart/form-data unless explicitly provided. File values can be passed either as fs.ReadStream or as file-like object containing file name, mime-type and its content.

      • Optionalparams?: string | URLSearchParams | { [key: string]: string | number | boolean }

        Query parameters to be sent with the URL.

      • Optionalsignal?: AbortSignal

        Allows to cancel the operation using an AbortSignal. If the signal is aborted, the operation will be aborted and throw an error. Note that providing a signal does not disable the default timeout, which can be changed using browserContext.setDefaultTimeout(timeout) or page.setDefaultTimeout(timeout); pass timeout: 0 to disable the timeout entirely.

      • Optionaltimeout?: number

        Request timeout in milliseconds. Defaults to 30000 (30 seconds). Pass 0 to disable timeout.

    • Sends a DELETE request.

      Type Parameters

      • T = any

        the response body's type

      Parameters

      • path: string

        relative to the base URL

      • Optionalpayload: unknown

        request body, encoded as the client's bodyType

      Returns Promise<APIResponse<T>>

    • Releases the request context. Optional: call it from an afterAll if a worker creates many clients.

      Returns Promise<void>

    • Sends a GET request.

      Type Parameters

      • T = any

        the response body's type

      Parameters

      • path: string

        relative to the base URL

      • Optionalparams: string | URLSearchParams | { [key: string]: string | number | boolean }

        query string parameters

      Returns Promise<APIResponse<T>>

    • Logs a labelled value when debug is on.

      Parameters

      • label: string
      • value: unknown

      Returns void

    • Sends a PATCH request.

      Type Parameters

      • T = any

        the response body's type

      Parameters

      • path: string

        relative to the base URL

      • Optionalpayload: unknown

        request body, encoded as the client's bodyType

      Returns Promise<APIResponse<T>>

    post

    • post<T = any>(path: string, payload?: unknown): Promise<APIResponse<T>>

      Sends a POST request.

      Type Parameters

      • T = any

        the response body's type

      Parameters

      • path: string

        relative to the base URL

      • Optionalpayload: unknown

        request body, encoded as the client's bodyType

      Returns Promise<APIResponse<T>>

    • Sends a PUT request.

      Type Parameters

      • T = any

        the response body's type

      Parameters

      • path: string

        relative to the base URL

      • Optionalpayload: unknown

        request body, encoded as the client's bodyType

      Returns Promise<APIResponse<T>>

    • The client's request context: created on first use, then reused for every request in the worker.

      Returns Promise<APIRequestContext>

    • Sends a request. The caller asserts on the response, usually through getBody or errorCheck, which also dispose it.

      Type Parameters

      • T = any

        the response body's type

      Parameters

      • method: string

        HTTP method, any case

      • path: string

        relative to the base URL

      • options: {
            data?: any;
            failOnStatusCode?: boolean;
            form?: FormData | { [key: string]: string | number | boolean };
            headers?: { [key: string]: string };
            ignoreHTTPSErrors?: boolean;
            maxRedirects?: number;
            maxRetries?: number;
            method?: string;
            multipart?:
                | FormData
                | {
                    [key: string]: | string
                    | number
                    | boolean
                    | ReadStream
                    | { buffer: Buffer; mimeType: string; name: string };
                };
            params?: | string
            | URLSearchParams
            | { [key: string]: string
            | number
            | boolean };
            signal?: AbortSignal;
            timeout?: number;
        } = {}

        Playwright fetch options; their headers are merged over the client's

        • Optionaldata?: any

          Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string and content-type header will be set to application/json if not explicitly set. Otherwise the content-type header will be set to application/octet-stream if not explicitly set.

        • OptionalfailOnStatusCode?: boolean

          Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes.

        • Optionalform?: FormData | { [key: string]: string | number | boolean }

          Provides an object that will be serialized as html form using application/x-www-form-urlencoded encoding and sent as this request body. If this parameter is specified content-type header will be set to application/x-www-form-urlencoded unless explicitly provided.

        • Optionalheaders?: { [key: string]: string }

          Allows to set HTTP headers. These headers will apply to the fetched request as well as any redirects initiated by it.

        • OptionalignoreHTTPSErrors?: boolean

          Whether to ignore HTTPS errors when sending network requests. Defaults to false.

        • OptionalmaxRedirects?: number

          Maximum number of request redirects that will be followed automatically. An error will be thrown if the number is exceeded. Defaults to 20. Pass 0 to not follow redirects.

        • OptionalmaxRetries?: number

          Maximum number of times network errors should be retried. Currently only ECONNRESET error is retried. Does not retry based on HTTP response codes. An error will be thrown if the limit is exceeded. Defaults to 0 - no retries.

        • Optionalmethod?: string

          If set changes the fetch method (e.g. PUT or POST). If not specified, GET method is used.

        • Optionalmultipart?:
              | FormData
              | {
                  [key: string]: | string
                  | number
                  | boolean
                  | ReadStream
                  | { buffer: Buffer; mimeType: string; name: string };
              }

          Provides an object that will be serialized as html form using multipart/form-data encoding and sent as this request body. If this parameter is specified content-type header will be set to multipart/form-data unless explicitly provided. File values can be passed either as fs.ReadStream or as file-like object containing file name, mime-type and its content.

        • Optionalparams?: string | URLSearchParams | { [key: string]: string | number | boolean }

          Query parameters to be sent with the URL.

        • Optionalsignal?: AbortSignal

          Allows to cancel the operation using an AbortSignal. If the signal is aborted, the operation will be aborted and throw an error. Note that providing a signal does not disable the default timeout, which can be changed using browserContext.setDefaultTimeout(timeout) or page.setDefaultTimeout(timeout); pass timeout: 0 to disable the timeout entirely.

        • Optionaltimeout?: number

          Request timeout in milliseconds. Defaults to 30000 (30 seconds). Pass 0 to disable timeout.

      Returns Promise<APIResponse<T>>

      the response, typed as APIResponse<T>

    • Builds the full URL for a path.

      Parameters

      • path: string

        relative to the base URL; a leading slash is ignored

      Returns string

      e.g. "https://example.com/api/products" for "products"