feat(request): add global request fixture (#9332)
This commit is contained in:
parent
9b7e02b88b
commit
bc71d20d0f
|
|
@ -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'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// ...
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
|
||||||
|
|
@ -456,7 +456,15 @@ export const test = _baseTest.extend<TestFixtures, WorkerAndFileFixtures>({
|
||||||
}
|
}
|
||||||
await use(await context.newPage());
|
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;
|
export default test;
|
||||||
|
|
||||||
function formatPendingCalls(calls: ParsedStackTrace[]) {
|
function formatPendingCalls(calls: ParsedStackTrace[]) {
|
||||||
|
|
|
||||||
|
|
@ -130,7 +130,7 @@ const baseFixtures: Fixtures<{}, BaseOptions & BaseFixtures> = {
|
||||||
type ServerOptions = {
|
type ServerOptions = {
|
||||||
loopback?: string;
|
loopback?: string;
|
||||||
};
|
};
|
||||||
type ServerFixtures = {
|
export type ServerFixtures = {
|
||||||
server: TestServer;
|
server: TestServer;
|
||||||
httpsServer: TestServer;
|
httpsServer: TestServer;
|
||||||
socksPort: number;
|
socksPort: number;
|
||||||
|
|
@ -138,8 +138,8 @@ type ServerFixtures = {
|
||||||
asset: (p: string) => string;
|
asset: (p: string) => string;
|
||||||
};
|
};
|
||||||
|
|
||||||
type ServersInternal = ServerFixtures & { socksServer: socks.SocksServer };
|
export type ServersInternal = ServerFixtures & { socksServer: socks.SocksServer };
|
||||||
const serverFixtures: Fixtures<ServerFixtures, ServerOptions & { __servers: ServersInternal }> = {
|
export const serverFixtures: Fixtures<ServerFixtures, ServerOptions & { __servers: ServersInternal }> = {
|
||||||
loopback: [ undefined, { scope: 'worker' } ],
|
loopback: [ undefined, { scope: 'worker' } ],
|
||||||
__servers: [ async ({ loopback }, run, workerInfo) => {
|
__servers: [ async ({ loopback }, run, workerInfo) => {
|
||||||
const assetsPath = path.join(__dirname, '..', 'assets');
|
const assetsPath = path.join(__dirname, '..', 'assets');
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ import type { JSONReport, JSONReportSuite } from '../../src/test/reporters/json'
|
||||||
import rimraf from 'rimraf';
|
import rimraf from 'rimraf';
|
||||||
import { promisify } from 'util';
|
import { promisify } from 'util';
|
||||||
import * as url from 'url';
|
import * as url from 'url';
|
||||||
|
import { serverFixtures, ServerFixtures } from '../config/baseTest';
|
||||||
|
|
||||||
const removeFolderAsync = promisify(rimraf);
|
const removeFolderAsync = promisify(rimraf);
|
||||||
|
|
||||||
|
|
@ -194,7 +195,7 @@ type Fixtures = {
|
||||||
};
|
};
|
||||||
|
|
||||||
const common = base.extend<CommonFixtures>(commonFixtures as any);
|
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) => {
|
writeFiles: async ({}, use, testInfo) => {
|
||||||
await use(files => writeFiles(testInfo, files));
|
await use(files => writeFiles(testInfo, files));
|
||||||
},
|
},
|
||||||
|
|
|
||||||
21
types/test.d.ts
vendored
21
types/test.d.ts
vendored
|
|
@ -15,7 +15,7 @@
|
||||||
* limitations under the License.
|
* 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';
|
import type { Expect } from './testExpect';
|
||||||
|
|
||||||
export type { Expect } from './testExpect';
|
export type { Expect } from './testExpect';
|
||||||
|
|
@ -2656,6 +2656,25 @@ export interface PlaywrightTestArgs {
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
page: Page;
|
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>;
|
export type PlaywrightTestProject<TestArgs = {}, WorkerArgs = {}> = Project<PlaywrightTestOptions & TestArgs, PlaywrightWorkerOptions & WorkerArgs>;
|
||||||
|
|
|
||||||
3
utils/generate_types/overrides-test.d.ts
vendored
3
utils/generate_types/overrides-test.d.ts
vendored
|
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* 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';
|
import type { Expect } from './testExpect';
|
||||||
|
|
||||||
export type { Expect } from './testExpect';
|
export type { Expect } from './testExpect';
|
||||||
|
|
@ -333,6 +333,7 @@ export interface PlaywrightWorkerArgs {
|
||||||
export interface PlaywrightTestArgs {
|
export interface PlaywrightTestArgs {
|
||||||
context: BrowserContext;
|
context: BrowserContext;
|
||||||
page: Page;
|
page: Page;
|
||||||
|
request: ApiRequestContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type PlaywrightTestProject<TestArgs = {}, WorkerArgs = {}> = Project<PlaywrightTestOptions & TestArgs, PlaywrightWorkerOptions & WorkerArgs>;
|
export type PlaywrightTestProject<TestArgs = {}, WorkerArgs = {}> = Project<PlaywrightTestOptions & TestArgs, PlaywrightWorkerOptions & WorkerArgs>;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue