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

Add waitForPageReady utility #361

Merged
merged 1 commit into from
Oct 25, 2023
Merged
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
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 @@ -559,6 +559,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 @@ -568,6 +569,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);
};
Loading