スクリーンショットのビジュアル差分
EXPERIMENTAL:スクリーンショットのビジュアル差分テストは現在大規模な開発中であり、安定した状態には達していません。 ただし、支援テストをいただければ幸いです。
視覚的回帰テストコマンド
stencil test --e2e --screenshot
簡単な例
Puppeteerは、スクリーンショットを比較するために使用されます。 1つを作成するには、e2eテストを設定する必要があります。例:
it('render something', async () => {
const page: E2EPage = await newE2EPage();
await page.setContent('<my-cmp></my-cmp>');
await page.compareScreenshot('My Component (...is beautiful. Look at it!)', {fullPage: false});
});
高度な例
describe('stencil-avatar', () => {
it('renders and responds to the size property', async () => {
const page = await newE2EPage();
// In order to test against any global styles you may have, don't forget to set the link to the global css. You don't have to do this if your stencil.config.ts file doesn't build a global css file with globalStyle.
await page.setContent('<link href="http://localhost:3333/build/stellar-core.css" rel="stylesheet" /><stencil-avatar size="small"></stencil-avatar>');
const element = await page.find('stencil-avatar');
expect(element).toHaveClass('hydrated');
// To start comparing the visual result, you first must run page.compareScreenshot; This will capture a screenshot, and save the file to "/screenshot/images". You'll be able to check that into your repo to provide those results to your team. You can only have one of these commands per test.
const results = await page.compareScreenshot();
// Finally, we can test against the previous screenshots.
// Test against hard pixels
expect(results).toMatchScreenshot({ allowableMismatchedPixels: 100 })
// Test against the percentage of changes. if 'allowableMismatchedRatio' is above 20% changed,
expect(results).toMatchScreenshot({ allowableMismatchedRatio: 0.2 })
});
});
テストを実行した後、プロジェクト内の /screenshot/compare.htmlページを開くことで、変更点やその違いを確認できます。
Current issues:
- body 内の要素の幅だけをスクリーンショットに収めます。現在は 600x600 ピクセルで撮影されるため、allowableMismatchedRatio を指定しても効果は限定的です。
await page.readjustSize()のような方法で Puppeteer のページを実際のコンテンツ幅に切り詰められれば、allowableMismatchedRatio がより役立ちます。 - さらにテストが必要です!