-
Notifications
You must be signed in to change notification settings - Fork 37
/
screenshots.js
executable file
·483 lines (432 loc) · 13.8 KB
/
screenshots.js
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#!/usr/bin/env node
let puppeteer
try {
puppeteer = require('puppeteer')
} catch (e) {
console.error(e)
console.log(
'Could not import puppeteer, you should install it if you want to take screenshots'
)
process.exit(1)
}
const path = require('path')
const fs = require('fs')
const sortBy = require('lodash/sortBy')
const flattenDeep = require('lodash/flattenDeep')
const stackExampleApp = require('../examples/stack')
const { ArgumentParser } = require('argparse')
const rootDirectory = path.join(__dirname, '../')
const emptyDirectory = directory => {
for (const filename of fs.readdirSync(directory)) {
fs.unlinkSync(path.join(directory, filename))
}
}
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
const defaultGetScreenshotName = ({ component, viewport, suffix }) =>
`${component.testId}-${suffix ? `${suffix}-` : ''}${formatViewport(
viewport
)}.png`
const formatViewport = viewport => `${viewport.width}x${viewport.height}`
const pushAll = (arr, items) => arr.push.apply(arr, items)
/**
* Screenshot a component to the screenshot directory, taking care of
* resizing the viewport before-hand so that the viewport fits the
* component.
*/
const screenshotComponent = async (page, options) => {
const { component, screenshotDir, viewport } = options
const { link, name } = component
await page.goto(link, { waitUntil: 'load', timeout: 0 })
await sleep(100)
const getScreenshotName =
options.getScreenshotName || defaultGetScreenshotName
const screenshot = async suffix => {
await page.screenshot({
path: path.join(
screenshotDir,
getScreenshotName({ component, viewport, suffix })
),
fullPage: true,
captureBeyondViewport: false
})
}
await page.bringToFront()
if (options.componentConfig && options.componentConfig.script) {
const componentScript = require(path.join(
rootDirectory,
options.componentConfig.script
))
console.log(`Executing custom script for ${name}`)
await componentScript(page, screenshot)
} else {
console.log(`Screenshotting ${name} at ${formatViewport(viewport)}`)
await screenshot()
}
}
const getComponentNameFromTestId = testId => {
return testId.split('-example-')[0]
}
const LINK_BASE = 'file://'
const formatLink = (parsedStyleguideURL, relativeLink) => {
if (parsedStyleguideURL.protocol === 'file:') {
return `file://${relativeLink}`
} else {
return `${parsedStyleguideURL.toString()}${relativeLink}`
}
}
/**
* Fetch all available components on the styleguide and returns an array
* of { name, link } describing each component.
* Components are sorted by name.
*/
const fetchAllComponents = async (page, args, config) => {
const styleguideIndexURL = `${args.styleguideUrl}/index.html`
console.log(`Opening styleguide ${styleguideIndexURL}`)
await page.goto(styleguideIndexURL, {
waitUntil: 'load',
timeout: 0
})
console.log('Extracting links')
// We want to take screenshot for individual example, so we :
// - extract categories (link from the side menu with no ?id=)
// - go to category's page
// - look for `.rsg--controls-40 a` which is the open isolated for examples
// - look for its closest data-testid to get the name
const categoriesName = await page.evaluate(() => {
const sidebarSelector = '.rsg--sidebar-4'
return Array.from(document.querySelectorAll(`${sidebarSelector} a`))
.filter(v => !v.href.includes('?id='))
.map(x => x.text)
})
const sortedCategoriesNames = sortBy(
categoriesName.map(catName => ({
link: styleguideIndexURL + '#/' + catName,
name: catName
})),
x => x.name
)
const allLinks = []
const parsedStyleguideURL = new URL(args.styleguideUrl)
for (const cate of sortedCategoriesNames) {
await page.goto(cate.link, { waitUntil: 'load', timeout: 0 })
await sleep(100)
const componentLinks = flattenDeep(
await page.evaluate(config => {
const componentSectionSelector = '.rsg--root-23'
const exampleToolbarSelector = '.rsg--toolbar-41'
const componentToolbarSelector = '.rsg--toolbar-11'
const openIsolatedButtonSelector = '.rsg--button-21'
const componentContainers = document.querySelectorAll(
componentSectionSelector
)
return Array.from(componentContainers, componentContainer => {
const componentId = componentContainer.dataset.testid.replace(
'-container',
''
)
const perExampleScreenshot =
config[componentId] && config[componentId].perExampleScreenshot
const isolateButtons = componentContainer.querySelectorAll(
`${
perExampleScreenshot
? exampleToolbarSelector
: componentToolbarSelector
} ${openIsolatedButtonSelector}`
)
return Array.from(isolateButtons).map(btn => {
const testId = btn.dataset.testid.replace('-isolate-button', '')
return {
testId,
link: btn.getAttribute('href')
}
})
})
}, config)
)
pushAll(
allLinks,
componentLinks.map(link => ({
...link,
link: formatLink(parsedStyleguideURL, link.link),
name: getComponentNameFromTestId(link.testId)
}))
)
}
return allLinks
}
/**
* Ensure directories are ready for taking screenshots.
*
* - Throws if styleguide has not been built
* - Creates the screenshot dir if it does not exist
*
* @param {string} options.screenshotDir - Where to store screenshots
* @param {boolean} options.emptyScreenshotDir - Whether to empty the screenshot dir
*
*/
const prepareFS = async options => {
const { screenshotDir, emptyScreenshotDir } = options
if (!fs.existsSync(screenshotDir)) {
console.log(`Creating screenshot directory ${screenshotDir}`)
fs.mkdirSync(screenshotDir)
} else if (emptyScreenshotDir) {
console.log(`Emptying screenshot directory ${screenshotDir}`)
emptyDirectory(screenshotDir)
}
}
/**
* Opens and configure browser and page.
* Returns { browser, page }
*/
const prepareBrowser = async options => {
const browser = await puppeteer.launch()
const page = await browser.newPage()
// Put Argos in user agent
await page.setUserAgent(
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36; Argos'
)
await page.emulateMediaFeatures([
{ name: 'prefers-reduced-motion', value: 'reduce' }
])
page.setViewport(options.viewport)
await page.setDefaultNavigationTimeout(0)
console.log('Browser opened and set up')
return { browser, page }
}
const pathArgument = p => {
if (p.startsWith('/')) {
return p
} else {
return path.join(process.cwd(), p)
}
}
const builtinViewports = {
mobile: '320x480',
desktop: '800x600'
}
const parseViewportArgument = viewportStr => {
viewportStr = builtinViewports[viewportStr] || viewportStr
const splitted = viewportStr.split('x')
if (!splitted[1]) {
console.warn(
`Viewport format unsupported (${viewportStr}), supported format example: 800x600. You can also use built-in viewport names: ${Object.keys(
builtinViewports
).join(', ')}.`
)
throw new Error('Bad viewport format')
}
return { width: parseInt(splitted[0]), height: parseInt(splitted[1]) }
}
const readConfig = async () => {
const configPath = path.join(process.cwd(), 'rsgscreenshots.json')
if (fs.existsSync(configPath)) {
return JSON.parse(fs.readFileSync(configPath))
} else {
return {}
}
}
const loadJSON = filename => {
try {
return JSON.parse(fs.readFileSync(filename).toString())
} catch {
return null
}
}
const cacheToDisk = (fnToCache, options) =>
async function() {
const { cacheFile } = options
let res
if (cacheFile) {
res = loadJSON(cacheFile)
}
if (res) {
options.onLoadCache && options.onLoadCache()
} else {
res = await fnToCache.apply(this, arguments)
if (cacheFile) {
fs.writeFileSync(cacheFile, JSON.stringify(res, null, 2))
options.onSaveCache && options.onSaveCache()
}
}
return res
}
const screenshotReactStyleguide = async (page, args, config) => {
const cachedFetchAllComponents = cacheToDisk(fetchAllComponents, {
cacheFile: args.cacheFile,
onLoadCache: () =>
console.log(`Using cached component list from ${args.cacheFile}`),
onSaveCache: () => console.log(`Saved component list to ${args.cacheFile}`)
})
let components = await cachedFetchAllComponents(page, args, config)
if (args.component) {
components = components.filter(component =>
component.name.includes(args.component)
)
}
console.log('Screenshotting components')
for (const component of components) {
const componentConfig = config[component.name] || {}
const componentViewportSpec =
(componentConfig.viewports && componentConfig.viewports[args.viewport]) ||
null
const componentViewport = componentViewportSpec
? parseViewportArgument(componentViewportSpec)
: parseViewportArgument(args.viewport)
await page.setViewport(componentViewport)
await screenshotComponent(page, {
component,
componentConfig,
screenshotDir: args.screenshotDir,
viewport: componentViewport
})
}
}
const screenshotStackExamples = async (page, args) => {
return new Promise(async (resolve, reject) => {
try {
const port = 3000
const examples = [
{ name: 'connection', url: 'connection' },
{ name: 'connection-red', url: 'connection?theme=red' }
]
const server = stackExampleApp.listen(port, async () => {
console.log(
`Stack examples server listening at http://localhost:${port}`
)
for (let example of examples) {
await page.goto(`http://localhost:3000/${example.url}`, {
waitUntil: 'load',
timeout: 0
})
await page.bringToFront()
await page.screenshot({
path: path.join(
args.screenshotDir,
`stack-example-${example.name}.png`
),
fullPage: true
})
}
server.close()
resolve()
})
} catch (e) {
reject(e)
}
})
}
const screenshotKSSStyleguide = async (page, args) => {
const resolveLink = relativePage =>
`${LINK_BASE}${path.join(args.kssDir, relativePage)}`
const kssPage = resolveLink('index.html')
await page.goto(kssPage)
// We do not screenshot utilities because
// - their screenshots are huge
// - they do not change often and the probability of mistake is low
const ignore = [/^utilities/]
const WIDTH = 800
await page.setViewport({ width: WIDTH, height: 800 })
const links = await page.evaluate(() => {
const navLinks = Array.from(
document.querySelectorAll('.kss-nav > .kss-nav__item > a[href]')
)
return navLinks.map(node => node.getAttribute('href'))
})
for (const link of links) {
await page
await page.goto(resolveLink(link))
const sections = Array.from(await page.$$('.kss-section--depth-2')).concat(
Array.from(await page.$$('.kss-section--depth-3'))
)
for (let section of sections) {
const idProp = await section.getProperty('id')
const idValue = await idProp.jsonValue()
const id = idValue.replace('kssref-', '')
const ref = await section.$eval('.kss-section__ref', el => el.textContent)
if (ignore.filter(rx => rx.exec(id)).length > 0) {
console.log('Ignoring', id)
continue
}
// Need to resize the viewport otherwise screenshots are blank
const body = await page.$('body')
const bodySize = await body.boundingBox()
await page.setViewport({
height: Math.ceil(bodySize.height),
width: WIDTH
})
console.log('Screenshotting section', idValue)
await page.screenshot({
clip: await section.boundingBox(),
path: path.join(args.screenshotDir, `kss-${ref}-${id}.png`)
})
}
}
}
const urlArgument = rawURL => {
const c = new URL(rawURL)
return rawURL
}
/**
* Fetches all components from styleguide and takes a screenshot of each.
*/
const main = async () => {
const parser = new ArgumentParser()
parser.addArgument('--mode', {
choices: ['react', 'stack', 'kss'],
defaultValue: 'react'
})
parser.addArgument('--screenshot-dir', {
required: true,
dest: 'screenshotDir',
type: pathArgument
})
parser.addArgument('--styleguide-url', {
required: true,
dest: 'styleguideUrl',
type: urlArgument
})
parser.addArgument('--kss-dir', {
required: true,
dest: 'kssDir',
type: pathArgument
})
parser.addArgument('--viewport', {
defaultValue: builtinViewports.desktop
})
parser.addArgument('--no-empty-screenshot-dir', {
action: 'storeFalse',
defaultValue: true,
dest: 'emptyScreenshotDir'
})
parser.addArgument('--component')
parser.addArgument('--cache-file', {
defaultValue: '/tmp/cozy-ui-e2e-screenshots-cache.json',
dest: 'cacheFile'
})
parser.addArgument('--no-cache', {
dest: 'cacheFile',
action: 'storeFalse'
})
const config = await readConfig()
const args = parser.parseArgs()
const parsedViewport = parseViewportArgument(args.viewport)
await prepareFS({
screenshotDir: args.screenshotDir,
emptyScreenshotDir: args.emptyScreenshotDir
})
const { browser, page } = await prepareBrowser({ viewport: parsedViewport })
if (args.mode == 'react') {
await screenshotReactStyleguide(page, args, config)
} else if (args.mode == 'stack') {
await screenshotStackExamples(page, args)
} else if (args.mode == 'kss') {
await screenshotKSSStyleguide(page, args)
}
await browser.close()
}
if (require.main === module) {
main().catch(e => {
console.error(e)
process.exit(1)
})
}