Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly load rrweb-snapshot on AMD-based web pages #210

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/little-donuts-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@chromatic-com/playwright': patch
---

Add support for AMD module-based web pages
14 changes: 13 additions & 1 deletion packages/playwright/src/takeSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,19 @@ async function takeSnapshot(
// Serialize and capture the DOM
const domSnapshot: elementNode = await page.evaluate(dedent`
${rrweb};
rrwebSnapshot.snapshot(document);
// page.evaluate returns the value of the function being evaluated. In this case, it means that
// it is returning either the resolved value of the Promise or the return value of the call to
// the snapshot function. See https://playwright.dev/docs/api/class-page#page-evaluate.
if (typeof define === "function" && define.amd) {
skitterm marked this conversation as resolved.
Show resolved Hide resolved
// AMD support is detected, so we need to load rrwebSnapshot asynchronously
new Promise((resolve) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One small thing -- is there a reason we're wrapping this in a Promise here? Since we're not awaiting the results of the promise.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't quite understand why, but this doesn't work if I remove the Promise

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe playwright is waiting on the promise somehow? Maybe we should return new Promise... just to make that 100% clear.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return is implicit. See https://playwright.dev/docs/api/class-page#page-evaluate. I find it a bit confusing, but that seems to be how it functions. Just to be sure, I tried adding return in front of new Promise and it errored with Error: page.evaluate: SyntaxError: Illegal return statement.

Copy link
Member

@tmeasday tmeasday Nov 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see how if (x) { y } else { z } evaluates to anything, but clearly it does somehow (I assume we use the return value domSnapshot still right?).

Maybe there is some way to wrap this in a function (so you can return) to make it less confusing? Otherwise I'd be in favour of a ternary (x ? y : z) to make it less weird.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added additional comments to hopefully point out the odd behavior that page.evaluate displays, along with a link to Playwright doc. I'd like to avoid using a ternary since it's possible we might have other scenarios to account for in the future.

require(['rrwebSnapshot'], (rrwebSnapshot) => {
andrewortwein marked this conversation as resolved.
Show resolved Hide resolved
resolve(rrwebSnapshot.snapshot(document));
});
});
} else {
rrwebSnapshot.snapshot(document);
}
`);

const bufferedSnapshot = Buffer.from(JSON.stringify(domSnapshot));
Expand Down
6 changes: 6 additions & 0 deletions packages/playwright/tests/amd.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { test, expect } from '../src';

test('pages with AMD modules are archived', async ({ page }) => {
await page.goto('/amd');
await expect(page.getByText('Sum of')).toBeVisible();
});
14 changes: 14 additions & 0 deletions test-server/fixtures/amd.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<title>AMD Example</title>
<script
data-main="scripts/amd"
src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js"
></script>
</head>
<body>
This is a basic page with some math content below that is loaded from an AMD module:
<div id="output"></div>
</body>
</html>
12 changes: 12 additions & 0 deletions test-server/fixtures/assets/scripts/amd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
requirejs.config({
basePath: 'fixtures/amd',
paths: {
lodash: 'https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min',
},
});

require(['lodash'], function (_) {
const numbers = [1, 2, 3, 4, 5];
const sum = _.sum(numbers);
document.getElementById('output').textContent = `Sum of ${numbers.join('+')} = ${sum}`;
});
4 changes: 4 additions & 0 deletions test-server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ app.get('/constructable-stylesheets/:page', (req, res) => {
res.sendFile(path.join(__dirname, `fixtures/constructable-stylesheets/${page}.html`));
});

app.get('/amd', (req, res) => {
res.sendFile(path.join(__dirname, 'fixtures/amd.html'));
});

app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});
Loading