Skip to content

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

chore: remove unnecessary `__pw_link` attribute (#38374)

Dmitry Gozmancd03fe9904e7
diff --git a/packages/trace-viewer/src/sw/snapshotRenderer.ts b/packages/trace-viewer/src/sw/snapshotRenderer.ts
index e08b726862..6473688e6e 100644
--- a/packages/trace-viewer/src/sw/snapshotRenderer.ts
+++ b/packages/trace-viewer/src/sw/snapshotRenderer.ts
@@ -128,9 +128,7 @@ export class SnapshotRenderer {
attrName = '_' + attrName;
}
let attrValue = value;
- if (isAnchor && attr.toLowerCase() === 'href')
- result.push(' ', '__pw_link');
- else if (attr.toLowerCase() === 'href' || attr.toLowerCase() === 'src' || attr === kCurrentSrcAttribute)
+ if (!isAnchor && (attr.toLowerCase() === 'href' || attr.toLowerCase() === 'src' || attr === kCurrentSrcAttribute))
attrValue = rewriteURLForCustomProtocol(value);
result.push(' ', attrName, '="', escapeHTMLAttribute(attrValue), '"');
}
@@ -371,10 +369,8 @@ function snapshotScript(viewport: ViewportSize, ...targetIds: (string | undefine
visit(shadowRoot);
}
- for (const element of root.querySelectorAll('[__pw_link]')) {
+ for (const element of root.querySelectorAll('a'))
element.addEventListener('click', event => { event.preventDefault(); });
- element.removeAttribute('__pw_link');
- }
if ('adoptedStyleSheets' in (root as any)) {
const adoptedSheets: CSSStyleSheet[] = [...(root as any).adoptedStyleSheets];
diff --git a/tests/library/snapshotter.spec.ts b/tests/library/snapshotter.spec.ts
index 05e9b57919..12f8681f89 100644
--- a/tests/library/snapshotter.spec.ts
+++ b/tests/library/snapshotter.spec.ts
@@ -260,19 +260,6 @@ it.describe('snapshots', () => {
// Second snapshot should be just a copy of the first one.
expect(snapshot2.html).toEqual([[1, 13]]);
});
-
- it('should not navigate on anchor clicks', async ({ page, toImpl, snapshotter, browser }) => {
- const url = 'http://localhost/';
- await page.setContent(`<a href="${url}">example link</a>`);
- const snapshot = await snapshotter.captureSnapshot(toImpl(page), 'call@1', 'snapshot@call@1');
- expect(distillSnapshot(snapshot, page.url())).toBe(`<A __pw_link href="${url}">example link</A>`);
-
- const { html } = snapshot.render();
- const newPage = await browser.newPage();
- await newPage.setContent(html);
- await newPage.getByRole('link').click();
- await expect(newPage).not.toHaveURL(url, { timeout: 500 });
- });
});
function distillSnapshot(snapshot, url: string, options: { distillTarget: boolean, distillBoundingRect: boolean } = { distillTarget: true, distillBoundingRect: true }) {
diff --git a/tests/library/trace-viewer.spec.ts b/tests/library/trace-viewer.spec.ts
index 115947e164..2450ca6998 100644
--- a/tests/library/trace-viewer.spec.ts
+++ b/tests/library/trace-viewer.spec.ts
@@ -2101,3 +2101,32 @@ test('should survive service worker restart', async ({ page, runAndTrace, server
const snapshot2 = await traceViewer.snapshotFrame('Set content');
await expect(snapshot2.locator('body')).toHaveText('Old world');
});
+
+test('should not navigate on anchor clicks', async ({ runAndTrace, page, server }) => {
+ const url = server.EMPTY_PAGE;
+ const traceViewer = await runAndTrace(async () => {
+ await page.setContent(`
+ <a href="${url}">link1</a>
+ <a href="${url}" target="_blank">link2</a>
+ <a href="${url}" target="_top">link3</a>
+ `);
+ });
+ const snapshot = await traceViewer.snapshotFrame('Set content');
+ const frame = await snapshot.owner().elementHandle().then(handle => handle.contentFrame());
+
+ const checkLink = async (name: string) => {
+ let newPageOpened = false;
+ traceViewer.page.context().on('page', () => {
+ newPageOpened = true;
+ });
+ await snapshot.getByRole('link', { name }).click();
+ await traceViewer.page.waitForTimeout(500);
+ await expect(traceViewer.page).not.toHaveURL(url, { timeout: 500 });
+ expect(frame.url()).not.toBe(url);
+ expect(newPageOpened).toBe(false);
+ };
+
+ await checkLink('link1');
+ await checkLink('link2');
+ await checkLink('link3');
+});