Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"@stylistic/eslint-plugin": "^5.2.3",
"@types/codemirror": "^5.60.7",
"@types/formidable": "^2.0.4",
"@types/node": "18.19.76",
"@types/node": "25.0.3",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use 20 which is our oldest supported node?

"@types/react": "^19.2.1",
"@types/react-dom": "^19.2.1",
"@types/ws": "^8.5.3",
Expand Down
4 changes: 3 additions & 1 deletion packages/playwright/src/mcp/browser/tools/runCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ const runCode = defineTabTool({
__end__.reject(e);
}
})()`;
await vm.runInContext(snippet, context);
await vm.runInContext(snippet, context, {
importModuleDynamically: vm.constants?.USE_MAIN_CONTEXT_DEFAULT_LOADER,
});
const result = await __end__;
if (typeof result === 'string')
response.addResult(result);
Expand Down
63 changes: 63 additions & 0 deletions tests/mcp/run-code.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,66 @@ test('browser_run_code return value', async ({ client, server }) => {
result: '{"message":"Hello, world!"}',
});
});

test('browser_run_code dynamic import', async ({ client, server }) => {
test.skip(process.platform === 'win32', 'windows only works with file:// imports');
server.setContent('/', `
<button onclick="console.log('Submit')">Submit</button>
`, 'text/html');
await client.callTool({
name: 'browser_navigate',
arguments: { url: server.PREFIX },
});

const scriptPath = test.info().outputPath('helper.mjs');
await fs.writeFile(scriptPath, `
export async function clickSubmit(page) {
await page.getByRole("button", { name: "Submit" }).click();
}
`);

const code = `async (page) => {
const { clickSubmit } = await import(${JSON.stringify(scriptPath)});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we want to prohibit random imports? Looks like it did work for require but not for import. I think the user should have better control over what can be imported.

await clickSubmit(page);
}`;
expect(await client.callTool({
name: 'browser_run_code',
arguments: {
code,
},
})).toHaveResponse({
code: `await (${code})(page);`,
consoleMessages: expect.stringContaining('- [LOG] Submit'),
});
});

test('browser_run_code dynamic import (file: proto)', async ({ client, server }) => {
server.setContent('/', `
<button onclick="console.log('Submit')">Submit</button>
`, 'text/html');
await client.callTool({
name: 'browser_navigate',
arguments: { url: server.PREFIX },
});

const scriptPath = test.info().outputPath('helper.mjs');
await fs.writeFile(scriptPath, `
export async function clickSubmit(page) {
await page.getByRole("button", { name: "Submit" }).click();
}
`);

const code = `async (page) => {
const { clickSubmit } = await import(${JSON.stringify('file://' + scriptPath)});
await clickSubmit(page);
}`;
expect(await client.callTool({
name: 'browser_run_code',
arguments: {
code,
},
})).toHaveResponse({
code: `await (${code})(page);`,
consoleMessages: expect.stringContaining('- [LOG] Submit'),
});
});
Loading