This repository was archived by the owner on Nov 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathpage.js
More file actions
170 lines (137 loc) · 4.76 KB
/
Copy pathpage.js
File metadata and controls
170 lines (137 loc) · 4.76 KB
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
const { promisify } = require('util')
const fs = require('fs-extra')
const path = require('path')
const chalk = require('chalk')
const { debug } = require('./debug')
const ensureDir = promisify(fs.ensureDir)
async function getPreviews (page, { url, filter, viewport, navigationOptions }) {
await goToUrl(page, url, navigationOptions)
return page.evaluate(getPreviewsInPage, { filter, viewport })
}
function getPreviewsInPage ({ filter, viewport }) {
const shouldIncludePreview = name => {
if (filter == null) {
return true
}
return [].concat(filter).some(str => {
const regexp = new RegExp(str.toLowerCase())
return regexp.test(name.toLowerCase())
})
}
const extractPreviewInfo = (memo, el) => {
const url = el.nextSibling.querySelector('a[href][title]').href
const name = el.dataset.preview
const description = el.dataset.description
const actionStates = el.dataset.actionStates
const previewSelector = el.dataset.previewSelector
if (!shouldIncludePreview(name)) {
return memo
}
memo[name] = (memo[name] || []).concat({
url,
name,
description,
actionStates,
viewport,
previewSelector
})
return memo
}
const result = document.querySelectorAll('[data-preview]')
return Array.prototype.reduce.call(result, extractPreviewInfo, {})
}
async function takeNewScreenshotsOfPreviews (page, previewMap, { dir, progress, navigationOptions, wait }) {
await ensureDir(dir)
let progressIndex = 1
const progressTotal = Object.keys(previewMap).reduce((memo, name) => memo + previewMap[name].length, 0)
for (const name of Object.keys(previewMap)) {
const previewList = previewMap[name]
let previewIndex = 1
for (const preview of previewList) {
const actionStateList = preview.actionStates ? JSON.parse(preview.actionStates) : [{ action: 'none' }]
progress.update(progressIndex, progressTotal)
const { url } = preview
await goToHashUrl(page, url)
for (const actionState of actionStateList) {
await takeNewScreenshotOfPreview(page, preview, previewIndex, actionState, { dir, wait })
previewIndex += 1
}
await resetMouseAndFocus(page)
progressIndex += 1
}
}
}
async function takeNewScreenshotOfPreview (page, preview, index, actionState, { dir, wait }) {
const el = await page.$('[data-preview]')
if (wait) {
await sleep(wait)
}
await triggerAction(page, el, actionState)
const boundingBoxEl = preview.previewSelector ? await page.$(preview.previewSelector) : el
const isVisible = await page.evaluate((element) => {
if (!element) {
return false
}
const style = window.getComputedStyle(element)
return style && style.display !== 'none' && style.visibility !== 'hidden' && style.opacity !== '0'
}, boundingBoxEl)
const path = await getRelativeFilepath(preview, index, actionState, dir)
debug('Storing screenshot of %s in %s', chalk.blue(preview.name), chalk.cyan(path))
if (isVisible) {
await boundingBoxEl.screenshot({ path })
}
}
async function getRelativeFilepath (preview, index, actionState, dir) {
const { name, description = `${index}`, viewport } = preview
const { action = '', key = '' } = actionState
const actionName = action === 'none' ? '' : action
const baseName = name + ` ${description} ${actionName} ${key} ${viewport}`.toLowerCase()
const underscoredName = baseName.replace(/[^0-9A-Z]+/gi, '_')
return path.join(dir, `${underscoredName}.new.png`)
}
async function triggerAction (page, el, actionState) {
const actionEl = actionState.selector ? await el.$(actionState.selector) || await page.$(actionState.selector) : el
switch (actionState.action) {
case 'hover':
await actionEl.hover()
break
case 'click':
await actionEl.click()
break
case 'mouseDown':
const box = await actionEl.boundingBox()
await page.mouse.move(box.x + box.width / 2, box.y + box.height / 2)
await page.mouse.down()
break
case 'focus':
await actionEl.focus()
break
case 'keyPress':
const key = actionState.key || 'a'
await page.keyboard.press(key)
break
}
await sleep(actionState.wait)
}
async function resetMouseAndFocus (page) {
await page.focus('body')
await page.mouse.up()
await page.mouse.move(0, 0)
}
async function goToUrl (page, url, navigationOptions) {
debug('Navigating to URL %s', chalk.blue(url))
return page.goto(url, navigationOptions)
}
async function goToHashUrl (page, url) {
debug('Navigating to hash URL %s', chalk.blue(url))
return page.evaluate(url => {
window.location.href = url
}, url)
}
function sleep (ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
module.exports = {
getPreviews,
takeNewScreenshotsOfPreviews
}