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
fix(mcp): do not crash when download fails after browser close (#42922)
Yury Semikhatsky48844a570e64
diff --git a/packages/playwright-core/src/tools/backend/tab.ts b/packages/playwright-core/src/tools/backend/tab.tsindex de61c443c4..da326c2721 100644--- a/packages/playwright-core/src/tools/backend/tab.ts+++ b/packages/playwright-core/src/tools/backend/tab.ts@@ -137,7 +137,7 @@ export class Tab extends EventEmitter<TabEventsInterface> {eventsHelper.addEventListener(p, 'dialog', dialog => this._dialogShown(dialog)),eventsHelper.addEventListener(p, 'dialogclosed', dialog => this._dialogClosed(dialog)),eventsHelper.addEventListener(p, 'download', download => {- void this._downloadStarted(download);+ this._downloadStarted(download).catch(e => debug('pw:tools:error')(e));}),];// eslint-disable-next-line no-restricted-syntaxdiff --git a/tests/mcp/files.spec.ts b/tests/mcp/files.spec.tsindex 8cc7ddaf90..d08e3c8904 100644--- a/tests/mcp/files.spec.ts+++ b/tests/mcp/files.spec.ts@@ -198,6 +198,49 @@ test('navigating to download link emits download', async ({ startClient, server,});});+test('closing browser during download does not crash the server', async ({ startClient, server }, testInfo) => {+ // Persistent Chromium on macOS does not quit while a download is in progress.+ const { client } = await startClient({+ config: { outputDir: testInfo.outputPath('output'), browser: { isolated: true } },+ });++ server.setContent('/', `<a href="/download" download="test.txt">Download</a>`, 'text/html');+ server.setRoute('/download', (req, res) => {+ res.writeHead(200, {+ 'Content-Type': 'application/octet-stream',+ 'Content-Disposition': 'attachment; filename=test.txt',+ });+ // Never finish the response so that the download stays in progress.+ res.write('a'.repeat(4096));+ });++ await client.callTool({+ name: 'browser_navigate',+ arguments: { url: server.PREFIX },+ });+ let events = parseResponse(await client.callTool({+ name: 'browser_click',+ arguments: { element: 'Download link', target: 'e2' },+ })).events ?? '';+ await expect.poll(async () => {+ events += parseResponse(await client.callTool({ name: 'browser_snapshot' })).events ?? '';+ return events;+ }).toContain('- Downloading file test.txt ...');++ expect(await client.callTool({ name: 'browser_close' })).toHaveResponse({+ result: expect.stringContaining('No open tabs'),+ });+ // Give the pending download a chance to fail after the browser is gone.+ await new Promise(f => setTimeout(f, 1000));++ expect(await client.callTool({+ name: 'browser_navigate',+ arguments: { url: server.PREFIX },+ })).toHaveResponse({+ snapshot: expect.stringContaining(`- link "Download" [ref=e2]`),+ });+});+test('file upload restricted to roots by default', async ({ startClient, server }, testInfo) => {const rootDir = testInfo.outputPath('workspace');await fs.mkdir(rootDir, { recursive: true });