From fc0b8ad3f0298f7fe46d8b9b759909ebac7ce113 Mon Sep 17 00:00:00 2001 From: Dmitry Gozman Date: Tue, 29 Jun 2021 16:20:15 -0700 Subject: [PATCH] fix(tracing): record scroll position for all scrolled elements (#7388) --- src/server/snapshot/snapshotterInjected.ts | 22 +++++------- tests/snapshotter.spec.ts | 39 ++++++++++++++++++++++ 2 files changed, 48 insertions(+), 13 deletions(-) diff --git a/src/server/snapshot/snapshotterInjected.ts b/src/server/snapshot/snapshotterInjected.ts index 526cce8ddd..46dcd10f60 100644 --- a/src/server/snapshot/snapshotterInjected.ts +++ b/src/server/snapshot/snapshotterInjected.ts @@ -322,19 +322,15 @@ export function frameSnapshotStreamer(snapshotStreamer: string) { attrs['checked'] = ''; } } - if (element === document.scrollingElement) { - // TODO: restoring scroll positions of all elements - // is somewhat expensive. Figure this out. - if (element.scrollTop) { - expectValue(kScrollTopAttribute); - expectValue(element.scrollTop); - attrs[kScrollTopAttribute] = '' + element.scrollTop; - } - if (element.scrollLeft) { - expectValue(kScrollLeftAttribute); - expectValue(element.scrollLeft); - attrs[kScrollLeftAttribute] = '' + element.scrollLeft; - } + if (element.scrollTop) { + expectValue(kScrollTopAttribute); + expectValue(element.scrollTop); + attrs[kScrollTopAttribute] = '' + element.scrollTop; + } + if (element.scrollLeft) { + expectValue(kScrollLeftAttribute); + expectValue(element.scrollLeft); + attrs[kScrollLeftAttribute] = '' + element.scrollLeft; } if (element.shadowRoot) { ++shadowDomNesting; diff --git a/tests/snapshotter.spec.ts b/tests/snapshotter.spec.ts index 0063f75767..87837731d0 100644 --- a/tests/snapshotter.spec.ts +++ b/tests/snapshotter.spec.ts @@ -237,6 +237,45 @@ it.describe('snapshots', () => { expect(divColor).toBe('rgb(0, 0, 255)'); await previewContext.close(); }); + + it('should restore scroll positions', async ({ page, contextFactory, toImpl, snapshotter, snapshotPort, browserName }) => { + it.skip(browserName === 'firefox'); + + await page.setContent(` + +
+ +
+ `); + + await (await page.$('text=Item 8')).scrollIntoViewIfNeeded(); + const snapshot = await snapshotter.captureSnapshot(toImpl(page), 'scrolled'); + + // Render snapshot, check expectations. + const previewContext = await contextFactory(); + const previewPage = await previewContext.newPage(); + await previewPage.goto(`http://localhost:${snapshotPort}/snapshot/`); + await previewPage.evaluate(snapshotId => { + (window as any).showSnapshot(snapshotId); + }, `${snapshot.snapshot().pageId}?name=scrolled`); + const div = await previewPage.frames()[1].waitForSelector('div'); + await previewPage.frames()[1].waitForLoadState(); + expect(await div.evaluate(div => div.scrollTop)).toBe(136); + }); }); function distillSnapshot(snapshot) {