Skip to content

Commit

Permalink
Merge pull request #361 from storybookjs/feat/wait-for-page-ready
Browse files Browse the repository at this point in the history
Add waitForPageReady utility
  • Loading branch information
yannbf committed Oct 25, 2023
2 parents 2d1c980 + e0c0bcf commit 6f93bd5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .storybook/test-runner.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { toMatchImageSnapshot } from 'jest-image-snapshot';
import { getStoryContext } from '../dist/playwright/hooks';
import { getStoryContext, waitForPageReady } from '../dist/playwright/hooks';
import type { TestRunnerConfig } from '../dist';

const snapshotsDir = process.env.SNAPSHOTS_DIR || '__snapshots__';
Expand All @@ -22,6 +22,8 @@ const config: TestRunnerConfig = {
return;
}

await waitForPageReady(page);

// Visual snapshot tests
const image = await page.screenshot({ fullPage: true });
expect(image).toMatchImageSnapshot({
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,7 @@ Here's a slightly different recipe for image snapshot testing:

```js
// .storybook/test-runner.js
const { waitForPageReady } = require('@storybook/test-runner');
const { toMatchImageSnapshot } = require('jest-image-snapshot');

const customSnapshotsDir = `${process.cwd()}/__snapshots__`;
Expand All @@ -587,6 +588,9 @@ module.exports = {
expect.extend({ toMatchImageSnapshot });
},
async postRender(page, context) {
// use the test-runner utility to wait for fonts to load, etc.
await waitForPageReady(page);

// If you want to take screenshot of multiple browsers, use
// page.context().browser().browserType().name() to get the browser name to prefix the file name
const image = await page.screenshot();
Expand Down
7 changes: 7 additions & 0 deletions src/playwright/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,10 @@ export const getStoryContext = async (page: Page, context: TestContext): Promise
storyId: context.id,
});
};

export const waitForPageReady = async (page: Page) => {
await page.waitForLoadState('domcontentloaded');
await page.waitForLoadState('load');
await page.waitForLoadState('networkidle');
await page.evaluate(() => document.fonts.ready);
};

0 comments on commit 6f93bd5

Please sign in to comment.