-
Notifications
You must be signed in to change notification settings - Fork 0
/
screenshot.spec.ts
50 lines (45 loc) · 1.51 KB
/
screenshot.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import * as fs from 'fs'
import * as cheerio from 'cheerio'
import { test } from '@playwright/test'
import { argosScreenshot } from '@argos-ci/playwright'
// Constants:
const siteUrl = 'http://localhost:4321'
const sitemapPath = './.vercel/output/static/sitemap-0.xml'
const stylesheetPath = './screenshot.css'
const stylesheet = fs.readFileSync(stylesheetPath).toString()
const 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)
}
const pathnameToArgosName = ({
browserName,
isMobile,
pathname
}: {
browserName: string
isMobile: boolean
pathname: string
}): string => {
return `${browserName}/${isMobile ? 'mobile' : 'desktop'}/${
pathname.replace(/^\/|\/$/g, '') || 'index'
}`
}
const screenshotPathname = (pathname: string) => {
test(`pathname ${pathname}`, async ({ page, browserName, isMobile }) => {
const url = siteUrl + pathname
await page.goto(url)
await page.waitForLoadState('networkidle') // Wait redirect pages
await page.addStyleTag({ content: stylesheet })
await argosScreenshot(page, pathnameToArgosName({ browserName, pathname, isMobile }))
})
}
test.describe('Site screenshots', () => {
const pathnames = extractSitemapPathnames(sitemapPath)
console.log('Pathnames to screenshot:', pathnames)
pathnames.forEach(screenshotPathname)
})