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): do not fail on racy navigation waits (#38292)

Pavel Feldman88b6a534ea15
diff --git a/packages/playwright/src/mcp/browser/tab.ts b/packages/playwright/src/mcp/browser/tab.ts
index b99fd7d9ce..9613112d88 100644
--- a/packages/playwright/src/mcp/browser/tab.ts
+++ b/packages/playwright/src/mcp/browser/tab.ts
@@ -308,7 +308,7 @@ export class Tab extends EventEmitter<TabEventsInterface> {
}
await callOnPageNoTrace(this.page, page => {
- return page.evaluate(() => new Promise(f => setTimeout(f, 1000)));
+ return page.evaluate(() => new Promise(f => setTimeout(f, 1000))).catch(() => {});
});
}
}
diff --git a/tests/mcp/autowait.spec.ts b/tests/mcp/autowait.spec.ts
new file mode 100644
index 0000000000..f64d2ecaa9
--- /dev/null
+++ b/tests/mcp/autowait.spec.ts
@@ -0,0 +1,39 @@
+/**
+ * Copyright (c) Microsoft Corporation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import { test, expect } from './fixtures';
+
+test('racy navigation destroys context', async ({ client, server, mcpBrowser }) => {
+ server.setContent('/', `
+ <button onclick='setTimeout(() => window.location = "about:blank", 100)'>Submit</button>
+ `, 'text/html');
+
+ await client.callTool({
+ name: 'browser_navigate',
+ arguments: { url: server.PREFIX },
+ });
+
+ expect(await client.callTool({
+ name: 'browser_click',
+ arguments: {
+ element: 'Submit button',
+ ref: 'e2',
+ },
+ })).toHaveResponse({
+ code: `await page.getByRole('button', { name: 'Submit' }).click();`,
+ pageState: expect.stringContaining(`- Page URL: about:blank`),
+ });
+});