Skip to content

Commit

Permalink
Merge pull request #210 from chromaui/andrew/ap-5218-tweak-version-of…
Browse files Browse the repository at this point in the history
…-rrweb-used-by-chromatic-by-default

Properly load rrweb-snapshot on AMD-based web pages
  • Loading branch information
andrewortwein authored Nov 26, 2024
2 parents a2a8f09 + f78e3ce commit cb4dd2a
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 1 deletion.
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) {
// AMD support is detected, so we need to load rrwebSnapshot asynchronously
new Promise((resolve) => {
require(['rrwebSnapshot'], (rrwebSnapshot) => {
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}`);
});

0 comments on commit cb4dd2a

Please sign in to comment.