Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(mcp): race browser_navigate_back and forward against modal states (#43252)

Alok Pandey6e5419ad3084
diff --git a/packages/playwright-core/src/tools/backend/navigate.ts b/packages/playwright-core/src/tools/backend/navigate.ts
index 3975abf1da..08f29b061b 100644
--- a/packages/playwright-core/src/tools/backend/navigate.ts
+++ b/packages/playwright-core/src/tools/backend/navigate.ts
@@ -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();`);
},
@@ -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();`);
},
diff --git a/packages/playwright-core/src/tools/backend/tab.ts b/packages/playwright-core/src/tools/backend/tab.ts
index a4deb7e5ee..5a3c7ae4e7 100644
--- a/packages/playwright-core/src/tools/backend/tab.ts
+++ b/packages/playwright-core/src/tools/backend/tab.ts
@@ -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' });
diff --git a/tests/mcp/cli-navigation.spec.ts b/tests/mcp/cli-navigation.spec.ts
index 00fc31c34a..a9a4147876 100644
--- a/tests/mcp/cli-navigation.spec.ts
+++ b/tests/mcp/cli-navigation.spec.ts
@@ -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');
diff --git a/tests/mcp/dialogs.spec.ts b/tests/mcp/dialogs.spec.ts
index ee7c80da7a..e1bc404306 100644
--- a/tests/mcp/dialogs.spec.ts
+++ b/tests/mcp/dialogs.spec.ts
@@ -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"`),
+ });
+});