Skip to content

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

fix(tracing): workaround chromium scrollTop slowness (#14114)

Dmitry Gozmanf6712ec43ae7
diff --git a/packages/playwright-core/src/server/trace/recorder/snapshotterInjected.ts b/packages/playwright-core/src/server/trace/recorder/snapshotterInjected.ts
index 03f9b86543..4b8798171d 100644
--- a/packages/playwright-core/src/server/trace/recorder/snapshotterInjected.ts
+++ b/packages/playwright-core/src/server/trace/recorder/snapshotterInjected.ts
@@ -44,6 +44,7 @@ export function frameSnapshotStreamer(snapshotStreamer: string) {
const kScrollTopAttribute = '__playwright_scroll_top_';
const kScrollLeftAttribute = '__playwright_scroll_left_';
const kStyleSheetAttribute = '__playwright_style_sheet_';
+ const kTargetAttribute = '__playwright_target__';
// Symbols for our own info on Nodes/StyleSheets.
const kSnapshotFrameId = Symbol('__playwright_snapshot_frameid_');
@@ -273,6 +274,28 @@ export function frameSnapshotStreamer(snapshotStreamer: string) {
// Ensure we are up to date.
this._handleMutations(this._observer.takeRecords());
+ // Restore scroll positions for all ancestors of action target elements
+ // that will show the highlight/red dot in the trace viewer.
+ // Workaround for chromium regression:
+ // https://bugs.chromium.org/p/chromium/issues/detail?id=1324419
+ // https://github.com/microsoft/playwright/issues/14037
+ // TODO: remove after chromium is fixed?
+ const elementsToRestoreScrollPosition = new Set<Node>();
+ const findElementsToRestoreScrollPositionRecursively = (element: Element) => {
+ let shouldAdd = element.hasAttribute(kTargetAttribute);
+ for (let child = element.firstElementChild; child; child = child.nextElementSibling)
+ shouldAdd = shouldAdd || findElementsToRestoreScrollPositionRecursively(child);
+ if (element.shadowRoot) {
+ for (let child = element.shadowRoot.firstElementChild; child; child = child.nextElementSibling)
+ shouldAdd = shouldAdd || findElementsToRestoreScrollPositionRecursively(child);
+ }
+ if (shouldAdd)
+ elementsToRestoreScrollPosition.add(element);
+ return shouldAdd;
+ };
+ if (document.documentElement)
+ findElementsToRestoreScrollPositionRecursively(document.documentElement);
+
const visitNode = (node: Node | ShadowRoot): { equals: boolean, n: NodeSnapshot } | undefined => {
const nodeType = node.nodeType;
const nodeName = nodeType === Node.DOCUMENT_FRAGMENT_NODE ? 'template' : node.nodeName;
@@ -380,12 +403,12 @@ export function frameSnapshotStreamer(snapshotStreamer: string) {
expectValue(value);
attrs[kSelectedAttribute] = value;
}
- if (element.scrollTop) {
+ if (elementsToRestoreScrollPosition.has(element) && element.scrollTop) {
expectValue(kScrollTopAttribute);
expectValue(element.scrollTop);
attrs[kScrollTopAttribute] = '' + element.scrollTop;
}
- if (element.scrollLeft) {
+ if (elementsToRestoreScrollPosition.has(element) && element.scrollLeft) {
expectValue(kScrollLeftAttribute);
expectValue(element.scrollLeft);
attrs[kScrollLeftAttribute] = '' + element.scrollLeft;