-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcapture.js
81 lines (67 loc) · 1.94 KB
/
capture.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
const webdriver = require('selenium-webdriver');
require('geckodriver');
const By = webdriver.By;
const firefox = require('selenium-webdriver/firefox');
const fs = require('fs');
const urllib = require('url');
exports.capture = async function capture({ channel, dimensions, url, prefix }) {
let binary;
switch (channel) {
case 'nightly':
binary = new firefox.Binary(firefox.Channel.NIGHTLY);
break;
case 'beta':
binary = new firefox.Binary(firefox.Channel.BETA);
break;
default:
binary = new firefox.Binary(firefox.Channel.RELEASE);
}
binary.addArguments('-headless');
let options = new firefox.Options();
options.setBinary(binary);
let driver = new webdriver.Builder()
.forBrowser('firefox')
.setFirefoxOptions(options)
.build();
if (!prefix) {
prefix = urllib.parse(url).hostname;
}
try {
await runTest({ driver, url, dimensions, prefix });
} catch (e) {
console.error('Error during capture:', e);
} finally {
driver.quit();
}
};
async function runTest({ driver, url, dimensions, prefix }) {
let {innerSize, outerSize} = await driver.executeScript(`
return {
outerSize: {
width: window.outerWidth,
height: window.outerHeight
},
innerSize: {
width: window.innerWidth,
height: window.innerHeight
}
}
`);
let chromeSize = {
width: outerSize.width - innerSize.width,
height: outerSize.height - innerSize.height
};
console.log('loading site...');
await driver.get(url);
await driver.sleep(1000);
for (let [width, height] of dimensions) {
let string = width + 'x' + height;
console.log(string);
await driver.manage().window().setSize(width + chromeSize.width, height + chromeSize.height);
await driver.sleep(1000);
let data = await driver.takeScreenshot();
let b = Buffer.from(data, 'base64');
fs.writeFileSync(`./${prefix}_${string}.png`, b);
}
return true;
}