fix tests, don't emit on page

This commit is contained in:
Simon Knott 2025-01-27 12:50:33 +01:00
parent b2722c653c
commit 26523384f6
No known key found for this signature in database
GPG key ID: 8CEDC00028084AEC
5 changed files with 14 additions and 31 deletions

View file

@ -69,7 +69,7 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel>
_closeWasCalled = false; _closeWasCalled = false;
private _closeReason: string | undefined; private _closeReason: string | undefined;
private _harRouters: HarRouter[] = []; private _harRouters: HarRouter[] = [];
private _mockingProxy?: MockingProxy; _mockingProxy?: MockingProxy;
static from(context: channels.BrowserContextChannel): BrowserContext { static from(context: channels.BrowserContextChannel): BrowserContext {
return (context as any)._object; return (context as any)._object;

View file

@ -17,6 +17,7 @@ import * as network from './network';
import type * as channels from '@protocol/channels'; import type * as channels from '@protocol/channels';
import { ChannelOwner } from './channelOwner'; import { ChannelOwner } from './channelOwner';
import { APIRequestContext } from './fetch'; import { APIRequestContext } from './fetch';
import { Events } from './events';
export class MockingProxy extends ChannelOwner<channels.MockingProxyChannel> { export class MockingProxy extends ChannelOwner<channels.MockingProxyChannel> {
private _port: number; private _port: number;
@ -30,7 +31,7 @@ export class MockingProxy extends ChannelOwner<channels.MockingProxyChannel> {
this._channel.on('route', (params: channels.MockingProxyRouteEvent) => { this._channel.on('route', (params: channels.MockingProxyRouteEvent) => {
const route = network.Route.from(params.route); const route = network.Route.from(params.route);
route._context = requestContext; route._context = requestContext;
this.emit('route', route); this.emit(Events.MockingProxy.Route, route);
}); });
} }

View file

@ -568,8 +568,6 @@ export class Page extends ChannelOwner<channels.PageChannel> implements api.Page
private async _updateInterceptionPatterns() { private async _updateInterceptionPatterns() {
const patterns = RouteHandler.prepareInterceptionPatterns(this._routes); const patterns = RouteHandler.prepareInterceptionPatterns(this._routes);
await this._channel.setNetworkInterceptionPatterns({ patterns }); await this._channel.setNetworkInterceptionPatterns({ patterns });
for (const proxy of this._browserContext._mockingProxies)
await proxy.setInterceptionPatterns({ patterns });
} }
private async _updateWebSocketInterceptionPatterns() { private async _updateWebSocketInterceptionPatterns() {

View file

@ -239,7 +239,7 @@ const playwrightFixtures: Fixtures<TestFixtures, WorkerFixtures> = ({
if (serviceWorkers !== undefined) if (serviceWorkers !== undefined)
options.serviceWorkers = serviceWorkers; options.serviceWorkers = serviceWorkers;
if (_mockingProxy) if (_mockingProxy)
options.extraHTTPHeaders = { ...options.extraHTTPHeaders, 'x-playwright-proxy-port': String(_mockingProxy.port()) }; options.extraHTTPHeaders = { ...options.extraHTTPHeaders, 'x-playwright-proxy': encodeURIComponent(`http://localhost:${_mockingProxy.port()}/`) };
await use({ await use({
...contextOptions, ...contextOptions,
...options, ...options,

View file

@ -18,7 +18,7 @@ import { test, expect } from './playwright-test-fixtures';
test('inject mode', async ({ runInlineTest, server }) => { test('inject mode', async ({ runInlineTest, server }) => {
server.setRoute('/page', (req, res) => { server.setRoute('/page', (req, res) => {
res.end(req.headers['x-pw-proxy-port'] ?? 'no port given'); res.end(req.headers['x-playwright-proxy'] ? 'proxy url injected' : 'proxy url missing');
}); });
const result = await runInlineTest({ const result = await runInlineTest({
'playwright.config.ts': ` 'playwright.config.ts': `
@ -30,12 +30,11 @@ test('inject mode', async ({ runInlineTest, server }) => {
`, `,
'a.test.ts': ` 'a.test.ts': `
import { test, expect } from '@playwright/test'; import { test, expect } from '@playwright/test';
test('foo', async ({ server, page, request }) => { test('foo', async ({ page, request }) => {
await page.goto('${server.PREFIX}/page'); await page.goto('${server.PREFIX}/page');
expect(await page.textContent('body')).toEqual('' + server.port()); expect(await page.textContent('body')).toEqual('proxy url injected');
const response = await request.get('${server.PREFIX}/page'); const response = await request.get('${server.PREFIX}/page');
expect(await response.text()).toEqual('' + server.port()); expect(await response.text()).toEqual('proxy url injected');
}); });
` `
}, { workers: 1 }); }, { workers: 1 });
@ -55,8 +54,7 @@ test('throws on fixed mocking proxy port and parallel workers', async ({ runInli
`, `,
'a.test.ts': ` 'a.test.ts': `
import { test, expect } from '@playwright/test'; import { test, expect } from '@playwright/test';
test('foo', async ({ server }) => { test('foo', async ({}) => {});
});
` `
}, { workers: 2 }); }, { workers: 2 });
@ -64,26 +62,12 @@ test('throws on fixed mocking proxy port and parallel workers', async ({ runInli
expect(result.output).toContain('Cannot share mocking proxy between multiple workers.'); expect(result.output).toContain('Cannot share mocking proxy between multiple workers.');
}); });
test('throws on missing config', async ({ runInlineTest }) => {
const result = await runInlineTest({
'a.test.ts': `
import { test, expect } from '@playwright/test';
test('foo', async ({ server }) => {
});
`
}, { workers: 2 });
expect(result.exitCode).toBe(1);
expect(result.output).toContain(`The 'server' fixture is only available when 'mockingProxy' is enabled.`);
});
test('routes are reset between tests', async ({ runInlineTest, server, request }) => { test('routes are reset between tests', async ({ runInlineTest, server, request }) => {
server.setRoute('/fallback', async (req, res) => { server.setRoute('/fallback', async (req, res) => {
res.end('fallback'); res.end('fallback');
}); });
server.setRoute('/page', async (req, res) => { server.setRoute('/page', async (req, res) => {
const port = req.headers['x-pw-proxy-port']; const proxyURL = decodeURIComponent((req.headers['x-playwright-proxy'] as string) ?? '');
const proxyURL = `http://localhost:${port}/`;
const response = await request.get(proxyURL + server.PREFIX + '/fallback'); const response = await request.get(proxyURL + server.PREFIX + '/fallback');
res.end(await response.body()); res.end(await response.body());
}); });
@ -97,13 +81,13 @@ test('routes are reset between tests', async ({ runInlineTest, server, request }
`, `,
'a.test.ts': ` 'a.test.ts': `
import { test, expect } from '@playwright/test'; import { test, expect } from '@playwright/test';
test('first', async ({ server, page, request }) => { test('first', async ({ page, request, context }) => {
await server.route('${server.PREFIX}/fallback', route => route.fulfill({ body: 'first' })); await context.route('${server.PREFIX}/fallback', route => route.fulfill({ body: 'first' }));
await page.goto('${server.PREFIX}/page'); await page.goto('${server.PREFIX}/page');
expect(await page.textContent('body')).toEqual('first'); expect(await page.textContent('body')).toEqual('first');
}); });
test('second', async ({ server, page, request }) => { test('second', async ({ page, request, context }) => {
await server.route('${server.PREFIX}/fallback', route => route.fallback()); await context.route('${server.PREFIX}/fallback', route => route.fallback());
await page.goto('${server.PREFIX}/page'); await page.goto('${server.PREFIX}/page');
expect(await page.textContent('body')).toEqual('fallback'); expect(await page.textContent('body')).toEqual('fallback');
}); });