Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
fix(mcp): report a dialog opened during navigation instead of timing out (#42908)
Yury Semikhatsky7563c5a67eab
diff --git a/packages/playwright-core/src/tools/backend/navigate.ts b/packages/playwright-core/src/tools/backend/navigate.tsindex 683f318649..3975abf1da 100644--- a/packages/playwright-core/src/tools/backend/navigate.ts+++ b/packages/playwright-core/src/tools/backend/navigate.ts@@ -86,7 +86,7 @@ const reload = defineTabTool({},handle: async (tab, params, response) => {- await tab.page.reload(tab.navigationTimeoutOptions);+ await tab.reload();response.setIncludeSnapshot();response.addCode(`await page.reload();`);},diff --git a/packages/playwright-core/src/tools/backend/tab.ts b/packages/playwright-core/src/tools/backend/tab.tsindex ab0a29d64b..de61c443c4 100644--- a/packages/playwright-core/src/tools/backend/tab.ts+++ b/packages/playwright-core/src/tools/backend/tab.ts@@ -348,8 +348,11 @@ export class Tab extends EventEmitter<TabEventsInterface> {this._clearCollectedArtifacts();const { promise: downloadEvent, abort: abortDownloadEvent } = eventWaiter<playwright.Download>(this.page, 'download', 3000);+ let modalStates: ModalState[];try {- await this.page.goto(url, { waitUntil: 'domcontentloaded', ...this.navigationTimeoutOptions });+ modalStates = await this._raceAgainstModalStates(async () => {+ await this.page.goto(url, { waitUntil: 'domcontentloaded', ...this.navigationTimeoutOptions });+ });abortDownloadEvent();} catch (_e: unknown) {const e = _e as Error;@@ -364,11 +367,20 @@ export class Tab extends EventEmitter<TabEventsInterface> {await new Promise(resolve => setTimeout(resolve, 500));return;}+ if (modalStates.length)+ return;// Cap load event to 5 seconds, the page is operational at this point.await this.waitForLoadState('load', { timeout: 5000 });}+ async reload() {+ await this._initializedPromise;+ await this._raceAgainstModalStates(async () => {+ await this.page.reload(this.navigationTimeoutOptions);+ });+ }+async consoleMessageCount(): Promise<{ total: number, errors: number, warnings: number }> {await this._initializedPromise;const messages = await this.page.consoleMessages({ filter: 'since-navigation' });@@ -504,13 +516,11 @@ export class Tab extends EventEmitter<TabEventsInterface> {const listener = (modalState: ModalState) => promise.resolve([modalState]);this.once(TabEvents.modalState, listener);- return await Promise.race([- action().then(() => {- this.off(TabEvents.modalState, listener);- return [];- }),- promise,- ]);+ try {+ return await Promise.race([action().then(() => []), promise]);+ } finally {+ this.off(TabEvents.modalState, listener);+ }}async waitForCompletion(callback: () => Promise<void>) {diff --git a/tests/mcp/cli-navigation.spec.ts b/tests/mcp/cli-navigation.spec.tsindex 74f606fadf..ae34d404b7 100644--- a/tests/mcp/cli-navigation.spec.ts+++ b/tests/mcp/cli-navigation.spec.ts@@ -35,6 +35,17 @@ test('go-forward', async ({ cli, server }) => {- Page Title: Title`);});+test('reload with alert during load', async ({ cli, server }) => {+ server.setContent('/', `<title>Title</title>`, 'text/html');+ await cli('open', server.PREFIX);+ server.setContent('/', `<title>Title</title><script>alert('MyAlert')</script><button>Button</button>`, 'text/html');+ const { output } = await cli('reload');+ expect(output).toContain('["alert" dialog with message "MyAlert"]: can be handled by dialog-accept or dialog-dismiss');+ await cli('dialog-accept');+ const { inlineSnapshot } = await cli('snapshot');+ expect(inlineSnapshot).toContain('button "Button"');+});+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.tsindex 6b13ba0803..ee7c80da7a 100644--- a/tests/mcp/dialogs.spec.ts+++ b/tests/mcp/dialogs.spec.ts@@ -293,3 +293,29 @@ test('alert dialog w/ race', async ({ client, server }) => {- Page Title: Title`),});});++test('alert dialog during navigation', async ({ client, server }) => {+ server.setContent('/', `<title>Title</title><script>alert('Alert')</script><button>Button</button>`, 'text/html');+ expect(await client.callTool({+ name: 'browser_navigate',+ arguments: { url: server.PREFIX },+ })).toHaveResponse({+ modalState: expect.stringContaining(`- ["alert" dialog with message "Alert"]: can be handled by browser_handle_dialog`),+ });++ expect(await client.callTool({+ name: 'browser_handle_dialog',+ arguments: { accept: true },+ })).toHaveResponse({+ modalState: undefined,+ page: expect.stringContaining(`- Page URL: ${server.PREFIX}/+- Page Title: Title`),+ });++ expect(await client.callTool({+ name: 'browser_snapshot',+ arguments: {},+ })).toHaveResponse({+ inlineSnapshot: expect.stringContaining(`- button "Button"`),+ });+});