Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

feat(request): add global request fixture (#9332)

Pavel Feldmanbc71d20d0fb1
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<TestFixtures, WorkerAndFileFixtures>({
}
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<ServerFixtures, ServerOptions & { __servers: ServersInternal }> = {
+export type ServersInternal = ServerFixtures & { socksServer: socks.SocksServer };
+export const serverFixtures: Fixtures<ServerFixtures, ServerOptions & { __servers: ServersInternal }> = {
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>(commonFixtures as any);
-export const test = common.extend<Fixtures>({
+export const test = common.extend<ServerFixtures>(serverFixtures as any).extend<Fixtures>({
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<TestArgs = {}, WorkerArgs = {}> = Project<PlaywrightTestOptions & TestArgs, PlaywrightWorkerOptions & WorkerArgs>;
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<TestArgs = {}, WorkerArgs = {}> = Project<PlaywrightTestOptions & TestArgs, PlaywrightWorkerOptions & WorkerArgs>;