diff --git a/docs/src/api/class-apirequest.md b/docs/src/api/class-apirequest.md new file mode 100644 index 0000000000..09bba3555c --- /dev/null +++ b/docs/src/api/class-apirequest.md @@ -0,0 +1,53 @@ +# class: ApiRequest +* langs: js + +Exposes API that can be used for the Web API testing. + +## async method: ApiRequest.newContext +* langs: js +- returns: <[ApiRequestContext]> + +**experimental** Creates new instances of [ApiRequestContext]. + +### option: ApiRequest.newContext.useragent = %%-context-option-useragent-%% +### option: ApiRequest.newContext.extraHTTPHeaders = %%-context-option-extrahttpheaders-%% +### option: ApiRequest.newContext.httpCredentials = %%-context-option-httpcredentials-%% +### option: ApiRequest.newContext.proxy = %%-browser-option-proxy-%% +### option: ApiRequest.newContext.ignoreHTTPSErrors = %%-context-option-ignorehttpserrors-%% + +### option: ApiRequest.newContext.timeout +- `timeout` <[float]> + +Maximum time in milliseconds to wait for the response. Defaults to +`30000` (30 seconds). Pass `0` to disable timeout. + + +### option: ApiRequest.newContext.baseURL +- `baseURL` <[string]> + +When using [`method: ApiRequestContext.get`], [`method: ApiRequestContext.post`], [`method: ApiRequestContext.fetch`] it takes the base URL in consideration by using the [`URL()`](https://developer.mozilla.org/en-US/docs/Web/API/URL/URL) constructor for building the corresponding URL. Examples: +* baseURL: `http://localhost:3000` and sending rquest to `/bar.html` results in `http://localhost:3000/bar.html` +* baseURL: `http://localhost:3000/foo/` and sending rquest to `./bar.html` results in `http://localhost:3000/foo/bar.html` + +### option: ApiRequest.newContext.storageState +- `storageState` <[path]|[Object]> + - `cookies` <[Array]<[Object]>> + - `name` <[string]> + - `value` <[string]> + - `domain` <[string]> + - `path` <[string]> + - `expires` <[float]> Unix time in seconds. + - `httpOnly` <[boolean]> + - `secure` <[boolean]> + - `sameSite` <[SameSiteAttribute]<"Strict"|"Lax"|"None">> + - `origins` <[Array]<[Object]>> + - `origin` <[string]> + - `localStorage` <[Array]<[Object]>> + - `name` <[string]> + - `value` <[string]> + +Populates context with given storage state. This option can be used to initialize context with logged-in information +obtained via [`method: BrowserContext.storageState`] or [`method: ApiRequestContext.storageState`]. Either a path to the +file with saved storage, or the value returned by one of [`method: BrowserContext.storageState`] or +[`method: ApiRequestContext.storageState`] methods. + diff --git a/docs/src/api/class-fetchrequest.md b/docs/src/api/class-apirequestcontext.md similarity index 57% rename from docs/src/api/class-fetchrequest.md rename to docs/src/api/class-apirequestcontext.md index 4fcd187552..24b3a85b69 100644 --- a/docs/src/api/class-fetchrequest.md +++ b/docs/src/api/class-apirequestcontext.md @@ -1,137 +1,136 @@ -# class: FetchRequest +# class: ApiRequestContext * langs: js -This API is used for Web API testing. You can use it to trigger API endpoints, configure micro-services, prepare +This API is used for the Web API testing. You can use it to trigger API endpoints, configure micro-services, prepare environment or the service to your e2e test. When used on [Page] or a [BrowserContext], this API will automatically use the cookies from the corresponding [BrowserContext]. This means that if you log in using this API, your e2e test will be logged in and vice versa. -## async method: FetchRequest.dispose +## async method: ApiRequestContext.dispose -All responses received through [`method: FetchRequest.fetch`], [`method: FetchRequest.get`], [`method: FetchRequest.post`] -and other methods are stored in the memory, so that you can later call [`method: FetchResponse.body`]. This method -discards all stored responses, and makes [`method: FetchResponse.body`] throw "Response disposed" error. +All responses received through [`method: ApiRequestContext.fetch`], [`method: ApiRequestContext.get`], [`method: ApiRequestContext.post`] +and other methods are stored in the memory, so that you can later call [`method: ApiResponse.body`]. This method +discards all stored responses, and makes [`method: ApiResponse.body`] throw "Response disposed" error. -## async method: FetchRequest.fetch -- returns: <[FetchResponse]> +## async method: ApiRequestContext.fetch +- returns: <[ApiResponse]> Sends HTTP(S) fetch and returns its response. The method will populate fetch cookies from the context and update context cookies from the response. The method will automatically follow redirects. -### param: FetchRequest.fetch.urlOrRequest +### param: ApiRequestContext.fetch.urlOrRequest - `urlOrRequest` <[string]|[Request]> Target URL or Request to get all fetch parameters from. -### option: FetchRequest.fetch.params +### option: ApiRequestContext.fetch.params - `params` <[Object]<[string], [string]>> Query parameters to be send with the URL. -### option: FetchRequest.fetch.method +### option: ApiRequestContext.fetch.method - `method` <[string]> If set changes the fetch method (e.g. PUT or POST). If not specified, GET method is used. -### option: FetchRequest.fetch.headers +### option: ApiRequestContext.fetch.headers - `headers` <[Object]<[string], [string]>> Allows to set HTTP headers. -### option: FetchRequest.fetch.data = %%-fetch-option-data-%% +### option: ApiRequestContext.fetch.data = %%-fetch-option-data-%% +### option: ApiRequestContext.fetch.form = %%-fetch-option-form-%% +### option: ApiRequestContext.fetch.multipart = %%-fetch-option-multipart-%% -### option: FetchRequest.fetch.form = %%-fetch-option-form-%% - -### option: FetchRequest.fetch.multipart = %%-fetch-option-multipart-%% - -### option: FetchRequest.fetch.timeout +### option: ApiRequestContext.fetch.timeout - `timeout` <[float]> Request timeout in milliseconds. -### option: FetchRequest.fetch.failOnStatusCode +### option: ApiRequestContext.fetch.failOnStatusCode - `failOnStatusCode` <[boolean]> Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes. -### option: FetchRequest.fetch.ignoreHTTPSErrors = %%-context-option-ignorehttpserrors-%% +### option: ApiRequestContext.fetch.ignoreHTTPSErrors = %%-context-option-ignorehttpserrors-%% -## async method: FetchRequest.get -- returns: <[FetchResponse]> + +## async method: ApiRequestContext.get +- returns: <[ApiResponse]> Sends HTTP(S) GET request and returns its response. The method will populate fetch cookies from the context and update context cookies from the response. The method will automatically follow redirects. -### param: FetchRequest.get.url +### param: ApiRequestContext.get.url - `url` <[string]> Target URL. -### option: FetchRequest.get.params +### option: ApiRequestContext.get.params - `params` <[Object]<[string], [string]>> Query parameters to be send with the URL. -### option: FetchRequest.get.headers +### option: ApiRequestContext.get.headers - `headers` <[Object]<[string], [string]>> Allows to set HTTP headers. -### option: FetchRequest.get.timeout +### option: ApiRequestContext.get.timeout - `timeout` <[float]> Request timeout in milliseconds. -### option: FetchRequest.get.failOnStatusCode +### option: ApiRequestContext.get.failOnStatusCode - `failOnStatusCode` <[boolean]> Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes. -### option: FetchRequest.get.ignoreHTTPSErrors = %%-context-option-ignorehttpserrors-%% +### option: ApiRequestContext.get.ignoreHTTPSErrors = %%-context-option-ignorehttpserrors-%% -## async method: FetchRequest.post -- returns: <[FetchResponse]> +## async method: ApiRequestContext.post +- returns: <[ApiResponse]> Sends HTTP(S) fetch and returns its response. The method will populate fetch cookies from the context and update context cookies from the response. The method will automatically follow redirects. -### param: FetchRequest.post.url +### param: ApiRequestContext.post.url - `url` <[string]> Target URL. -### option: FetchRequest.post.params +### option: ApiRequestContext.post.params - `params` <[Object]<[string], [string]>> Query parameters to be send with the URL. -### option: FetchRequest.post.headers +### option: ApiRequestContext.post.headers - `headers` <[Object]<[string], [string]>> Allows to set HTTP headers. -### option: FetchRequest.post.data = %%-fetch-option-data-%% +### option: ApiRequestContext.post.data = %%-fetch-option-data-%% -### option: FetchRequest.post.form = %%-fetch-option-form-%% +### option: ApiRequestContext.post.form = %%-fetch-option-form-%% -### option: FetchRequest.post.multipart = %%-fetch-option-multipart-%% +### option: ApiRequestContext.post.multipart = %%-fetch-option-multipart-%% -### option: FetchRequest.post.timeout +### option: ApiRequestContext.post.timeout - `timeout` <[float]> Request timeout in milliseconds. -### option: FetchRequest.post.failOnStatusCode +### option: ApiRequestContext.post.failOnStatusCode - `failOnStatusCode` <[boolean]> Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes. -### option: FetchRequest.post.ignoreHTTPSErrors = %%-context-option-ignorehttpserrors-%% +### option: ApiRequestContext.post.ignoreHTTPSErrors = %%-context-option-ignorehttpserrors-%% -## async method: FetchRequest.storageState +## async method: ApiRequestContext.storageState - returns: <[Object]> - `cookies` <[Array]<[Object]>> - `name` <[string]> @@ -150,4 +149,4 @@ for all status codes. Returns storage state for this request context, contains current cookies and local storage snapshot if it was passed to the constructor. -### option: FetchRequest.storageState.path = %%-storagestate-option-path-%% +### option: ApiRequestContext.storageState.path = %%-storagestate-option-path-%% diff --git a/docs/src/api/class-fetchresponse.md b/docs/src/api/class-apiresponse.md similarity index 74% rename from docs/src/api/class-fetchresponse.md rename to docs/src/api/class-apiresponse.md index 6c5ee6342a..b2e32df641 100644 --- a/docs/src/api/class-fetchresponse.md +++ b/docs/src/api/class-apiresponse.md @@ -1,23 +1,23 @@ -# class: FetchResponse +# class: ApiResponse * langs: js -[FetchResponse] class represents responses received from [`method: FetchRequest.fetch`]. +[ApiResponse] class represents responses received from [`method: ApiRequestContext.fetch`]. -## async method: FetchResponse.body +## async method: ApiResponse.body - returns: <[Buffer]> Returns the buffer with response body. -## async method: FetchResponse.dispose +## async method: ApiResponse.dispose Disposes the body of this response. If not called then the body will stay in memory until the context closes. -## method: FetchResponse.headers +## method: ApiResponse.headers - returns: <[Object]<[string], [string]>> An object with all the response HTTP headers associated with this response. -## method: FetchResponse.headersArray +## method: ApiResponse.headersArray - returns: <[Array]<[Object]>> - `name` <[string]> Name of the header. - `value` <[string]> Value of the header. @@ -25,7 +25,7 @@ An object with all the response HTTP headers associated with this response. An array with all the request HTTP headers associated with this response. Header names are not lower-cased. Headers with multiple entries, such as `Set-Cookie`, appear in the array multiple times. -## async method: FetchResponse.json +## async method: ApiResponse.json * langs: js, python - returns: <[Serializable]> @@ -33,7 +33,7 @@ Returns the JSON representation of response body. This method will throw if the response body is not parsable via `JSON.parse`. -## async method: FetchResponse.json +## async method: ApiResponse.json * langs: csharp - returns: <[null]|[JsonElement]> @@ -41,27 +41,27 @@ Returns the JSON representation of response body. This method will throw if the response body is not parsable via `JSON.parse`. -## method: FetchResponse.ok +## method: ApiResponse.ok - returns: <[boolean]> Contains a boolean stating whether the response was successful (status in the range 200-299) or not. -## method: FetchResponse.status +## method: ApiResponse.status - returns: <[int]> Contains the status code of the response (e.g., 200 for a success). -## method: FetchResponse.statusText +## method: ApiResponse.statusText - returns: <[string]> Contains the status text of the response (e.g. usually an "OK" for a success). -## async method: FetchResponse.text +## async method: ApiResponse.text - returns: <[string]> Returns the text representation of response body. -## method: FetchResponse.url +## method: ApiResponse.url - returns: <[string]> Contains the URL of the response. diff --git a/docs/src/api/class-browsercontext.md b/docs/src/api/class-browsercontext.md index 9a2adb200c..7ee8ba6265 100644 --- a/docs/src/api/class-browsercontext.md +++ b/docs/src/api/class-browsercontext.md @@ -851,7 +851,7 @@ Returns all open pages in the context. ## property: BrowserContext.request * langs: js -- type: <[FetchRequest]> +- type: <[ApiRequestContext]> API testing helper associated with this context. Requests made with this API will use context cookies. diff --git a/docs/src/api/class-page.md b/docs/src/api/class-page.md index 2cef4864a4..85fb9bb244 100644 --- a/docs/src/api/class-page.md +++ b/docs/src/api/class-page.md @@ -2416,7 +2416,7 @@ last redirect. ## property: Page.request * langs: js -- type: <[FetchRequest]> +- type: <[ApiRequestContext]> API testing helper associated with this page. Requests made with this API will use page cookies. diff --git a/docs/src/api/class-playwright.md b/docs/src/api/class-playwright.md index b6e293d029..ad6bb1c67e 100644 --- a/docs/src/api/class-playwright.md +++ b/docs/src/api/class-playwright.md @@ -214,56 +214,11 @@ except TimeoutError as e: This object can be used to launch or connect to Firefox, returning instances of [Browser]. -## async method: Playwright.newRequest +## property: Playwright.request * langs: js -- returns: <[FetchRequest]> +- type: <[ApiRequest]> -**experimental** Creates new instances of [FetchRequest]. - -### option: Playwright.newRequest.useragent = %%-context-option-useragent-%% - -### option: Playwright.newRequest.extraHTTPHeaders = %%-context-option-extrahttpheaders-%% - -### option: Playwright.newRequest.httpCredentials = %%-context-option-httpcredentials-%% - -### option: Playwright.newRequest.proxy = %%-browser-option-proxy-%% - -### option: Playwright.newRequest.timeout -- `timeout` <[float]> - -Maximum time in milliseconds to wait for the response. Defaults to -`30000` (30 seconds). Pass `0` to disable timeout. - -### option: Playwright.newRequest.ignoreHTTPSErrors = %%-context-option-ignorehttpserrors-%% - -### option: Playwright.newRequest.baseURL -- `baseURL` <[string]> - -When using [`method: FetchRequest.get`], [`method: FetchRequest.post`], [`method: FetchRequest.fetch`] it takes the base URL in consideration by using the [`URL()`](https://developer.mozilla.org/en-US/docs/Web/API/URL/URL) constructor for building the corresponding URL. Examples: -* baseURL: `http://localhost:3000` and sending rquest to `/bar.html` results in `http://localhost:3000/bar.html` -* baseURL: `http://localhost:3000/foo/` and sending rquest to `./bar.html` results in `http://localhost:3000/foo/bar.html` - -### option: Playwright.newRequest.storageState -- `storageState` <[path]|[Object]> - - `cookies` <[Array]<[Object]>> - - `name` <[string]> - - `value` <[string]> - - `domain` <[string]> - - `path` <[string]> - - `expires` <[float]> Unix time in seconds. - - `httpOnly` <[boolean]> - - `secure` <[boolean]> - - `sameSite` <[SameSiteAttribute]<"Strict"|"Lax"|"None">> - - `origins` <[Array]<[Object]>> - - `origin` <[string]> - - `localStorage` <[Array]<[Object]>> - - `name` <[string]> - - `value` <[string]> - -Populates context with given storage state. This option can be used to initialize context with logged-in information -obtained via [`method: BrowserContext.storageState`] or [`method: FetchRequest.storageState`]. Either a path to the -file with saved storage, or the value returned by one of [`method: BrowserContext.storageState`] or -[`method: FetchRequest.storageState`] methods. +Exposes API that can be used for the Web API testing. ## property: Playwright.selectors - type: <[Selectors]> diff --git a/docs/src/api/class-route.md b/docs/src/api/class-route.md index eb2a5f806a..c4af7b88b8 100644 --- a/docs/src/api/class-route.md +++ b/docs/src/api/class-route.md @@ -222,9 +222,9 @@ is resolved relative to the current working directory. ### option: Route.fulfill.response * langs: js -- `response` <[FetchResponse]> +- `response` <[ApiResponse]> -[FetchResponse] to fulfill route's request with. Individual fields of the response (such as headers) can be overridden using fulfill options. +[ApiResponse] to fulfill route's request with. Individual fields of the response (such as headers) can be overridden using fulfill options. ## method: Route.request - returns: <[Request]> diff --git a/src/client/api.ts b/src/client/api.ts index 3fb9dac488..6668582423 100644 --- a/src/client/api.ts +++ b/src/client/api.ts @@ -34,7 +34,7 @@ export { Frame } from './frame'; export { Keyboard, Mouse, Touchscreen } from './input'; export { JSHandle } from './jsHandle'; export { Request, Response, Route, WebSocket } from './network'; -export { FetchRequest, FetchResponse } from './fetch'; +export { Fetch as ApiRequest, FetchRequest as ApiRequestContext, FetchResponse as ApiResponse } from './fetch'; export { Page } from './page'; export { Selectors } from './selectors'; export { Tracing } from './tracing'; diff --git a/src/client/fetch.ts b/src/client/fetch.ts index 4af738da19..eb9b6c2be9 100644 --- a/src/client/fetch.ts +++ b/src/client/fetch.ts @@ -27,6 +27,7 @@ import { ChannelOwner } from './channelOwner'; import * as network from './network'; import { RawHeaders } from './network'; import { FilePayload, Headers, StorageState } from './types'; +import { Playwright } from './playwright'; export type FetchOptions = { params?: { [key: string]: string; }, @@ -40,7 +41,32 @@ export type FetchOptions = { ignoreHTTPSErrors?: boolean, }; -export class FetchRequest extends ChannelOwner implements api.FetchRequest { +type NewContextOptions = Omit & { + extraHTTPHeaders?: Headers, + storageState?: string | StorageState, +}; + +export class Fetch implements api.ApiRequest { + private _playwright: Playwright; + constructor(playwright: Playwright) { + this._playwright = playwright; + } + + async newContext(options: NewContextOptions = {}): Promise { + return await this._playwright._wrapApiCall(async (channel: channels.PlaywrightChannel) => { + const storageState = typeof options.storageState === 'string' ? + JSON.parse(await fs.promises.readFile(options.storageState, 'utf8')) : + options.storageState; + return FetchRequest.from((await channel.newRequest({ + ...options, + extraHTTPHeaders: options.extraHTTPHeaders ? headersObjectToArray(options.extraHTTPHeaders) : undefined, + storageState, + })).request); + }); + } +} + +export class FetchRequest extends ChannelOwner implements api.ApiRequestContext { static from(channel: channels.FetchRequestChannel): FetchRequest { return (channel as any)._object; } @@ -153,7 +179,7 @@ export class FetchRequest extends ChannelOwner { let useInterceptedResponseBody; let fetchResponseUid; diff --git a/src/client/playwright.ts b/src/client/playwright.ts index bbf0290b78..d862944b86 100644 --- a/src/client/playwright.ts +++ b/src/client/playwright.ts @@ -15,20 +15,18 @@ */ import dns from 'dns'; -import fs from 'fs'; import net from 'net'; import util from 'util'; import * as channels from '../protocol/channels'; import { TimeoutError } from '../utils/errors'; import { createSocket } from '../utils/netUtils'; -import { headersObjectToArray } from '../utils/utils'; import { Android } from './android'; import { BrowserType } from './browserType'; import { ChannelOwner } from './channelOwner'; import { Electron } from './electron'; -import { FetchRequest } from './fetch'; +import { Fetch } from './fetch'; import { Selectors, SelectorsOwner, sharedSelectors } from './selectors'; -import { NewRequestOptions, Size } from './types'; +import { Size } from './types'; const dnsLookupAsync = util.promisify(dns.lookup); type DeviceDescriptor = { @@ -49,6 +47,7 @@ export class Playwright extends ChannelOwner(); @@ -56,6 +55,7 @@ export class Playwright extends ChannelOwner { - return await this._wrapApiCall(async (channel: channels.PlaywrightChannel) => { - const storageState = typeof options.storageState === 'string' ? - JSON.parse(await fs.promises.readFile(options.storageState, 'utf8')) : - options.storageState; - return FetchRequest.from((await channel.newRequest({ - ...options, - extraHTTPHeaders: options.extraHTTPHeaders ? headersObjectToArray(options.extraHTTPHeaders) : undefined, - storageState, - })).request); - }); - } - _enablePortForwarding(redirectPortForTest?: number) { this._redirectPortForTest = redirectPortForTest; this._channel.on('socksRequested', ({ uid, host, port }) => this._onSocksRequested(uid, host, port)); diff --git a/src/client/types.ts b/src/client/types.ts index 8bae20de73..353ccbbe2c 100644 --- a/src/client/types.ts +++ b/src/client/types.ts @@ -118,8 +118,3 @@ export type SelectorEngine = { export type RemoteAddr = channels.RemoteAddr; export type SecurityDetails = channels.SecurityDetails; - -export type NewRequestOptions = Omit & { - extraHTTPHeaders?: Headers, - storageState?: string | StorageState, -}; diff --git a/tests/browsercontext-fetch.spec.ts b/tests/browsercontext-fetch.spec.ts index ed23bc92d9..d1c0d334b7 100644 --- a/tests/browsercontext-fetch.spec.ts +++ b/tests/browsercontext-fetch.spec.ts @@ -885,4 +885,4 @@ it('context request should export same storage state as context', async ({ conte expect(requestState).toEqual(contextState); const pageState = await page.request.storageState(); expect(pageState).toEqual(contextState); -}); \ No newline at end of file +}); diff --git a/tests/global-fetch-cookie.spec.ts b/tests/global-fetch-cookie.spec.ts index 841d75345e..24167e5563 100644 --- a/tests/global-fetch-cookie.spec.ts +++ b/tests/global-fetch-cookie.spec.ts @@ -16,23 +16,23 @@ import fs from 'fs'; import http from 'http'; -import { FetchRequest } from '../index'; +import { ApiRequestContext } from '../index'; import { expect, playwrightTest } from './config/browserTest'; export type GlobalFetchFixtures = { - request: FetchRequest; + request: ApiRequestContext; }; const it = playwrightTest.extend({ request: async ({ playwright }, use) => { - const request = await playwright.newRequest({ ignoreHTTPSErrors: true }); + const request = await playwright.request.newContext({ ignoreHTTPSErrors: true }); await use(request); await request.dispose(); }, }); type PromiseArg = T extends Promise ? R : never; -type StorageStateType = PromiseArg>; +type StorageStateType = PromiseArg>; it.skip(({ mode }) => mode !== 'default'); @@ -235,7 +235,7 @@ it('should preserve local storage on import/export of storage state', async ({ p }, ] }; - const request = await playwright.newRequest({ storageState }); + const request = await playwright.request.newContext({ storageState }); await request.get(server.EMPTY_PAGE); const exportedState = await request.storageState(); expect(exportedState).toEqual(storageState); @@ -279,7 +279,7 @@ it('should send cookies from storage state', async ({ playwright, server }) => { ], 'origins': [] }; - const request = await playwright.newRequest({ storageState }); + const request = await playwright.request.newContext({ storageState }); const [serverRequest] = await Promise.all([ server.waitForRequest('/first/second/third/not_found.html'), request.get(`http://www.a.b.one.com:${server.PORT}/first/second/third/not_found.html`) @@ -304,7 +304,7 @@ it('storage state should round-trip through file', async ({ playwright, server } 'origins': [] }; - const request1 = await playwright.newRequest({ storageState }); + const request1 = await playwright.request.newContext({ storageState }); const path = testInfo.outputPath('storage-state.json'); const state1 = await request1.storageState({ path }); expect(state1).toEqual(storageState); @@ -312,7 +312,7 @@ it('storage state should round-trip through file', async ({ playwright, server } const written = await fs.promises.readFile(path, 'utf8'); expect(JSON.stringify(state1, undefined, 2)).toBe(written); - const request2 = await playwright.newRequest({ storageState: path }); + const request2 = await playwright.request.newContext({ storageState: path }); const state2 = await request2.storageState(); expect(state2).toEqual(storageState); }); diff --git a/tests/global-fetch.spec.ts b/tests/global-fetch.spec.ts index e53608be4f..2ec950e300 100644 --- a/tests/global-fetch.spec.ts +++ b/tests/global-fetch.spec.ts @@ -40,7 +40,7 @@ it.afterAll(() => { for (const method of ['get', 'post', 'fetch']) { it(`${method} should work`, async ({ playwright, server }) => { - const request = await playwright.newRequest(); + const request = await playwright.request.newContext(); const response = await request[method](server.PREFIX + '/simple.json'); expect(response.url()).toBe(server.PREFIX + '/simple.json'); expect(response.status()).toBe(200); @@ -53,7 +53,7 @@ for (const method of ['get', 'post', 'fetch']) { }); it(`should dispose global ${method} request`, async function({ playwright, context, server }) { - const request = await playwright.newRequest(); + const request = await playwright.request.newContext(); const response = await request.get(server.PREFIX + '/simple.json'); expect(await response.json()).toEqual({ foo: 'bar' }); await request.dispose(); @@ -63,7 +63,7 @@ for (const method of ['get', 'post', 'fetch']) { } it('should support global userAgent option', async ({ playwright, server }) => { - const request = await playwright.newRequest({ userAgent: 'My Agent' }); + const request = await playwright.request.newContext({ userAgent: 'My Agent' }); const [serverRequest, response] = await Promise.all([ server.waitForRequest('/empty.html'), request.get(server.EMPTY_PAGE) @@ -74,7 +74,7 @@ it('should support global userAgent option', async ({ playwright, server }) => { }); it('should support global timeout option', async ({ playwright, server }) => { - const request = await playwright.newRequest({ timeout: 1 }); + const request = await playwright.request.newContext({ timeout: 1 }); server.setRoute('/empty.html', (req, res) => {}); const error = await request.get(server.EMPTY_PAGE).catch(e => e); expect(error.message).toContain('Request timed out after 1ms'); @@ -83,7 +83,7 @@ it('should support global timeout option', async ({ playwright, server }) => { it('should propagate extra http headers with redirects', async ({ playwright, server }) => { server.setRedirect('/a/redirect1', '/b/c/redirect2'); server.setRedirect('/b/c/redirect2', '/simple.json'); - const request = await playwright.newRequest({ extraHTTPHeaders: { 'My-Secret': 'Value' } }); + const request = await playwright.request.newContext({ extraHTTPHeaders: { 'My-Secret': 'Value' } }); const [req1, req2, req3] = await Promise.all([ server.waitForRequest('/a/redirect1'), server.waitForRequest('/b/c/redirect2'), @@ -97,12 +97,12 @@ it('should propagate extra http headers with redirects', async ({ playwright, se it('should support global httpCredentials option', async ({ playwright, server }) => { server.setAuth('/empty.html', 'user', 'pass'); - const request1 = await playwright.newRequest(); + const request1 = await playwright.request.newContext(); const response1 = await request1.get(server.EMPTY_PAGE); expect(response1.status()).toBe(401); await request1.dispose(); - const request2 = await playwright.newRequest({ httpCredentials: { username: 'user', password: 'pass' } }); + const request2 = await playwright.request.newContext({ httpCredentials: { username: 'user', password: 'pass' } }); const response2 = await request2.get(server.EMPTY_PAGE); expect(response2.status()).toBe(200); await request2.dispose(); @@ -110,7 +110,7 @@ it('should support global httpCredentials option', async ({ playwright, server } it('should return error with wrong credentials', async ({ playwright, server }) => { server.setAuth('/empty.html', 'user', 'pass'); - const request = await playwright.newRequest({ httpCredentials: { username: 'user', password: 'wrong' } }); + const request = await playwright.request.newContext({ httpCredentials: { username: 'user', password: 'wrong' } }); const response2 = await request.get(server.EMPTY_PAGE); expect(response2.status()).toBe(401); }); @@ -122,7 +122,7 @@ it('should pass proxy credentials', async ({ playwright, server, proxyServer }) auth = req.headers['proxy-authorization']; return !!auth; }); - const request = await playwright.newRequest({ + const request = await playwright.request.newContext({ proxy: { server: `localhost:${proxyServer.PORT}`, username: 'user', password: 'secret' } }); const response = await request.get('http://non-existent.com/simple.json'); @@ -133,19 +133,19 @@ it('should pass proxy credentials', async ({ playwright, server, proxyServer }) }); it('should support global ignoreHTTPSErrors option', async ({ playwright, httpsServer }) => { - const request = await playwright.newRequest({ ignoreHTTPSErrors: true }); + const request = await playwright.request.newContext({ ignoreHTTPSErrors: true }); const response = await request.get(httpsServer.EMPTY_PAGE); expect(response.status()).toBe(200); }); it('should resolve url relative to gobal baseURL option', async ({ playwright, server }) => { - const request = await playwright.newRequest({ baseURL: server.PREFIX }); + const request = await playwright.request.newContext({ baseURL: server.PREFIX }); const response = await request.get('/empty.html'); expect(response.url()).toBe(server.EMPTY_PAGE); }); it('should set playwright as user-agent', async ({ playwright, server }) => { - const request = await playwright.newRequest(); + const request = await playwright.request.newContext(); const [serverRequest] = await Promise.all([ server.waitForRequest('/empty.html'), request.get(server.EMPTY_PAGE) diff --git a/tests/page/page-request-fulfill.spec.ts b/tests/page/page-request-fulfill.spec.ts index 3fab893ba0..a20ce630f1 100644 --- a/tests/page/page-request-fulfill.spec.ts +++ b/tests/page/page-request-fulfill.spec.ts @@ -197,7 +197,7 @@ it('should include the origin header', async ({ page, server, isAndroid }) => { it('should fulfill with global fetch result', async ({ playwright, page, server, isElectron }) => { it.fixme(isElectron, 'error: Browser context management is not supported.'); await page.route('**/*', async route => { - const request = await playwright.newRequest(); + const request = await playwright.request.newContext(); const response = await request.get(server.PREFIX + '/simple.json'); route.fulfill({ response }); }); @@ -277,7 +277,7 @@ it('should fulfill with fetch response that has multiple set-cookie', async ({ p res.end(); }); await page.route('**/empty.html', async route => { - const request = await playwright.newRequest(); + const request = await playwright.request.newContext(); const response = await request.fetch(route.request()); route.fulfill({ response }); }); diff --git a/types/types.d.ts b/types/types.d.ts index 28ff99f817..116e50e777 100644 --- a/types/types.d.ts +++ b/types/types.d.ts @@ -2753,7 +2753,7 @@ export interface Page { /** * API testing helper associated with this page. Requests made with this API will use page cookies. */ - request: FetchRequest; + request: ApiRequestContext; /** * Routing provides the capability to modify network requests that are made by a page. @@ -6455,7 +6455,7 @@ export interface BrowserContext { /** * API testing helper associated with this context. Requests made with this API will use context cookies. */ - request: FetchRequest; + request: ApiRequestContext; /** * Routing provides the capability to modify network requests that are made by any page in the browser context. Once route @@ -11589,6 +11589,441 @@ export interface AndroidWebView { pkg(): string; } +/** + * Exposes API that can be used for the Web API testing. + */ +export interface ApiRequest { + /** + * **experimental** Creates new instances of [ApiRequestContext]. + * @param options + */ + newContext(options?: { + /** + * When using + * [apiRequestContext.get(url[, options])](https://playwright.dev/docs/api/class-apirequestcontext#api-request-context-get), + * [apiRequestContext.post(url[, options])](https://playwright.dev/docs/api/class-apirequestcontext#api-request-context-post), + * [apiRequestContext.fetch(urlOrRequest[, options])](https://playwright.dev/docs/api/class-apirequestcontext#api-request-context-fetch) + * it takes the base URL in consideration by using the [`URL()`](https://developer.mozilla.org/en-US/docs/Web/API/URL/URL) + * constructor for building the corresponding URL. Examples: + * - baseURL: `http://localhost:3000` and sending rquest to `/bar.html` results in `http://localhost:3000/bar.html` + * - baseURL: `http://localhost:3000/foo/` and sending rquest to `./bar.html` results in + * `http://localhost:3000/foo/bar.html` + */ + baseURL?: string; + + /** + * An object containing additional HTTP headers to be sent with every request. + */ + extraHTTPHeaders?: { [key: string]: string; }; + + /** + * Credentials for [HTTP authentication](https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication). + */ + httpCredentials?: { + username: string; + + password: string; + }; + + /** + * Whether to ignore HTTPS errors when sending network requests. Defaults to `false`. + */ + ignoreHTTPSErrors?: boolean; + + /** + * Network proxy settings. + */ + proxy?: { + /** + * Proxy to be used for all requests. HTTP and SOCKS proxies are supported, for example `http://myproxy.com:3128` or + * `socks5://myproxy.com:3128`. Short form `myproxy.com:3128` is considered an HTTP proxy. + */ + server: string; + + /** + * Optional coma-separated domains to bypass proxy, for example `".com, chromium.org, .domain.com"`. + */ + bypass?: string; + + /** + * Optional username to use if HTTP proxy requires authentication. + */ + username?: string; + + /** + * Optional password to use if HTTP proxy requires authentication. + */ + password?: string; + }; + + /** + * Populates context with given storage state. This option can be used to initialize context with logged-in information + * obtained via + * [browserContext.storageState([options])](https://playwright.dev/docs/api/class-browsercontext#browser-context-storage-state) + * or + * [apiRequestContext.storageState([options])](https://playwright.dev/docs/api/class-apirequestcontext#api-request-context-storage-state). + * Either a path to the file with saved storage, or the value returned by one of + * [browserContext.storageState([options])](https://playwright.dev/docs/api/class-browsercontext#browser-context-storage-state) + * or + * [apiRequestContext.storageState([options])](https://playwright.dev/docs/api/class-apirequestcontext#api-request-context-storage-state) + * methods. + */ + storageState?: string|{ + cookies: Array<{ + name: string; + + value: string; + + domain: string; + + path: string; + + /** + * Unix time in seconds. + */ + expires: number; + + httpOnly: boolean; + + secure: boolean; + + sameSite: "Strict"|"Lax"|"None"; + }>; + + origins: Array<{ + origin: string; + + localStorage: Array<{ + name: string; + + value: string; + }>; + }>; + }; + + /** + * Maximum time in milliseconds to wait for the response. Defaults to `30000` (30 seconds). Pass `0` to disable timeout. + */ + timeout?: number; + + /** + * Specific user agent to use in this context. + */ + userAgent?: string; + }): Promise; +} + +/** + * This API is used for the Web API testing. You can use it to trigger API endpoints, configure micro-services, prepare + * environment or the service to your e2e test. When used on [Page] or a [BrowserContext], this API will automatically use + * the cookies from the corresponding [BrowserContext]. This means that if you log in using this API, your e2e test will be + * logged in and vice versa. + */ +export interface ApiRequestContext { + /** + * All responses received through + * [apiRequestContext.fetch(urlOrRequest[, options])](https://playwright.dev/docs/api/class-apirequestcontext#api-request-context-fetch), + * [apiRequestContext.get(url[, options])](https://playwright.dev/docs/api/class-apirequestcontext#api-request-context-get), + * [apiRequestContext.post(url[, options])](https://playwright.dev/docs/api/class-apirequestcontext#api-request-context-post) + * and other methods are stored in the memory, so that you can later call + * [apiResponse.body()](https://playwright.dev/docs/api/class-apiresponse#api-response-body). This method discards all + * stored responses, and makes [apiResponse.body()](https://playwright.dev/docs/api/class-apiresponse#api-response-body) + * throw "Response disposed" error. + */ + dispose(): Promise; + + /** + * Sends HTTP(S) fetch and returns its response. The method will populate fetch cookies from the context and update context + * cookies from the response. The method will automatically follow redirects. + * @param urlOrRequest Target URL or Request to get all fetch parameters from. + * @param options + */ + fetch(urlOrRequest: string|Request, options?: { + /** + * 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. + */ + data?: string|Buffer|Serializable; + + /** + * Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes. + */ + failOnStatusCode?: 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. + */ + form?: { [key: string]: string|number|boolean; }; + + /** + * Allows to set HTTP headers. + */ + headers?: { [key: string]: string; }; + + /** + * Whether to ignore HTTPS errors when sending network requests. Defaults to `false`. + */ + ignoreHTTPSErrors?: boolean; + + /** + * If set changes the fetch method (e.g. PUT or POST). If not specified, GET method is used. + */ + method?: 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`](https://nodejs.org/api/fs.html#fs_class_fs_readstream) + * or as file-like object containing file name, mime-type and its content. + */ + multipart?: { [key: string]: string|number|boolean|ReadStream|{ + /** + * File name + */ + name: string; + + /** + * File type + */ + mimeType: string; + + /** + * File content + */ + buffer: Buffer; + }; }; + + /** + * Query parameters to be send with the URL. + */ + params?: { [key: string]: string; }; + + /** + * Request timeout in milliseconds. + */ + timeout?: number; + }): Promise; + + /** + * Sends HTTP(S) GET request and returns its response. The method will populate fetch cookies from the context and update + * context cookies from the response. The method will automatically follow redirects. + * @param url Target URL. + * @param options + */ + get(url: string, options?: { + /** + * Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes. + */ + failOnStatusCode?: boolean; + + /** + * Allows to set HTTP headers. + */ + headers?: { [key: string]: string; }; + + /** + * Whether to ignore HTTPS errors when sending network requests. Defaults to `false`. + */ + ignoreHTTPSErrors?: boolean; + + /** + * Query parameters to be send with the URL. + */ + params?: { [key: string]: string; }; + + /** + * Request timeout in milliseconds. + */ + timeout?: number; + }): Promise; + + /** + * Sends HTTP(S) fetch and returns its response. The method will populate fetch cookies from the context and update context + * cookies from the response. The method will automatically follow redirects. + * @param url Target URL. + * @param options + */ + post(url: string, options?: { + /** + * 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. + */ + data?: string|Buffer|Serializable; + + /** + * Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes. + */ + failOnStatusCode?: 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. + */ + form?: { [key: string]: string|number|boolean; }; + + /** + * Allows to set HTTP headers. + */ + headers?: { [key: string]: string; }; + + /** + * Whether to ignore HTTPS errors when sending network requests. Defaults to `false`. + */ + ignoreHTTPSErrors?: boolean; + + /** + * 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`](https://nodejs.org/api/fs.html#fs_class_fs_readstream) + * or as file-like object containing file name, mime-type and its content. + */ + multipart?: { [key: string]: string|number|boolean|ReadStream|{ + /** + * File name + */ + name: string; + + /** + * File type + */ + mimeType: string; + + /** + * File content + */ + buffer: Buffer; + }; }; + + /** + * Query parameters to be send with the URL. + */ + params?: { [key: string]: string; }; + + /** + * Request timeout in milliseconds. + */ + timeout?: number; + }): Promise; + + /** + * Returns storage state for this request context, contains current cookies and local storage snapshot if it was passed to + * the constructor. + * @param options + */ + storageState(options?: { + /** + * The file path to save the storage state to. If `path` is a relative path, then it is resolved relative to current + * working directory. If no path is provided, storage state is still returned, but won't be saved to the disk. + */ + path?: string; + }): Promise<{ + cookies: Array<{ + name: string; + + value: string; + + domain: string; + + path: string; + + /** + * Unix time in seconds. + */ + expires: number; + + httpOnly: boolean; + + secure: boolean; + + sameSite: "Strict"|"Lax"|"None"; + }>; + + origins: Array<{ + origin: string; + + localStorage: Array<{ + name: string; + + value: string; + }>; + }>; + }>; +} + +/** + * [ApiResponse] class represents responses received from + * [apiRequestContext.fetch(urlOrRequest[, options])](https://playwright.dev/docs/api/class-apirequestcontext#api-request-context-fetch). + */ +export interface ApiResponse { + /** + * Returns the buffer with response body. + */ + body(): Promise; + + /** + * Disposes the body of this response. If not called then the body will stay in memory until the context closes. + */ + dispose(): Promise; + + /** + * An object with all the response HTTP headers associated with this response. + */ + headers(): { [key: string]: string; }; + + /** + * An array with all the request HTTP headers associated with this response. Header names are not lower-cased. Headers with + * multiple entries, such as `Set-Cookie`, appear in the array multiple times. + */ + headersArray(): Array<{ + /** + * Name of the header. + */ + name: string; + + /** + * Value of the header. + */ + value: string; + }>; + + /** + * Returns the JSON representation of response body. + * + * This method will throw if the response body is not parsable via `JSON.parse`. + */ + json(): Promise; + + /** + * Contains a boolean stating whether the response was successful (status in the range 200-299) or not. + */ + ok(): boolean; + + /** + * Contains the status code of the response (e.g., 200 for a success). + */ + status(): number; + + /** + * Contains the status text of the response (e.g. usually an "OK" for a success). + */ + statusText(): string; + + /** + * Returns the text representation of response body. + */ + text(): Promise; + + /** + * Contains the URL of the response. + */ + url(): string; +} + /** * - extends: [EventEmitter] * @@ -12655,318 +13090,6 @@ export interface Electron { }): Promise; } -/** - * This API is used for Web API testing. You can use it to trigger API endpoints, configure micro-services, prepare - * environment or the service to your e2e test. When used on [Page] or a [BrowserContext], this API will automatically use - * the cookies from the corresponding [BrowserContext]. This means that if you log in using this API, your e2e test will be - * logged in and vice versa. - */ -export interface FetchRequest { - /** - * All responses received through - * [fetchRequest.fetch(urlOrRequest[, options])](https://playwright.dev/docs/api/class-fetchrequest#fetch-request-fetch), - * [fetchRequest.get(url[, options])](https://playwright.dev/docs/api/class-fetchrequest#fetch-request-get), - * [fetchRequest.post(url[, options])](https://playwright.dev/docs/api/class-fetchrequest#fetch-request-post) and other - * methods are stored in the memory, so that you can later call - * [fetchResponse.body()](https://playwright.dev/docs/api/class-fetchresponse#fetch-response-body). This method discards - * all stored responses, and makes - * [fetchResponse.body()](https://playwright.dev/docs/api/class-fetchresponse#fetch-response-body) throw "Response - * disposed" error. - */ - dispose(): Promise; - - /** - * Sends HTTP(S) fetch and returns its response. The method will populate fetch cookies from the context and update context - * cookies from the response. The method will automatically follow redirects. - * @param urlOrRequest Target URL or Request to get all fetch parameters from. - * @param options - */ - fetch(urlOrRequest: string|Request, options?: { - /** - * 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. - */ - data?: string|Buffer|Serializable; - - /** - * Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes. - */ - failOnStatusCode?: 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. - */ - form?: { [key: string]: string|number|boolean; }; - - /** - * Allows to set HTTP headers. - */ - headers?: { [key: string]: string; }; - - /** - * Whether to ignore HTTPS errors when sending network requests. Defaults to `false`. - */ - ignoreHTTPSErrors?: boolean; - - /** - * If set changes the fetch method (e.g. PUT or POST). If not specified, GET method is used. - */ - method?: 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`](https://nodejs.org/api/fs.html#fs_class_fs_readstream) - * or as file-like object containing file name, mime-type and its content. - */ - multipart?: { [key: string]: string|number|boolean|ReadStream|{ - /** - * File name - */ - name: string; - - /** - * File type - */ - mimeType: string; - - /** - * File content - */ - buffer: Buffer; - }; }; - - /** - * Query parameters to be send with the URL. - */ - params?: { [key: string]: string; }; - - /** - * Request timeout in milliseconds. - */ - timeout?: number; - }): Promise; - - /** - * Sends HTTP(S) GET request and returns its response. The method will populate fetch cookies from the context and update - * context cookies from the response. The method will automatically follow redirects. - * @param url Target URL. - * @param options - */ - get(url: string, options?: { - /** - * Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes. - */ - failOnStatusCode?: boolean; - - /** - * Allows to set HTTP headers. - */ - headers?: { [key: string]: string; }; - - /** - * Whether to ignore HTTPS errors when sending network requests. Defaults to `false`. - */ - ignoreHTTPSErrors?: boolean; - - /** - * Query parameters to be send with the URL. - */ - params?: { [key: string]: string; }; - - /** - * Request timeout in milliseconds. - */ - timeout?: number; - }): Promise; - - /** - * Sends HTTP(S) fetch and returns its response. The method will populate fetch cookies from the context and update context - * cookies from the response. The method will automatically follow redirects. - * @param url Target URL. - * @param options - */ - post(url: string, options?: { - /** - * 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. - */ - data?: string|Buffer|Serializable; - - /** - * Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes. - */ - failOnStatusCode?: 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. - */ - form?: { [key: string]: string|number|boolean; }; - - /** - * Allows to set HTTP headers. - */ - headers?: { [key: string]: string; }; - - /** - * Whether to ignore HTTPS errors when sending network requests. Defaults to `false`. - */ - ignoreHTTPSErrors?: boolean; - - /** - * 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`](https://nodejs.org/api/fs.html#fs_class_fs_readstream) - * or as file-like object containing file name, mime-type and its content. - */ - multipart?: { [key: string]: string|number|boolean|ReadStream|{ - /** - * File name - */ - name: string; - - /** - * File type - */ - mimeType: string; - - /** - * File content - */ - buffer: Buffer; - }; }; - - /** - * Query parameters to be send with the URL. - */ - params?: { [key: string]: string; }; - - /** - * Request timeout in milliseconds. - */ - timeout?: number; - }): Promise; - - /** - * Returns storage state for this request context, contains current cookies and local storage snapshot if it was passed to - * the constructor. - * @param options - */ - storageState(options?: { - /** - * The file path to save the storage state to. If `path` is a relative path, then it is resolved relative to current - * working directory. If no path is provided, storage state is still returned, but won't be saved to the disk. - */ - path?: string; - }): Promise<{ - cookies: Array<{ - name: string; - - value: string; - - domain: string; - - path: string; - - /** - * Unix time in seconds. - */ - expires: number; - - httpOnly: boolean; - - secure: boolean; - - sameSite: "Strict"|"Lax"|"None"; - }>; - - origins: Array<{ - origin: string; - - localStorage: Array<{ - name: string; - - value: string; - }>; - }>; - }>; -} - -/** - * [FetchResponse] class represents responses received from - * [fetchRequest.fetch(urlOrRequest[, options])](https://playwright.dev/docs/api/class-fetchrequest#fetch-request-fetch). - */ -export interface FetchResponse { - /** - * Returns the buffer with response body. - */ - body(): Promise; - - /** - * Disposes the body of this response. If not called then the body will stay in memory until the context closes. - */ - dispose(): Promise; - - /** - * An object with all the response HTTP headers associated with this response. - */ - headers(): { [key: string]: string; }; - - /** - * An array with all the request HTTP headers associated with this response. Header names are not lower-cased. Headers with - * multiple entries, such as `Set-Cookie`, appear in the array multiple times. - */ - headersArray(): Array<{ - /** - * Name of the header. - */ - name: string; - - /** - * Value of the header. - */ - value: string; - }>; - - /** - * Returns the JSON representation of response body. - * - * This method will throw if the response body is not parsable via `JSON.parse`. - */ - json(): Promise; - - /** - * Contains a boolean stating whether the response was successful (status in the range 200-299) or not. - */ - ok(): boolean; - - /** - * Contains the status code of the response (e.g., 200 for a success). - */ - status(): number; - - /** - * Contains the status text of the response (e.g. usually an "OK" for a success). - */ - statusText(): string; - - /** - * Returns the text representation of response body. - */ - text(): Promise; - - /** - * Contains the URL of the response. - */ - url(): string; -} - /** * [FileChooser] objects are dispatched by the page in the * [page.on('filechooser')](https://playwright.dev/docs/api/class-page#page-event-file-chooser) event. @@ -13382,122 +13505,9 @@ export const chromium: BrowserType; export const firefox: BrowserType; /** - * **experimental** Creates new instances of [FetchRequest]. - * @param options + * Exposes API that can be used for the Web API testing. */ -export const newRequest: (options?: { - /** - * When using [fetchRequest.get(url[, options])](https://playwright.dev/docs/api/class-fetchrequest#fetch-request-get), - * [fetchRequest.post(url[, options])](https://playwright.dev/docs/api/class-fetchrequest#fetch-request-post), - * [fetchRequest.fetch(urlOrRequest[, options])](https://playwright.dev/docs/api/class-fetchrequest#fetch-request-fetch) it - * takes the base URL in consideration by using the [`URL()`](https://developer.mozilla.org/en-US/docs/Web/API/URL/URL) - * constructor for building the corresponding URL. Examples: - * - baseURL: `http://localhost:3000` and sending rquest to `/bar.html` results in `http://localhost:3000/bar.html` - * - baseURL: `http://localhost:3000/foo/` and sending rquest to `./bar.html` results in - * `http://localhost:3000/foo/bar.html` - */ - baseURL?: string; - - /** - * An object containing additional HTTP headers to be sent with every request. - */ - extraHTTPHeaders?: { [key: string]: string; }; - - /** - * Credentials for [HTTP authentication](https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication). - */ - httpCredentials?: { - username: string; - - password: string; - }; - - /** - * Whether to ignore HTTPS errors when sending network requests. Defaults to `false`. - */ - ignoreHTTPSErrors?: boolean; - - /** - * Network proxy settings. - */ - proxy?: { - /** - * Proxy to be used for all requests. HTTP and SOCKS proxies are supported, for example `http://myproxy.com:3128` or - * `socks5://myproxy.com:3128`. Short form `myproxy.com:3128` is considered an HTTP proxy. - */ - server: string; - - /** - * Optional coma-separated domains to bypass proxy, for example `".com, chromium.org, .domain.com"`. - */ - bypass?: string; - - /** - * Optional username to use if HTTP proxy requires authentication. - */ - username?: string; - - /** - * Optional password to use if HTTP proxy requires authentication. - */ - password?: string; - }; - - /** - * Populates context with given storage state. This option can be used to initialize context with logged-in information - * obtained via - * [browserContext.storageState([options])](https://playwright.dev/docs/api/class-browsercontext#browser-context-storage-state) - * or - * [fetchRequest.storageState([options])](https://playwright.dev/docs/api/class-fetchrequest#fetch-request-storage-state). - * Either a path to the file with saved storage, or the value returned by one of - * [browserContext.storageState([options])](https://playwright.dev/docs/api/class-browsercontext#browser-context-storage-state) - * or - * [fetchRequest.storageState([options])](https://playwright.dev/docs/api/class-fetchrequest#fetch-request-storage-state) - * methods. - */ - storageState?: string|{ - cookies: Array<{ - name: string; - - value: string; - - domain: string; - - path: string; - - /** - * Unix time in seconds. - */ - expires: number; - - httpOnly: boolean; - - secure: boolean; - - sameSite: "Strict"|"Lax"|"None"; - }>; - - origins: Array<{ - origin: string; - - localStorage: Array<{ - name: string; - - value: string; - }>; - }>; - }; - - /** - * Maximum time in milliseconds to wait for the response. Defaults to `30000` (30 seconds). Pass `0` to disable timeout. - */ - timeout?: number; - - /** - * Specific user agent to use in this context. - */ - userAgent?: string; -}) => Promise; +export const request: ApiRequest; /** * Selectors can be used to install custom selector engines. See [Working with selectors](https://playwright.dev/docs/selectors) for more @@ -14021,10 +14031,10 @@ export interface Route { path?: string; /** - * [FetchResponse] to fulfill route's request with. Individual fields of the response (such as headers) can be overridden + * [ApiResponse] to fulfill route's request with. Individual fields of the response (such as headers) can be overridden * using fulfill options. */ - response?: FetchResponse; + response?: ApiResponse; /** * Response status code, defaults to `200`.