Skip to content

Commit 16f08bb

Browse files
committed
Take screenshots of whole website
I need to update docusaurus next. I want to have before and after screenshots I'll run them through a diff thing. we can turn off the github action later if we want. It should only run on merge to main
1 parent d178f10 commit 16f08bb

File tree

8 files changed

+17357
-4
lines changed

8 files changed

+17357
-4
lines changed

.github/workflows/screenshots.yml

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: CI screenshots
2+
on:
3+
push:
4+
branches: main
5+
6+
jobs:
7+
screenshots:
8+
runs-on: gha-runner-x64
9+
steps:
10+
- name: Check out repository code
11+
uses: actions/checkout@v4
12+
13+
- name: Use Node.js
14+
uses: actions/setup-node@v4
15+
with:
16+
node-version: current
17+
18+
19+
run: |
20+
corepack enable && corepack prepare [email protected]
21+
22+
- name: Install dependencies
23+
run: yarn install --frozen-lockfile
24+
25+
- name: Install Playwright browsers
26+
run: yarn playwright install --with-deps chromium
27+
28+
- name: Build the website
29+
run: yarn docusaurus build
30+
31+
- name: Take screenshots with Playwright
32+
run: yarn playwright test
33+
34+
- uses: actions/upload-artifact@v4
35+
if: always()
36+
with:
37+
name: screenshots
38+
path: ./screenshots
39+
retention-days: 7

e2e/screenshot.css

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* Iframes can load lazily */
2+
iframe,
3+
/* Avatars can be flaky due to using external sources: GitHub/Unavatar */
4+
.avatar__photo,
5+
/* Gifs load lazily and are animated */
6+
img[src$='.gif'],
7+
/* Algolia keyboard shortcuts appear with a little delay */
8+
.DocSearch-Button-Keys > kbd,
9+
/* The live playground preview can often display dates/counters */
10+
[class*='playgroundPreview'] {
11+
visibility: hidden;
12+
}
13+
14+
/* Different docs last-update dates can alter layout */
15+
.theme-last-updated,
16+
/* Mermaid diagrams are rendered client-side and produce layout shifts */
17+
.docusaurus-mermaid-container {
18+
display: none;
19+
}

e2e/screenshot.spec.ts

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import * as fs from 'fs';
2+
import {test} from '@playwright/test';
3+
import {extractSitemapPathnames, pathnameToArgosName} from './utils';
4+
5+
// Constants
6+
const siteUrl = 'http://localhost:3000';
7+
const sitemapPath = './build/sitemap.xml';
8+
const stylesheetPath = './e2e/screenshot.css';
9+
const stylesheet = fs.readFileSync(stylesheetPath).toString();
10+
11+
// Wait for hydration, requires Docusaurus v2.4.3+
12+
// Docusaurus adds a <html data-has-hydrated="true"> once hydrated
13+
// See https://github.com/facebook/docusaurus/pull/9256
14+
function waitForDocusaurusHydration() {
15+
return document.documentElement.dataset.hasHydrated === 'true';
16+
}
17+
18+
function screenshotPathname(pathname: string) {
19+
test(`pathname ${pathname}`, async ({page}) => {
20+
const url = siteUrl + pathname;
21+
await page.goto(url);
22+
await page.waitForFunction(waitForDocusaurusHydration);
23+
await page.addStyleTag({content: stylesheet});
24+
const spath = `./screenshots/${pathnameToArgosName(pathname)}.png`
25+
await page.screenshot({ path: spath, fullPage: true });
26+
});
27+
}
28+
29+
test.describe('Docusaurus site screenshots', () => {
30+
const pathnames = extractSitemapPathnames(sitemapPath);
31+
console.log('Pathnames to screenshot:', pathnames);
32+
pathnames.forEach(screenshotPathname);
33+
});

e2e/utils.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import * as cheerio from 'cheerio';
2+
import * as fs from 'fs';
3+
4+
// Extract a list of pathnames, given a fs path to a sitemap.xml file
5+
// Docusaurus generates a build/sitemap.xml file for you!
6+
export function extractSitemapPathnames(sitemapPath: string): string[] {
7+
const sitemap = fs.readFileSync(sitemapPath).toString();
8+
const $ = cheerio.load(sitemap, {xmlMode: true});
9+
const urls: string[] = [];
10+
$('loc').each(function handleLoc() {
11+
urls.push($(this).text());
12+
});
13+
return urls.map((url) => new URL(url).pathname);
14+
}
15+
16+
// Converts a pathname to a decent screenshot name
17+
export function pathnameToArgosName(pathname: string): string {
18+
return pathname.replace(/^\/|\/$/g, '') || 'index';
19+
}

0 commit comments

Comments
 (0)