From bc71d20d0fb1883880916ca14c48926bab50ce48 Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Wed, 6 Oct 2021 09:09:27 -0800 Subject: [PATCH] feat(request): add global request fixture (#9332) --- docs/src/test-api/class-fixtures.md | 32 +++++++++++++++++++ src/test/index.ts | 8 +++++ tests/config/baseTest.ts | 6 ++-- .../playwright-test-fixtures.ts | 3 +- types/test.d.ts | 21 +++++++++++- utils/generate_types/overrides-test.d.ts | 3 +- 6 files changed, 67 insertions(+), 6 deletions(-) diff --git a/docs/src/test-api/class-fixtures.md b/docs/src/test-api/class-fixtures.md index f476a08ada..0348ec3922 100644 --- a/docs/src/test-api/class-fixtures.md +++ b/docs/src/test-api/class-fixtures.md @@ -90,3 +90,35 @@ test('basic test', async ({ page }) => { // ... }); ``` + +## property: Fixtures.request +- type: <[ApiRequestContext]> + +Isolated [ApiRequestContext] instance for each test. + +```js js-flavor=js +const { test, expect } = require('@playwright/test'); + +test('basic test', async ({ request }) => { + await request.post('/signin', { + data: { + username: 'user', + password: 'password' + } + }); +}); +``` + +```js js-flavor=ts +import { test, expect } from '@playwright/test'; + +test('basic test', async ({ request }) => { + await request.post('/signin', { + data: { + username: 'user', + password: 'password' + } + }); + // ... +}); +``` diff --git a/src/test/index.ts b/src/test/index.ts index b861f53527..93d3573f73 100644 --- a/src/test/index.ts +++ b/src/test/index.ts @@ -456,7 +456,15 @@ export const test = _baseTest.extend({ } await use(await context.newPage()); }, + + request: async ({ playwright, _combinedContextOptions }, use) => { + const request = await playwright.request.newContext(_combinedContextOptions); + await use(request); + await request.dispose(); + } + }); + export default test; function formatPendingCalls(calls: ParsedStackTrace[]) { diff --git a/tests/config/baseTest.ts b/tests/config/baseTest.ts index bb661f44a1..b2bdf210f3 100644 --- a/tests/config/baseTest.ts +++ b/tests/config/baseTest.ts @@ -130,7 +130,7 @@ const baseFixtures: Fixtures<{}, BaseOptions & BaseFixtures> = { type ServerOptions = { loopback?: string; }; -type ServerFixtures = { +export type ServerFixtures = { server: TestServer; httpsServer: TestServer; socksPort: number; @@ -138,8 +138,8 @@ type ServerFixtures = { asset: (p: string) => string; }; -type ServersInternal = ServerFixtures & { socksServer: socks.SocksServer }; -const serverFixtures: Fixtures = { +export type ServersInternal = ServerFixtures & { socksServer: socks.SocksServer }; +export const serverFixtures: Fixtures = { loopback: [ undefined, { scope: 'worker' } ], __servers: [ async ({ loopback }, run, workerInfo) => { const assetsPath = path.join(__dirname, '..', 'assets'); diff --git a/tests/playwright-test/playwright-test-fixtures.ts b/tests/playwright-test/playwright-test-fixtures.ts index 980a2405b1..3a607843a9 100644 --- a/tests/playwright-test/playwright-test-fixtures.ts +++ b/tests/playwright-test/playwright-test-fixtures.ts @@ -23,6 +23,7 @@ import type { JSONReport, JSONReportSuite } from '../../src/test/reporters/json' import rimraf from 'rimraf'; import { promisify } from 'util'; import * as url from 'url'; +import { serverFixtures, ServerFixtures } from '../config/baseTest'; const removeFolderAsync = promisify(rimraf); @@ -194,7 +195,7 @@ type Fixtures = { }; const common = base.extend(commonFixtures as any); -export const test = common.extend({ +export const test = common.extend(serverFixtures as any).extend({ writeFiles: async ({}, use, testInfo) => { await use(files => writeFiles(testInfo, files)); }, diff --git a/types/test.d.ts b/types/test.d.ts index 8f2168f824..c08f8f12d2 100644 --- a/types/test.d.ts +++ b/types/test.d.ts @@ -15,7 +15,7 @@ * limitations under the License. */ -import type { Browser, BrowserContext, BrowserContextOptions, Page, LaunchOptions, ViewportSize, Geolocation, HTTPCredentials } from './types'; +import type { ApiRequestContext, Browser, BrowserContext, BrowserContextOptions, Page, LaunchOptions, ViewportSize, Geolocation, HTTPCredentials } from './types'; import type { Expect } from './testExpect'; export type { Expect } from './testExpect'; @@ -2656,6 +2656,25 @@ export interface PlaywrightTestArgs { * */ page: Page; + /** + * Isolated [ApiRequestContext] instance for each test. + * + * ```ts + * import { test, expect } from '@playwright/test'; + * + * test('basic test', async ({ request }) => { + * await request.post('/signin', { + * data: { + * username: 'user', + * password: 'password' + * } + * }); + * // ... + * }); + * ``` + * + */ + request: ApiRequestContext; } export type PlaywrightTestProject = Project; diff --git a/utils/generate_types/overrides-test.d.ts b/utils/generate_types/overrides-test.d.ts index 601edd9898..3fd3ecf708 100644 --- a/utils/generate_types/overrides-test.d.ts +++ b/utils/generate_types/overrides-test.d.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import type { Browser, BrowserContext, BrowserContextOptions, Page, LaunchOptions, ViewportSize, Geolocation, HTTPCredentials } from './types'; +import type { ApiRequestContext, Browser, BrowserContext, BrowserContextOptions, Page, LaunchOptions, ViewportSize, Geolocation, HTTPCredentials } from './types'; import type { Expect } from './testExpect'; export type { Expect } from './testExpect'; @@ -333,6 +333,7 @@ export interface PlaywrightWorkerArgs { export interface PlaywrightTestArgs { context: BrowserContext; page: Page; + request: ApiRequestContext; } export type PlaywrightTestProject = Project;