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

CI : Add visual regression testing workflow based on Playwright and Argos. #511

Merged
merged 17 commits into from
Feb 17, 2025
Merged
36 changes: 36 additions & 0 deletions .github/workflows/argos.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Argos CI Screenshots

on:
push:
branches: [master]
pull_request:
branches: [master]
gemanor marked this conversation as resolved.
Show resolved Hide resolved

jobs:
take-screenshots:
runs-on: ubuntu-latest
steps:
- name: Check out repository code
uses: actions/checkout@v4

- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version: current
asr2003 marked this conversation as resolved.
Show resolved Hide resolved

- name: Install dependencies
run: yarn install

- name: Install Playwright browsers
run: yarn playwright install --with-deps chromium

- name: Build the website
run: yarn docusaurus build
asr2003 marked this conversation as resolved.
Show resolved Hide resolved

- name: Take screenshots with Playwright
run: yarn playwright test

- name: Upload screenshots to Argos
env:
ARGOS_TOKEN: ${{ secrets.ARGOS_TOKEN }}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please try to remove it, see if we can run the workflow without it

Copy link
Contributor Author

@asr2003 asr2003 Feb 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe having a token is good choice so I think let it in

gemanor marked this conversation as resolved.
Show resolved Hide resolved
run: yarn argos upload ./screenshots
17 changes: 17 additions & 0 deletions docs/utils/argo_utils.ts
asr2003 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as cheerio from "cheerio";
import * as fs from "fs";

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";
}
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,17 @@
"got": "^12.1.0"
},
"devDependencies": {
"@argos-ci/cli": "^2.5.5",
"@argos-ci/playwright": "^4.1.0",
"@babel/core": "^7.26.0",
"@babel/plugin-transform-modules-commonjs": "^7.25.9",
"@babel/preset-env": "^7.26.0",
"@babel/preset-react": "^7.25.9",
"@babel/register": "^7.25.9",
"@playwright/test": "^1.50.0",
"@typescript-eslint/eslint-plugin": "4.22.0",
"@untitaker/hyperlink": "^0.1.32",
"cheerio": "^1.0.0",
"eslint": "^7.24.0",
"eslint-config-prettier": "^8.2.0",
"eslint-import-resolver-typescript": "^2.4.0",
Expand Down
19 changes: 19 additions & 0 deletions playwright.config.ts
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;
12 changes: 12 additions & 0 deletions screenshot.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
iframe,
.avatar__photo,
img[src$='.gif'],
.DocSearch-Button-Keys > kbd,
[class*='playgroundPreview'] {
visibility: hidden;
}

.theme-last-updated,
.docusaurus-mermaid-container {
display: none;
}
asr2003 marked this conversation as resolved.
Show resolved Hide resolved
34 changes: 34 additions & 0 deletions screenshot.spec.ts
asr2003 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import * as fs from 'fs';
import { test } from '@playwright/test';
import { argosScreenshot } from '@argos-ci/playwright';
import { extractSitemapPathnames, pathnameToArgosName } from './docs/utils/argo_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
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);
const filteredPathnames = pathnames.filter((pathname) => !pathname.includes('/1.0.0/'));
console.log('Filtered Pathnames:', filteredPathnames);

filteredPathnames.forEach(screenshotPathname);
});