Skip to content

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

perf(mcp): skip aria snapshot capture when the response discards it (#41923)

Matthew Goodwin15b1aec478d9
diff --git a/packages/playwright-core/src/tools/backend/response.ts b/packages/playwright-core/src/tools/backend/response.ts
index 5a35236996..725edcdaaf 100644
--- a/packages/playwright-core/src/tools/backend/response.ts
+++ b/packages/playwright-core/src/tools/backend/response.ts
@@ -270,7 +270,7 @@ export class Response {
addSection('Ran Playwright code', this._code, 'js');
// Render tab titles upon changes or when more than one tab.
- const tabSnapshot = this._context.currentTab() ? await this._context.currentTabOrDie().captureSnapshot(this._includeSnapshotRoot, this._includeSnapshotDepth, this._includeSnapshotBoxes, this._clientWorkspace) : undefined;
+ const tabSnapshot = this._context.currentTab() ? await this._context.currentTabOrDie().captureSnapshot(this._includeSnapshotRoot, this._includeSnapshotDepth, this._includeSnapshotBoxes, this._clientWorkspace, this._includeSnapshot !== 'none') : undefined;
const tabHeaders = await Promise.all(this._context.tabs().map(tab => tab.headerSnapshot()));
if (this._includeSnapshot !== 'none' || tabHeaders.some(header => header.changed)) {
if (tabHeaders.length !== 1)
diff --git a/packages/playwright-core/src/tools/backend/tab.ts b/packages/playwright-core/src/tools/backend/tab.ts
index 32784e31d9..a23a6a1a91 100644
--- a/packages/playwright-core/src/tools/backend/tab.ts
+++ b/packages/playwright-core/src/tools/backend/tab.ts
@@ -411,19 +411,31 @@ export class Tab extends EventEmitter<TabEventsInterface> {
this._requests.length = 0;
}
- async captureSnapshot(root: playwright.Locator | undefined, depth: number | undefined, boxes: boolean | undefined, relativeTo: string | undefined): Promise<TabSnapshot> {
+ async captureSnapshot(root: playwright.Locator | undefined, depth: number | undefined, boxes: boolean | undefined, relativeTo: string | undefined, includeAria: boolean = true): Promise<TabSnapshot> {
await this._initializedPromise;
let tabSnapshot: TabSnapshot | undefined;
- const modalStates = await this._raceAgainstModalStates(async () => {
- const ariaSnapshot = root
- ? await root.ariaSnapshot({ mode: 'ai', depth, boxes })
- : await this.page.ariaSnapshot({ mode: 'ai', depth, boxes });
- tabSnapshot = {
- ariaSnapshot,
- modalStates: [],
- events: [],
- };
- });
+ let modalStates: ModalState[] = [];
+ if (includeAria) {
+ modalStates = await this._raceAgainstModalStates(async () => {
+ const ariaSnapshot = root
+ ? await root.ariaSnapshot({ mode: 'ai', depth, boxes })
+ : await this.page.ariaSnapshot({ mode: 'ai', depth, boxes });
+ tabSnapshot = {
+ ariaSnapshot,
+ modalStates: [],
+ events: [],
+ };
+ });
+ } else if (this.modalStates().length) {
+ // Matches the aria path's modal fallback below, without the race: there
+ // is no tree walk for a modal to interrupt.
+ modalStates = this.modalStates();
+ } else {
+ // The caller will not render the aria snapshot, so skip the accessibility
+ // tree walk, which dominates response latency on heavy pages. Console and
+ // events are still reported via the shared tail below.
+ tabSnapshot = { ariaSnapshot: '', modalStates: [], events: [] };
+ }
if (tabSnapshot) {
tabSnapshot.consoleLink = await this._consoleLog.take(relativeTo);
tabSnapshot.events = this._recentEventEntries;