-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Visual regression testing with Argos (#27)
In preparation of upgrading a major version of Docusaurus and other dependencies, it would be nice if we can easily identify any visual regressions without manually checking each page with each dependency change. This will upload screenshots to Argos for comparison against a reference (the main branch). If a pull request results in visual differences, it will notify us. Idea from https://docusaurus.io/blog/upgrading-frontend-dependencies-with-confidence-using-visual-regression-testing
- Loading branch information
Showing
9 changed files
with
748 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,6 @@ | |
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
# screenshots for visual regression testing | ||
/screenshots |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import {devices} from '@playwright/test'; | ||
import type {PlaywrightTestConfig} from '@playwright/test'; | ||
|
||
const config: PlaywrightTestConfig = { | ||
webServer: { | ||
port: 3000, | ||
command: 'yarn docusaurus serve', | ||
}, | ||
projects: [ | ||
{ | ||
name: 'chromium', | ||
use: { | ||
...devices['Desktop Chrome'], | ||
}, | ||
}, | ||
], | ||
}; | ||
|
||
export default config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* Iframes can load lazily */ | ||
iframe, | ||
/* Gifs load lazily and are animated */ | ||
img[src$='.gif'], | ||
|
||
/* Different docs last-update dates can alter layout */ | ||
.theme-last-updated, | ||
/* Mermaid diagrams are rendered client-side and produce layout shifts */ | ||
.docusaurus-mermaid-container { | ||
display: none; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import * as fs from 'fs'; | ||
import {test} from '@playwright/test'; | ||
import {argosScreenshot} from '@argos-ci/playwright'; | ||
import {extractSitemapPathnames, pathnameToArgosName} from './utils'; | ||
|
||
// Constants | ||
const siteUrl = 'http://localhost:3000'; | ||
const sitemapPath = './build/sitemap.xml'; | ||
const stylesheetPath = './screenshot.css'; | ||
const stylesheet = fs.readFileSync(stylesheetPath).toString(); | ||
|
||
// Wait for hydration, requires Docusaurus v2.4.3+ | ||
// Docusaurus adds a <html data-has-hydrated="true"> once hydrated | ||
// See https://github.com/facebook/docusaurus/pull/9256 | ||
function waitForDocusaurusHydration() { | ||
return document.documentElement.dataset.hasHydrated === 'true'; | ||
} | ||
|
||
function screenshotPathname(pathname: string) { | ||
test(`pathname ${pathname}`, async ({page}) => { | ||
const url = siteUrl + pathname; | ||
await page.goto(url); | ||
await page.waitForFunction(waitForDocusaurusHydration); | ||
await page.addStyleTag({content: stylesheet}); | ||
await argosScreenshot(page, pathnameToArgosName(pathname)); | ||
}); | ||
} | ||
|
||
test.describe('Docusaurus site screenshots', () => { | ||
const pathnames = extractSitemapPathnames(sitemapPath); | ||
console.log('Pathnames to screenshot:', pathnames); | ||
pathnames.forEach(screenshotPathname); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import * as cheerio from 'cheerio'; | ||
import * as fs from 'fs'; | ||
|
||
// Extract a list of pathnames, given a fs path to a sitemap.xml file | ||
// Docusaurus generates a build/sitemap.xml file for you! | ||
export function extractSitemapPathnames(sitemapPath: string): string[] { | ||
const sitemap = fs.readFileSync(sitemapPath).toString(); | ||
const $ = cheerio.load(sitemap, {xmlMode: true}); | ||
const urls: string[] = []; | ||
$('loc').each(function handleLoc() { | ||
urls.push($(this).text()); | ||
}); | ||
return urls.map((url) => new URL(url).pathname); | ||
} | ||
|
||
// Converts a pathname to a decent screenshot name | ||
export function pathnameToArgosName(pathname: string): string { | ||
return pathname.replace(/^\/|\/$/g, '') || 'index'; | ||
} |
Oops, something went wrong.