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

This commit is contained in:
Pavel Feldman 2021-10-06 09:09:27 -08:00 committed by GitHub
parent 9b7e02b88b
commit bc71d20d0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 67 additions and 6 deletions

View File

@ -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'
}
});
// ...
});
```

View File

@ -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[]) {

View File

@ -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');

View File

@ -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));
},

21
types/test.d.ts vendored
View File

@ -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>;

View File

@ -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>;