mirror of
https://github.com/microsoft/playwright-mcp.git
synced 2025-07-27 00:52:27 +08:00
fix: don't error on navigating to a download link (#328)
This commit is contained in:
parent
950d0d1d34
commit
c2255246a3
19
src/tab.ts
19
src/tab.ts
@ -66,7 +66,26 @@ export class Tab {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async navigate(url: string) {
|
async navigate(url: string) {
|
||||||
|
const downloadEvent = this.page.waitForEvent('download').catch(() => {});
|
||||||
|
try {
|
||||||
await this.page.goto(url, { waitUntil: 'domcontentloaded' });
|
await this.page.goto(url, { waitUntil: 'domcontentloaded' });
|
||||||
|
} catch (_e: unknown) {
|
||||||
|
const e = _e as Error;
|
||||||
|
const mightBeDownload =
|
||||||
|
e.message.includes('net::ERR_ABORTED') // chromium
|
||||||
|
|| e.message.includes('Download is starting'); // firefox + webkit
|
||||||
|
if (!mightBeDownload)
|
||||||
|
throw e;
|
||||||
|
|
||||||
|
// on chromium, the download event is fired *after* page.goto rejects, so we wait a lil bit
|
||||||
|
const download = await Promise.race([
|
||||||
|
downloadEvent,
|
||||||
|
new Promise(resolve => setTimeout(resolve, 500)),
|
||||||
|
]);
|
||||||
|
if (!download)
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
|
||||||
// Cap load event to 5 seconds, the page is operational at this point.
|
// Cap load event to 5 seconds, the page is operational at this point.
|
||||||
await this.page.waitForLoadState('load', { timeout: 5000 }).catch(() => {});
|
await this.page.waitForLoadState('load', { timeout: 5000 }).catch(() => {});
|
||||||
}
|
}
|
||||||
|
@ -119,3 +119,21 @@ test('clicking on download link emits download', async ({ startClient }, testInf
|
|||||||
### Downloads
|
### Downloads
|
||||||
- Downloaded file test.txt to ${path.join(outputDir, 'test.txt')}`);
|
- Downloaded file test.txt to ${path.join(outputDir, 'test.txt')}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('navigating to download link emits download', async ({ client, server, mcpBrowser }) => {
|
||||||
|
test.skip(mcpBrowser === 'webkit' && process.platform === 'linux', 'https://github.com/microsoft/playwright/blob/8e08fdb52c27bb75de9bf87627bf740fadab2122/tests/library/download.spec.ts#L436');
|
||||||
|
server.route('/download', (req, res) => {
|
||||||
|
res.writeHead(200, {
|
||||||
|
'Content-Type': 'text/plain',
|
||||||
|
'Content-Disposition': 'attachment; filename=test.txt',
|
||||||
|
});
|
||||||
|
res.end('Hello world!');
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(await client.callTool({
|
||||||
|
name: 'browser_navigate',
|
||||||
|
arguments: {
|
||||||
|
url: server.PREFIX + '/download',
|
||||||
|
},
|
||||||
|
})).toContainTextContent('### Downloads');
|
||||||
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user