fix: emit code for waitfor (#862)

Fixes https://github.com/microsoft/playwright-mcp/issues/859
This commit is contained in:
Pavel Feldman
2025-08-11 11:58:45 -07:00
committed by GitHub
parent 21ced701b5
commit 24f81a7a27
2 changed files with 21 additions and 5 deletions

View File

@@ -36,10 +36,8 @@ const wait = defineTool({
if (!params.text && !params.textGone && !params.time) if (!params.text && !params.textGone && !params.time)
throw new Error('Either time, text or textGone must be provided'); throw new Error('Either time, text or textGone must be provided');
const code: string[] = [];
if (params.time) { if (params.time) {
code.push(`await new Promise(f => setTimeout(f, ${params.time!} * 1000));`); response.addCode(`await new Promise(f => setTimeout(f, ${params.time!} * 1000));`);
await new Promise(f => setTimeout(f, Math.min(30000, params.time! * 1000))); await new Promise(f => setTimeout(f, Math.min(30000, params.time! * 1000)));
} }
@@ -48,12 +46,12 @@ const wait = defineTool({
const goneLocator = params.textGone ? tab.page.getByText(params.textGone).first() : undefined; const goneLocator = params.textGone ? tab.page.getByText(params.textGone).first() : undefined;
if (goneLocator) { if (goneLocator) {
code.push(`await page.getByText(${JSON.stringify(params.textGone)}).first().waitFor({ state: 'hidden' });`); response.addCode(`await page.getByText(${JSON.stringify(params.textGone)}).first().waitFor({ state: 'hidden' });`);
await goneLocator.waitFor({ state: 'hidden' }); await goneLocator.waitFor({ state: 'hidden' });
} }
if (locator) { if (locator) {
code.push(`await page.getByText(${JSON.stringify(params.text)}).first().waitFor({ state: 'visible' });`); response.addCode(`await page.getByText(${JSON.stringify(params.text)}).first().waitFor({ state: 'visible' });`);
await locator.waitFor({ state: 'visible' }); await locator.waitFor({ state: 'visible' });
} }

View File

@@ -47,6 +47,7 @@ test('browser_wait_for(text)', async ({ client, server }) => {
expect(await client.callTool({ expect(await client.callTool({
name: 'browser_wait_for', name: 'browser_wait_for',
arguments: { text: 'Text to appear' }, arguments: { text: 'Text to appear' },
code: `await page.getByText("Text to appear").first().waitFor({ state: 'visible' });`,
})).toHaveResponse({ })).toHaveResponse({
pageState: expect.stringContaining(`- generic [ref=e3]: Text to appear`), pageState: expect.stringContaining(`- generic [ref=e3]: Text to appear`),
}); });
@@ -83,7 +84,24 @@ test('browser_wait_for(textGone)', async ({ client, server }) => {
expect(await client.callTool({ expect(await client.callTool({
name: 'browser_wait_for', name: 'browser_wait_for',
arguments: { textGone: 'Text to disappear' }, arguments: { textGone: 'Text to disappear' },
code: `await page.getByText("Text to disappear").first().waitFor({ state: 'hidden' });`,
})).toHaveResponse({ })).toHaveResponse({
pageState: expect.stringContaining(`- generic [ref=e3]: Text to appear`), pageState: expect.stringContaining(`- generic [ref=e3]: Text to appear`),
}); });
}); });
test('browser_wait_for(time)', async ({ client, server }) => {
server.setContent('/', `<body><div>Hello World</div></body>`, 'text/html');
await client.callTool({
name: 'browser_navigate',
arguments: { url: server.PREFIX },
});
expect(await client.callTool({
name: 'browser_wait_for',
arguments: { time: 1 },
})).toHaveResponse({
code: `await new Promise(f => setTimeout(f, 1 * 1000));`,
});
});