Skip to content

Commit

Permalink
[WIP]
Browse files Browse the repository at this point in the history
  • Loading branch information
ss-o committed Nov 30, 2023
1 parent 7ee42ca commit b4efc46
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
33 changes: 33 additions & 0 deletions screenshot.spec.ts
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);
});
19 changes: 19 additions & 0 deletions utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as fs from "fs";
import * as cheerio from "cheerio";

// 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";
}

0 comments on commit b4efc46

Please sign in to comment.