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
3 changes: 2 additions & 1 deletion packages/playwright-core/src/tools/backend/cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

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

const cookieList = defineTool({
Expand Down Expand Up @@ -137,7 +138,7 @@ const cookieDelete = defineTool({
handle: async (context, params, response) => {
const browserContext = await context.ensureBrowserContext();
await browserContext.clearCookies({ name: params.name });
response.addCode(`await page.context().clearCookies({ name: '${params.name}' });`);
response.addCode(`await page.context().clearCookies({ name: ${escapeWithQuotes(params.name)} });`);
},
});

Expand Down
9 changes: 5 additions & 4 deletions packages/playwright-core/src/tools/backend/keyboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import * as z from 'zod';
import { escapeWithQuotes } from '@isomorphic/stringUtils';
import { defineTabTool } from './tool';
import { elementSchema } from './snapshot';

Expand All @@ -33,7 +34,7 @@ const press = defineTabTool({

handle: async (tab, params, response) => {
response.addCode(`// Press ${params.key}`);
response.addCode(`await page.keyboard.press('${params.key}');`);
response.addCode(`await page.keyboard.press(${escapeWithQuotes(params.key)});`);
if (params.key === 'Enter') {
response.setIncludeSnapshot();
await tab.waitForCompletion(async () => {
Expand Down Expand Up @@ -62,7 +63,7 @@ const pressSequentially = defineTabTool({

handle: async (tab, params, response) => {
response.addCode(`// Press ${params.text}`);
response.addCode(`await page.keyboard.type('${params.text}');`);
response.addCode(`await page.keyboard.type(${escapeWithQuotes(params.text)});`);
await tab.page.keyboard.type(params.text);
if (params.submit) {
response.addCode(`await page.keyboard.press('Enter');`);
Expand Down Expand Up @@ -133,7 +134,7 @@ const keydown = defineTabTool({
},

handle: async (tab, params, response) => {
response.addCode(`await page.keyboard.down('${params.key}');`);
response.addCode(`await page.keyboard.down(${escapeWithQuotes(params.key)});`);
await tab.page.keyboard.down(params.key);
},
});
Expand All @@ -153,7 +154,7 @@ const keyup = defineTabTool({
},

handle: async (tab, params, response) => {
response.addCode(`await page.keyboard.up('${params.key}');`);
response.addCode(`await page.keyboard.up(${escapeWithQuotes(params.key)});`);
await tab.page.keyboard.up(params.key);
},
});
Expand Down
3 changes: 2 additions & 1 deletion packages/playwright-core/src/tools/backend/navigate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import * as z from 'zod';
import { escapeWithQuotes } from '@isomorphic/stringUtils';
import { defineTool, defineTabTool } from './tool';

const navigate = defineTool({
Expand All @@ -35,7 +36,7 @@ const navigate = defineTool({
const url = await tab.checkUrlAndNavigate(params.url);

response.setIncludeSnapshot();
response.addCode(`await page.goto('${url}');`);
response.addCode(`await page.goto(${escapeWithQuotes(url)});`);
},
});

Expand Down
3 changes: 2 additions & 1 deletion packages/playwright-core/src/tools/backend/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

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

import type * as playwright from '../../..';
Expand Down Expand Up @@ -81,7 +82,7 @@ const route = defineTool({

await context.addRoute(entry);
response.addTextResult(`Route added for pattern: ${params.pattern}`);
response.addCode(`await page.context().route('${params.pattern}', async route => { /* route handler */ });`);
response.addCode(`await page.context().route(${escapeWithQuotes(params.pattern)}, async route => { /* route handler */ });`);
},
});

Expand Down
5 changes: 3 additions & 2 deletions packages/playwright-core/src/tools/backend/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

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

const storageState = defineTool({
Expand All @@ -35,7 +36,7 @@ const storageState = defineTool({
const state = await browserContext.storageState();
const serializedState = JSON.stringify(state, null, 2);
const resolvedFile = await response.resolveClientFile({ prefix: 'storage-state', ext: 'json', suggestedFilename: params.filename }, 'Storage state');
response.addCode(`await page.context().storageState({ path: '${resolvedFile.relativeName}' });`);
response.addCode(`await page.context().storageState({ path: ${escapeWithQuotes(resolvedFile.relativeName)} });`);
await response.addFileResult(resolvedFile, serializedState);
},
});
Expand All @@ -58,7 +59,7 @@ const setStorageState = defineTool({
const resolvedFilename = await response.resolveClientFilename(params.filename);
await browserContext.setStorageState(resolvedFilename);
response.addTextResult(`Storage state restored from ${params.filename}`);
response.addCode(`await page.context().setStorageState('${params.filename}');`);
response.addCode(`await page.context().setStorageState(${escapeWithQuotes(params.filename)});`);
},
});

Expand Down
3 changes: 2 additions & 1 deletion packages/playwright-core/src/tools/backend/tabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import * as z from 'zod';
import { escapeWithQuotes } from '@isomorphic/stringUtils';
import { defineTool } from './tool';
import { renderTabsMarkdown } from './response';

Expand Down Expand Up @@ -44,7 +45,7 @@ const browserTabs = defineTool({
if (params.url) {
const url = await tab.checkUrlAndNavigate(params.url);
response.setIncludeSnapshot();
response.addCode(`await page.goto('${url}');`);
response.addCode(`await page.goto(${escapeWithQuotes(url)});`);
}
break;
}
Expand Down
13 changes: 7 additions & 6 deletions packages/playwright-core/src/tools/backend/webstorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

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

const localStorageList = defineTabTool({
Expand Down Expand Up @@ -59,7 +60,7 @@ const localStorageGet = defineTabTool({
response.addTextResult(`localStorage key '${params.key}' not found`);
else
response.addTextResult(`${params.key}=${value}`);
response.addCode(`await page.localStorage.getItem('${params.key}');`);
response.addCode(`await page.localStorage.getItem(${escapeWithQuotes(params.key)});`);
},
});

Expand All @@ -79,7 +80,7 @@ const localStorageSet = defineTabTool({

handle: async (tab, params, response) => {
await tab.page.localStorage.setItem(params.key, params.value);
response.addCode(`await page.localStorage.setItem('${params.key}', '${params.value}');`);
response.addCode(`await page.localStorage.setItem(${escapeWithQuotes(params.key)}, ${escapeWithQuotes(params.value)});`);
},
});

Expand All @@ -98,7 +99,7 @@ const localStorageDelete = defineTabTool({

handle: async (tab, params, response) => {
await tab.page.localStorage.removeItem(params.key);
response.addCode(`await page.localStorage.removeItem('${params.key}');`);
response.addCode(`await page.localStorage.removeItem(${escapeWithQuotes(params.key)});`);
},
});

Expand Down Expand Up @@ -163,7 +164,7 @@ const sessionStorageGet = defineTabTool({
response.addTextResult(`sessionStorage key '${params.key}' not found`);
else
response.addTextResult(`${params.key}=${value}`);
response.addCode(`await page.sessionStorage.getItem('${params.key}');`);
response.addCode(`await page.sessionStorage.getItem(${escapeWithQuotes(params.key)});`);
},
});

Expand All @@ -183,7 +184,7 @@ const sessionStorageSet = defineTabTool({

handle: async (tab, params, response) => {
await tab.page.sessionStorage.setItem(params.key, params.value);
response.addCode(`await page.sessionStorage.setItem('${params.key}', '${params.value}');`);
response.addCode(`await page.sessionStorage.setItem(${escapeWithQuotes(params.key)}, ${escapeWithQuotes(params.value)});`);
},
});

Expand All @@ -202,7 +203,7 @@ const sessionStorageDelete = defineTabTool({

handle: async (tab, params, response) => {
await tab.page.sessionStorage.removeItem(params.key);
response.addCode(`await page.sessionStorage.removeItem('${params.key}');`);
response.addCode(`await page.sessionStorage.removeItem(${escapeWithQuotes(params.key)});`);
},
});

Expand Down
7 changes: 7 additions & 0 deletions tests/mcp/cli-core.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,3 +368,10 @@ test('--raw on command without output', async ({ cli, server }) => {
expect(output).not.toContain('### ');
expect(output).not.toContain('Page URL');
});

test('codegen escapes single quotes in user input', async ({ cli, server }) => {
server.setContent('/', `<input type=text>`, 'text/html');
await cli('open', server.PREFIX);
const { output } = await cli('type', "it's working");
expect(output).toContain(`await page.keyboard.type('it\\'s working');`);
});