Skip to content

Commit

Permalink
Allow snapshot strategy to be configured
Browse files Browse the repository at this point in the history
This matches the `snapshotStrategy` option we recently added to
happo-cypress:

happo/happo-cypress#122

The clip strategy means the whole DOM is included in the snapshot, and
happo workers will clip the screenshot based on where the
`[data-happo-clip]` element is located. This data attribute is added by
happo-e2e when using the clip snapshot strategy:

happo/happo-e2e#54
  • Loading branch information
lencioni committed Jan 24, 2025
1 parent 1933565 commit 286b994
Show file tree
Hide file tree
Showing 5 changed files with 500 additions and 22 deletions.
14 changes: 12 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ module.exports = {
await controller.finish();
},

async screenshot(page, handleOrLocator, { component, variant, ...rest }) {
async screenshot(
page,
handleOrLocator,
{ component, variant, snapshotStrategy = 'hoist', ...rest },
) {
if (!controller.isActive()) {
return;
}
Expand All @@ -57,11 +61,17 @@ module.exports = {
const elementHandle = handleOrLocator.elementHandle
? await handleOrLocator.elementHandle()
: handleOrLocator;

const snapshot = await page.evaluate(
(element) =>
window.happoTakeDOMSnapshot({ doc: element.ownerDocument, element }),
window.happoTakeDOMSnapshot({
doc: element.ownerDocument,
element,
strategy: snapshotStrategy,
}),
elementHandle,
);

await controller.registerSnapshot({
...snapshot,
component,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"devDependencies": {
"@playwright/test": "^1.46.0",
"eslint": "^8.2.0",
"happo-e2e": "^2.0.1",
"happo-e2e": "^3.3.1",
"happo.io": "^6.8.0",
"next": "^12.0.4",
"react": "^17.0.2",
Expand Down
8 changes: 8 additions & 0 deletions pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ export default function IndexPage() {
<h1>Hello world</h1>
<a href="/goodbye">goodbye</a>
<CanvasImage />

<div style={{ width: 200 }}>
I am 200px wide.
<div style={{ width: '100%' }} id="stretch-to-parent">
I stretch to 100% width of my parent, which is 200px wide.
</div>
</div>

<style>{`
h1 {
color: red;
Expand Down
14 changes: 14 additions & 0 deletions tests/app.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ test('basic test', async ({ page }) => {
],
});

const stretchToParent = page.locator('#stretch-to-parent');

await happoPlaywright.screenshot(page, stretchToParent, {
component: 'StretchToParent',
variant: 'snapshotStrategy hoist',
snapshotStrategy: 'hoist',
});

await happoPlaywright.screenshot(page, stretchToParent, {
component: 'StretchToParent',
variant: 'snapshotStrategy hoist',
snapshotStrategy: 'clip',
});

await page.click('text=goodbye');

await expect(page.locator('text=Sad to see you go').first()).toBeVisible();
Expand Down
Loading

0 comments on commit 286b994

Please sign in to comment.