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
13 changes: 1 addition & 12 deletions packages/playwright-core/src/tools/backend/navigate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,7 @@ const navigate = defineTool({

handle: async (context, params, response) => {
const tab = await context.ensureTab();
let url = params.url;
try {
new URL(url);
} catch (e) {
if (url.startsWith('localhost'))
url = 'http://' + url;
else
url = 'https://' + url;
}

context.checkUrlAllowed(url);
await tab.navigate(url);
const url = await tab.checkUrlAndNavigate(params.url);

response.setIncludeSnapshot();
response.addCode(`await page.goto('${url}');`);
Expand Down
14 changes: 14 additions & 0 deletions packages/playwright-core/src/tools/backend/tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,20 @@ export class Tab extends EventEmitter<TabEventsInterface> {
await this.page.waitForLoadState(state, options).catch(e => debug('pw:tools:error')(e));
}

async checkUrlAndNavigate(url: string): Promise<string> {
try {
new URL(url);
} catch (e) {
if (url.startsWith('localhost'))
url = 'http://' + url;
else
url = 'https://' + url;
}
this.context.checkUrlAllowed(url);
await this.navigate(url);
return url;
}

async navigate(url: string) {
await this._initializedPromise;

Expand Down
8 changes: 7 additions & 1 deletion packages/playwright-core/src/tools/backend/tabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const browserTabs = defineTool({
inputSchema: z.object({
action: z.enum(['list', 'new', 'close', 'select']).describe('Operation to perform'),
index: z.number().optional().describe('Tab index, used for close/select. If omitted for close, current tab is closed.'),
url: z.string().optional().describe('URL to navigate to in the new tab, used for new.'),
}),
type: 'action',
},
Expand All @@ -39,7 +40,12 @@ const browserTabs = defineTool({
break;
}
case 'new': {
await context.newTab();
const tab = await context.newTab();
if (params.url) {
const url = await tab.checkUrlAndNavigate(params.url);
response.setIncludeSnapshot();
response.addCode(`await page.goto('${url}');`);
}
break;
}
case 'close': {
Expand Down
7 changes: 7 additions & 0 deletions tests/mcp/cli-navigation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ test('open without url opens about:blank', async ({ cli }) => {
expect(output).toContain('- Page URL: about:blank');
});

test('tab-new with url', async ({ cli, server }) => {
await cli('open');
const { output } = await cli('tab-new', server.HELLO_WORLD);
expect(output).toContain(`- 0: [](about:blank)`);
expect(output).toContain(`- 1: (current) [Title](${server.HELLO_WORLD})`);
});

test('run-code', async ({ cli, server }) => {
await cli('open', server.HELLO_WORLD);
const { output } = await cli('run-code', '() => page.title()');
Expand Down
13 changes: 13 additions & 0 deletions tests/mcp/tabs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,19 @@ test('create new tab', async ({ client }) => {
});
});

test('create new tab with url', async ({ client }) => {
expect(await client.callTool({
name: 'browser_tabs',
arguments: {
action: 'new',
url: `data:text/html,<title>Tab one</title><body>Body one</body>`,
},
})).toHaveResponse({
result: `- 0: [](about:blank)
- 1: (current) [Tab one](data:text/html,<title>Tab one</title><body>Body one</body>)`,
});
});

test('select tab', async ({ client }) => {
await createTab(client, 'Tab one', 'Body one');
await createTab(client, 'Tab two', 'Body two');
Expand Down
Loading