-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
141 lines (126 loc) · 4.46 KB
/
index.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
#!/usr/bin/env node
var fs = require('fs')
var path = require('path')
var yaml = require('js-yaml')
var headless = require('puppeteer')
var devices = require('puppeteer/DeviceDescriptors')
var instructionsPath = path.join(process.cwd(), process.argv[2])
var outputDir = process.cwd()
var instructions = yaml.safeLoad(fs.readFileSync(instructionsPath, 'utf8'));
async function main () {
console.log('TAP version 13')
var browser = await headless.launch()
var tab = await browser.newPage()
var groups = instructions.pages.reduce(function (groups, page) {
if (page.skip === true) groups.skip.push(page)
else if (page.only === true) groups.only.push(page)
else groups.notag.push(page)
return groups
}, {only: [], notag: [], skip: []})
var pages = groups.only.length ? groups.only : groups.notag
var t = 1
console.log(`1..${pages.length + pages.reduce((s, p) => s + (p.instructions ? p.instructions.length : 2), 0)}`)
for(var page of pages) {
console.log(`# ${page.title}`)
try {
await tab.goto(page.url)
await tab.setViewport({
'width': 1280,
'height': 1024,
'deviceScaleFactor': 2,
'isMobile': false,
'hasTouch': false,
'isLandscape': false
})
console.log(`ok ${t++} goto (${page.url})`)
} catch (ex) {
console.log(`not ok ${t++} goto (${page.url})`)
reportError(ex)
}
if (!page.instructions) {
try {
await tab.screenshot({path: path.join(outputDir, `${page.title}.png`), fullPage: true})
console.log(`ok ${t++} screenshot (${page.title}.png)`)
} catch (ex) {
console.log(`not ok ${t++} screenshot (${page.title}.png)`)
reportError(ex)
}
}
else {
var np = 0
var ne = 0
var name = null
for (var [method, args] of page.instructions) {
switch (method) {
case 'screenshot':
var name = args && args[0]
var filename = `${page.title}-${name || np++}.png`
try {
await tab.screenshot({path: path.join(outputDir, filename), fullPage: true})
console.log(`ok ${t++} screenshot (${filename})`)
} catch (ex) {
console.log(`not ok ${t++} screenshot (${filename})`)
reportError(ex)
}
break
case 'screenshotElement':
try {
var clip = await tab.evaluate((selector, expand) => {
if (expand == null) expand = {top: 0, right: 0, bottom: 0, left: 0}
var rect = document.querySelector(selector).getBoundingClientRect()
return {
x: rect.x - expand.left,
y: rect.y - expand.top,
width: rect.width + expand.left + expand.right,
height: rect.height + expand.top + expand.bottom
}
}, ...args.slice(1))
var name = args && args[0]
var filename = `${page.title}-element-${name || ne++}.png`
await tab.screenshot({path: path.join(outputDir, filename), clip})
console.log(`ok ${t++} screenshotElement '${args[1]}' (${filename})`)
} catch (ex) {
console.log(`not ok ${t++} screenshotElement '${args[1]}' (${filename})`)
reportError(ex)
}
break
case 'clearCookies':
try {
var cookies = await tab.cookies()
if (cookies.length) await tab.deleteCookie(...cookies.map(c => ({name: c.name})))
console.log(`ok ${t++} clearCookies (${cookies.length})`)
}
catch (ex) {
console.log(`not ok ${t++} clearCookies (0)`)
reportError(ex)
}
break
default:
try {
await tab[method](...args)
console.log(`ok ${t++} ${method} (args = ${JSON.stringify(args)})`)
} catch (ex) {
console.log(`not ok ${t++} ${method} (args = ${JSON.stringify(args)})`)
reportError(ex)
}
break
}
}
}
}
await browser.close()
}
(async _ => main())()
function reportError(ex) {
var output = ''
var outer = ' '
var inner = outer + ' '
output += outer + '---\n'
output += inner + 'stack: |-\n'
var lines = String(ex.stack).split('\n')
for (var i = 0; i < lines.length; i++) {
output += inner + ' ' + lines[i] + '\n'
}
output += outer + '...\n';
console.log(output)
}