mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
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());
|
||||
},
|
||||
|
||||
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[]) {
|
||||
|
||||
@ -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');
|
||||
|
||||
@ -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
21
types/test.d.ts
vendored
@ -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>;
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
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>;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user