Skip to content

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

fix(snapshot): finalize snapshot before returning (#38583)

Pavel Feldmanfa583dffba8c
diff --git a/packages/playwright/src/mcp/browser/response.ts b/packages/playwright/src/mcp/browser/response.ts
index 570805da5f..14d0bb8968 100644
--- a/packages/playwright/src/mcp/browser/response.ts
+++ b/packages/playwright/src/mcp/browser/response.ts
@@ -106,6 +106,9 @@ export class Response {
}
async finish() {
+ if (this._tabSnapshot)
+ return;
+
// All the async snapshotting post-action is happening here.
// Everything below should race against modal states.
if (this._includeSnapshot !== 'none' && this._context.currentTab())
diff --git a/packages/playwright/src/mcp/browser/tools/snapshot.ts b/packages/playwright/src/mcp/browser/tools/snapshot.ts
index a77d444f73..c0500347b3 100644
--- a/packages/playwright/src/mcp/browser/tools/snapshot.ts
+++ b/packages/playwright/src/mcp/browser/tools/snapshot.ts
@@ -37,6 +37,7 @@ const snapshot = defineTool({
await context.ensureTab();
response.setIncludeFullSnapshot();
if (params.filename) {
+ await response.finish();
const renderedResponse = response.render();
const fileName = await response.addFile(params.filename, { origin: 'llm', reason: 'Saved snapshot' });
await fs.promises.writeFile(fileName, renderedResponse.asText());
diff --git a/tests/mcp/snapshot-mode.spec.ts b/tests/mcp/snapshot-mode.spec.ts
index 912f938e51..a25046bc2b 100644
--- a/tests/mcp/snapshot-mode.spec.ts
+++ b/tests/mcp/snapshot-mode.spec.ts
@@ -14,6 +14,8 @@
* limitations under the License.
*/
+import fs from 'fs';
+import path from 'path';
import { test, expect } from './fixtures';
test('should respect --snapshot-mode=full', async ({ startClient, server }) => {
@@ -124,4 +126,8 @@ test('should respect snapshot[filename]', async ({ startClient, server }, testIn
pageState: undefined,
files: expect.stringMatching(/\[Saved snapshot\]\(.*md\)/)
});
+
+ expect(await fs.promises.readFile(path.join(outputDir, 'snapshot1.md'), 'utf8')).toContain(`
+- button "Button 1" [ref=e2]
+`);
});