Skip to content

Commit 4a94c16

Browse files
committed
Implement snapshots in Playwright
This could be used for visual regression testing
1 parent ab72a50 commit 4a94c16

File tree

6 files changed

+99
-1
lines changed

6 files changed

+99
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ assets/build/*
1212
/e2e-report/
1313
/playwright/.cache/
1414
/playwright/.auth/
15+
tests/e2e/*.spec.js-snapshots/
1516
.env

playwright.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const dotenv = require('dotenv');
44
const testDir = './tests/e2e';
55
const envFile = process.env.CI ? `${testDir}/env/.env.ci` : `${testDir}/env/.env.default`;
66
dotenv.config({path: envFile});
7-
dotenv.config({override: true});// prioritize .env file if exists
7+
dotenv.config({override: true}); // prioritize .env file if exists
88

99
/**
1010
* @see https://playwright.dev/docs/test-configuration
@@ -113,6 +113,7 @@ const config = {
113113
// command: 'npm run start',
114114
// port: 3000,
115115
// },
116+
globalSetup: require.resolve('./tests/e2e/tools/setup/global-setup'),
116117
};
117118

118119
module.exports = config;

tests/e2e/snapshots.spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import {test, expect} from './tools/lib/test-utils.js';
2+
import {readSiteMap} from './tools/setup/sitemap.js';
3+
4+
test('Snapshots', async ({page}) => {
5+
const urls = await readSiteMap();
6+
for (const url of urls) {
7+
await page.goto(url);
8+
await expect(page).toHaveScreenshot();
9+
}
10+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const playwright = require('@playwright/test');
2+
3+
import {createSiteMap, readSiteMap} from './sitemap';
4+
5+
export default async () => {
6+
// only create site map if it doesn't already exist
7+
const sitemap = await readSiteMap();
8+
if (sitemap) {
9+
return;
10+
}
11+
12+
// launch browser and initiate crawler
13+
let browser = playwright.devices['Desktop Chrome'].defaultBrowserType;
14+
browser = await playwright[browser].launch();
15+
const page = await browser.newPage();
16+
await createSiteMap(process.env.WP_BASE_URL, page);
17+
await browser.close();
18+
};

tests/e2e/tools/setup/sitemap.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import {readFileSync, writeFileSync} from 'node:fs';
2+
import {join} from 'node:path';
3+
4+
const extractLocalLinks = baseURL => {
5+
const urls = new Set();
6+
const offset = baseURL.length;
7+
for (const {href} of document.links) {
8+
if (href.startsWith(baseURL)) {
9+
const path = href.slice(offset);
10+
urls.add(path);
11+
}
12+
}
13+
return Array.from(urls);
14+
};
15+
16+
const ENTRY_POINT = '/topics';
17+
const SITEMAP = join(__dirname, './sitemap.json');
18+
19+
/**
20+
* Determines URLs and writes them to disk.
21+
*
22+
* @param {string} baseURL - The site's base URL.
23+
* @param {Object} page - The current page object.
24+
*/
25+
const createSiteMap = async (baseURL, page) => {
26+
await page.goto(baseURL + ENTRY_POINT);
27+
const urls = await page.evaluate(extractLocalLinks, baseURL);
28+
const data = JSON.stringify(urls, null, 4);
29+
writeFileSync(SITEMAP, data, {encoding: 'utf-8'});
30+
};
31+
32+
/**
33+
* Reads any previously created site map from disk.
34+
*
35+
* @return {Object} Sitemap data.
36+
*/
37+
const readSiteMap = async () => {
38+
let data = null;
39+
try {
40+
data = readFileSync(SITEMAP, {encoding: 'utf-8'});
41+
} catch (err) {
42+
return null;
43+
}
44+
return JSON.parse(data);
45+
};
46+
47+
export {createSiteMap, readSiteMap};

tests/e2e/tools/setup/sitemap.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[
2+
"/topics#header",
3+
"/topics#content",
4+
"/topics#footer",
5+
"/",
6+
"/take-action/",
7+
"/petitions/consectetur-adipiscing/",
8+
"/petitions/vestibulum-placerat/",
9+
"/petitions/consectetur-adipiscing-elit/",
10+
"/get-informed/",
11+
"/get-informed/energy/",
12+
"/get-informed/nature/",
13+
"/get-informed/people/",
14+
"/about-us-2/",
15+
"/press-center/",
16+
"/sitemap/",
17+
"/privacy-and-cookies/",
18+
"/community-policy/",
19+
"/terms/",
20+
"/copyright/"
21+
]

0 commit comments

Comments
 (0)