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
4 changes: 2 additions & 2 deletions packages/playwright-core/src/tools/backend/navigate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const goBack = defineTabTool({
},

handle: async (tab, params, response) => {
await tab.page.goBack({ waitUntil: 'commit', ...tab.navigationTimeoutOptions });
await tab.goBack();
response.setIncludeSnapshot();
response.addCode(`await page.goBack();`);
},
Expand All @@ -68,7 +68,7 @@ const goForward = defineTabTool({
},

handle: async (tab, params, response) => {
await tab.page.goForward({ waitUntil: 'commit', ...tab.navigationTimeoutOptions });
await tab.goForward();
response.setIncludeSnapshot();
response.addCode(`await page.goForward();`);
},
Expand Down
15 changes: 15 additions & 0 deletions packages/playwright-core/src/tools/backend/tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,21 @@ export class Tab extends EventEmitter<TabEventsInterface> {
});
}

async goBack() {
await this._initializedPromise;
await this._raceAgainstModalStates(async () => {
await this.page.goBack({ waitUntil: 'commit', ...this.navigationTimeoutOptions });
});
}

async goForward() {
await this._initializedPromise;
await this._raceAgainstModalStates(async () => {
await this.page.goForward({ waitUntil: 'commit', ...this.navigationTimeoutOptions });
});
}


async consoleMessageCount(): Promise<{ total: number, errors: number, warnings: number }> {
await this._initializedPromise;
const messages = await this.page.consoleMessages({ filter: 'since-navigation' });
Expand Down
37 changes: 37 additions & 0 deletions tests/mcp/cli-navigation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,43 @@ test('reload with alert during load', async ({ cli, server }) => {
expect(inlineSnapshot).toContain('button "Button"');
});

test('go-back with dialog during navigation', async ({ cli, server }) => {
server.setContent('/page1', `<title>Page 1</title><button>Button 1</button>`, 'text/html');
server.setContent('/page2', `<title>Page 2</title><button id="btn">Click me</button><script>
window.addEventListener('beforeunload', (e) => {
e.preventDefault();
e.returnValue = '';
});
</script>`, 'text/html');

await cli('open', server.PREFIX + '/page1');
await cli('goto', server.PREFIX + '/page2');
await cli('click', 'e2');
const { output } = await cli('go-back');
expect(output).toMatch(/\["beforeunload" dialog.*\]: can be handled by dialog-accept or dialog-dismiss/);
await cli('dialog-accept');
const { inlineSnapshot } = await cli('snapshot');
expect(inlineSnapshot).toContain('button "Button 1"');
});

test('go-forward with dialog during navigation', async ({ cli, server }) => {
server.setContent('/page1', `<title>Page 1</title><button id="btn1" onclick="window.addEventListener('beforeunload', (e) => {
e.preventDefault();
e.returnValue = '';
})">Button 1</button>`, 'text/html');
server.setContent('/page2', `<title>Page 2</title><button id="btn2">Button 2</button>`, 'text/html');

await cli('open', server.PREFIX + '/page1');
await cli('goto', server.PREFIX + '/page2');
await cli('go-back');
await cli('click', 'button');
const { output } = await cli('go-forward');
expect(output).toMatch(/\["beforeunload" dialog.*\]: can be handled by dialog-accept or dialog-dismiss/);
await cli('dialog-accept');
const { inlineSnapshot } = await cli('snapshot');
expect(inlineSnapshot).toContain('button "Button 2"');
});

test('open without url opens about:blank', async ({ cli }) => {
const { output } = await cli('open');
expect(output).toContain('- Page URL: about:blank');
Expand Down
45 changes: 45 additions & 0 deletions tests/mcp/dialogs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,3 +319,48 @@ test('alert dialog during navigation', async ({ client, server }) => {
inlineSnapshot: expect.stringContaining(`- button "Button"`),
});
});

test('dialog during navigate back', async ({ client, server }) => {
server.setContent('/page1', `<title>Page 1</title><button>Button 1</button>`, 'text/html');
server.setContent('/page2', `<title>Page 2</title><button id="btn">Click me</button><script>
window.addEventListener('beforeunload', (e) => {
e.preventDefault();
e.returnValue = '';
});
</script>`, 'text/html');

await client.callTool({
name: 'browser_navigate',
arguments: { url: server.PREFIX + '/page1' },
});

await client.callTool({
name: 'browser_navigate',
arguments: { url: server.PREFIX + '/page2' },
});

// User interaction is required for beforeunload to trigger
await client.callTool({
name: 'browser_click',
arguments: { element: 'Click me', target: 'e2' },
});

expect(await client.callTool({
name: 'browser_navigate_back',
arguments: {},
})).toHaveResponse({
modalState: expect.stringMatching(/- \["beforeunload" dialog.*\]: can be handled by browser_handle_dialog/),
});

await client.callTool({
name: 'browser_handle_dialog',
arguments: { accept: true },
});

expect(await client.callTool({
name: 'browser_snapshot',
arguments: {},
})).toHaveResponse({
inlineSnapshot: expect.stringContaining(`- button "Button 1"`),
});
});