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): use waitUntil commit for navigate back/forward (#41153)

Yury Semikhatsky0492b3b0380d
diff --git a/packages/playwright-core/src/tools/backend/navigate.ts b/packages/playwright-core/src/tools/backend/navigate.ts
index fcaa3b0d46..460642cd92 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(tab.navigationTimeoutOptions);
+ await tab.page.goBack({ waitUntil: 'commit', ...tab.navigationTimeoutOptions });
response.setIncludeSnapshot();
response.addCode(`await page.goBack();`);
},
@@ -68,7 +68,7 @@ const goForward = defineTabTool({
},
handle: async (tab, params, response) => {
- await tab.page.goForward(tab.navigationTimeoutOptions);
+ await tab.page.goForward({ waitUntil: 'commit', ...tab.navigationTimeoutOptions });
response.setIncludeSnapshot();
response.addCode(`await page.goForward();`);
},
diff --git a/tests/mcp/core.spec.ts b/tests/mcp/core.spec.ts
index 06423357b5..8e6040a3f5 100644
--- a/tests/mcp/core.spec.ts
+++ b/tests/mcp/core.spec.ts
@@ -86,6 +86,32 @@ test('browser_navigate can navigate to file:// URLs allowUnrestrictedFileAccess
});
});
+test('browser_navigate_back does not time out when load never fires', async ({ client, server }) => {
+ // https://github.com/microsoft/playwright-mcp/issues/1635
+ // Page A never fires the `load` event because the image request hangs forever.
+ // Going back to it should still succeed because we wait for `commit`, not `load`.
+ server.setRoute('/hang', () => {});
+ server.setContent('/page-a', `<title>Page A</title><body>Page A<img src="/hang"></body>`, 'text/html');
+ server.setContent('/page-b', `<title>Page B</title><body>Page B</body>`, 'text/html');
+
+ await client.callTool({
+ name: 'browser_navigate',
+ arguments: { url: `${server.PREFIX}/page-a` },
+ });
+ await client.callTool({
+ name: 'browser_navigate',
+ arguments: { url: `${server.PREFIX}/page-b` },
+ });
+
+ expect(await client.callTool({
+ name: 'browser_navigate_back',
+ arguments: {},
+ })).toHaveResponse({
+ code: `await page.goBack();`,
+ page: expect.stringContaining(`- Page URL: ${server.PREFIX}/page-a`),
+ });
+});
+
test('browser_select_option', async ({ client, server }) => {
server.setContent('/', `
<title>Title</title>