Skip to content
Merged
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
24 changes: 23 additions & 1 deletion packages/playwright-core/src/tools/backend/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

import { inspect } from 'util';

import * as z from 'zod';
import { escapeWithQuotes } from '@isomorphic/stringUtils';

Expand Down Expand Up @@ -67,14 +69,34 @@ const evaluate = defineTabTool({
else
response.addCode(`await page.evaluate(${escapeWithQuotes(codeExpression)});`);

const text = JSON.stringify(evalResult.result, null, 2) ?? 'undefined';
const text = isJSONValue(evalResult.result) ? JSON.stringify(evalResult.result, null, 2) : inspect(evalResult.result, {
depth: null,
maxArrayLength: null,
maxStringLength: null,
compact: false,
colors: false,
customInspect: false,
});
await response.addResult('Evaluation result', text, { prefix: 'result', ext: 'json', suggestedFilename: params.filename });
}).catch(e => {
response.addError(e instanceof Error ? e.message : String(e));
});
},
});

function isJSONValue(value: unknown, seen = new Set<object>()): boolean {
if (value === null || typeof value === 'string' || typeof value === 'boolean')
return true;
if (typeof value === 'number')
return Number.isFinite(value) && !Object.is(value, -0);
if (typeof value !== 'object' || seen.has(value))
return false;
if (!Array.isArray(value) && Object.getPrototypeOf(value) !== Object.prototype)
return false;
seen.add(value);
return Object.values(value).every(item => isJSONValue(item, seen));
}

export default [
evaluate,
];
49 changes: 48 additions & 1 deletion tests/mcp/evaluate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ test('browser_evaluate (element)', async ({ client, server }) => {
});
});

test('browser_evaluate object', async ({ client, server }) => {
test('browser_evaluate object', { annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright-mcp/issues/1838' } }, async ({ client, server }) => {
expect(await client.callTool({
name: 'browser_navigate',
arguments: { url: server.HELLO_WORLD },
Expand All @@ -92,6 +92,53 @@ test('browser_evaluate object', async ({ client, server }) => {
result: JSON.stringify({ title: 'Title', url: server.HELLO_WORLD }, null, 2),
code: `await page.evaluate('() => ({ title: document.title, url: document.URL })');`,
});

expect(await client.callTool({
name: 'browser_evaluate',
arguments: {
function: `() => {
window.evaluationCount = (window.evaluationCount || 0) + 1;
const result = {
bigint: 10n,
regexp: /ab+c/i,
date: new Date('2026-01-01T00:00:00Z'),
numbers: [NaN, Infinity, -Infinity, -0],
missing: undefined,
nested: { a: { b: { c: { d: 'value' } } } },
};
result.self = result;
return result;
}`,
},
})).toHaveResponse({
result: `<ref *1> {
bigint: 10n,
regexp: /ab+c/i,
date: 2026-01-01T00:00:00.000Z,
numbers: [
NaN,
Infinity,
-Infinity,
-0
],
missing: undefined,
nested: {
a: {
b: {
c: {
d: 'value'
}
}
}
},
self: [Circular *1]
}`,
isError: undefined,
});
expect(await client.callTool({
name: 'browser_evaluate',
arguments: { function: '() => window.evaluationCount' },
})).toHaveResponse({ result: '1' });
});

test('browser_evaluate expression', async ({ client, server }) => {
Expand Down
Loading