Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
chore(mcp): debug_test returns pre-existing console/network (#37519)
Dmitry Gozman796cf89ed4a2
diff --git a/packages/playwright/src/mcp/browser/tab.ts b/packages/playwright/src/mcp/browser/tab.tsindex b0190b6c24..44a855b444 100644--- a/packages/playwright/src/mcp/browser/tab.ts+++ b/packages/playwright/src/mcp/browser/tab.ts@@ -91,12 +91,30 @@ export class Tab extends EventEmitter<TabEventsInterface> {page.setDefaultNavigationTimeout(this.context.config.timeouts.navigation);page.setDefaultTimeout(this.context.config.timeouts.action);(page as any)[tabSymbol] = this;+ void this._initialize();}static forPage(page: playwright.Page): Tab | undefined {return (page as any)[tabSymbol];}+ private async _initialize() {+ const messages = await this.page.consoleMessages().catch(() => []);+ for (const message of messages)+ this._handleConsoleMessage(messageToConsoleMessage(message));+ const errors = await this.page.pageErrors().catch(() => []);+ for (const error of errors)+ this._handleConsoleMessage(pageErrorToConsoleMessage(error));+ const requests = await this.page.requests().catch(() => []);+ for (const request of requests) {+ this._requests.set(request, null);+ void request.response().catch(() => null).then(response => {+ if (response)+ this._requests.set(request, response);+ });+ }+ }+modalStates(): ModalState[] {return this._modalStates;}diff --git a/tests/mcp/test-tools.spec.ts b/tests/mcp/test-tools.spec.tsindex b13432d81f..76151f87aa 100644--- a/tests/mcp/test-tools.spec.ts+++ b/tests/mcp/test-tools.spec.ts@@ -268,18 +268,38 @@ Try recovering from the error prior to continuing`);})).toHaveTextResponse(expect.stringContaining(`1) [id=<ID>] a.test.ts:3:11 › fail`));});-test('test_debug / browser_snapshot', async ({ startClient }) => {- const { client, id } = await prepareDebugTest(startClient);+test('test_debug (browser_snapshot/network/console)', async ({ startClient, server }) => {+ const { client, id } = await prepareDebugTest(startClient, `+ import { test, expect } from '@playwright/test';+ test('fail', async ({ page }) => {+ await page.goto(${JSON.stringify(server.HELLO_WORLD)});+ await page.evaluate(() => {+ console.log('hello from console');+ setTimeout(() => { throw new Error('error from page'); }, 0);+ });+ await expect(page.getByRole('button', { name: 'Missing' })).toBeVisible({ timeout: 1000 });+ });+ `);await client.callTool({name: 'test_debug',arguments: {test: { id, title: 'fail' },},});+ await expect.poll(() => client.callTool({+ name: 'browser_network_requests',+ })).toHaveResponse({+ result: expect.stringContaining(`[GET] ${server.HELLO_WORLD} => [200] OK`),+ });+ expect(await client.callTool({+ name: 'browser_console_messages',+ })).toHaveResponse({+ result: expect.stringMatching(/\[LOG\] hello from console.*\nError: error from page/),+ });expect(await client.callTool({name: 'browser_snapshot',})).toHaveResponse({- pageState: expect.stringContaining(`- button \"Submit\" [ref=e2]`),+ pageState: expect.stringContaining(`generic [active] [ref=e1]: Hello, world!`),});});@@ -522,9 +542,9 @@ test('test_setup_page without location respects testsDir', async ({ startClientexpect(fs.existsSync(test.info().outputPath('tests', 'default.seed.spec.ts'))).toBe(true);});-async function prepareDebugTest(startClient: StartClient) {+async function prepareDebugTest(startClient: StartClient, testFile?: string) {await writeFiles({- 'a.test.ts': `+ 'a.test.ts': testFile || `import { test, expect } from '@playwright/test';test('fail', async ({ page }) => {await page.setContent('<button>Submit</button>');