Skip to content

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

fix(trace): capture attribute mutations after a popup document swap (#41086)

Yury Semikhatsky9fbc0a60e049
diff --git a/packages/playwright-core/src/server/trace/recorder/snapshotterInjected.ts b/packages/playwright-core/src/server/trace/recorder/snapshotterInjected.ts
index 13bfcf09d7..dc4927cc56 100644
--- a/packages/playwright-core/src/server/trace/recorder/snapshotterInjected.ts
+++ b/packages/playwright-core/src/server/trace/recorder/snapshotterInjected.ts
@@ -73,6 +73,8 @@ export function frameSnapshotStreamer(snapshotStreamer: string, removeNoScript:
return obj[kCachedData];
}
+ const kObserverConfig: MutationObserverInit = { attributes: true, subtree: true };
+
function removeHash(url: string) {
try {
const u = new URL(url);
@@ -90,6 +92,7 @@ export function frameSnapshotStreamer(snapshotStreamer: string, removeNoScript:
private _readingStyleSheet = false; // To avoid invalidating due to our own reads.
private _fakeBase: HTMLBaseElement;
private _observer: MutationObserver;
+ private _observedDocument: Document | undefined;
private _targetGeneration = 0;
constructor() {
@@ -116,9 +119,7 @@ export function frameSnapshotStreamer(snapshotStreamer: string, removeNoScript:
this._fakeBase = document.createElement('base');
this._observer = new MutationObserver(list => this._handleMutations(list));
- const observerConfig = { attributes: true, subtree: true };
- this._observer.observe(document, observerConfig);
- this._refreshListenersWhenNeeded();
+ this._ensureObservingCurrentDocument();
}
private _refreshListenersWhenNeeded() {
@@ -210,6 +211,19 @@ export function frameSnapshotStreamer(snapshotStreamer: string, removeNoScript:
ensureCachedData(mutation.target).attributesCached = undefined;
}
+ private _ensureObservingCurrentDocument() {
+ // A window can swap its document without re-running the init script (e.g.
+ // a popup reusing its initial about:blank document), leaving our observers
+ // and listeners bound to the stale one. Re-attach on swap.
+ // https://github.com/microsoft/playwright/issues/40895
+ if (this._observedDocument === document)
+ return;
+ this._observedDocument = document;
+ this._observer.disconnect();
+ this._observer.observe(document, kObserverConfig);
+ this._refreshListenersWhenNeeded();
+ }
+
private _invalidateStyleSheet(sheet: CSSStyleSheet) {
if (this._readingStyleSheet)
return;
@@ -347,6 +361,7 @@ export function frameSnapshotStreamer(snapshotStreamer: string, removeNoScript:
let shadowDomNesting = 0;
let headNesting = 0;
+ this._ensureObservingCurrentDocument();
// Ensure we are up to date.
this._handleMutations(this._observer.takeRecords());
diff --git a/tests/library/trace-viewer.spec.ts b/tests/library/trace-viewer.spec.ts
index eb2008452f..b5a2b3903b 100644
--- a/tests/library/trace-viewer.spec.ts
+++ b/tests/library/trace-viewer.spec.ts
@@ -681,6 +681,32 @@ test('should popup snapshot', async ({ page, runAndTrace, server }) => {
await expect(popup.frameLocator('iframe').getByText('hello äöü 🙂')).toBeVisible();
});
+test('should capture attribute mutations inside a popup window', {
+ annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/40895' }
+}, async ({ page, server, runAndTrace }) => {
+ server.setRoute('/popup.html', (req, res) => {
+ res.end(`<!DOCTYPE html><html><head>
+ <style>.no-display { display: none !important; }</style>
+ </head><body>
+ <div id="overlay">overlay</div>
+ <button id="hide" type="button" onclick="document.getElementById('overlay').classList.add('no-display')">Hide</button>
+ </body></html>`);
+ });
+
+ const traceViewer = await runAndTrace(async () => {
+ await page.goto(server.EMPTY_PAGE);
+ const [popup] = await Promise.all([
+ page.waitForEvent('popup'),
+ page.evaluate(url => window.open(url, 'popup', 'popup'), server.PREFIX + '/popup.html'),
+ ]);
+ await popup.getByRole('button', { name: 'Hide' }).click();
+ await expect(popup.locator('#overlay')).toBeHidden();
+ });
+
+ const frame = await traceViewer.snapshotFrame('Click');
+ await expect(frame.locator('#overlay')).toHaveClass('no-display');
+});
+
test('should capture iframe with sandbox attribute', async ({ page, server, runAndTrace }) => {
await page.route('**/empty.html', route => {
void route.fulfill({