-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (53 loc) · 1.65 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
/* eslint-disable no-console */
require('dotenv').config();
const download = require('download');
const path = require('path');
const BrowserStack = require('browserstack');
const browsers = require('./browsers.json');
const DIRECTORY = path.join(__dirname, 'screenshots');
const client = BrowserStack.createScreenshotClient({
username: process.env.USERNAME,
password: process.env.PASSWORD
});
const urls = ['https://www.google.com', 'https://www.bing.com'];
console.log(`starting for ${urls.length} URLs`);
urls.forEach(url => {
client.generateScreenshots(
{
url,
browsers
},
(jobError, job) => {
if (jobError) {
console.error(jobError);
} else {
const { job_id: id, screenshots, wait_time: waitTime } = job;
const downloadDirectory = path.join(DIRECTORY, id);
console.info(
`generating ${screenshots.length} screenshot${
screenshots.length === 1 ? '' : 's'
} for ${url} with job_id ${id}`
);
const timer = setInterval(() => {
client.getJob(id, (err, { state, screenshots }) => {
if (err) {
console.error(err);
} else {
if (state === 'done') {
screenshots.map(({ image_url }) => {
download(image_url, downloadDirectory);
});
console.info(
`Downloaded screenshots for ${url} to ${path.basename(
downloadDirectory
)}.`
);
clearInterval(timer);
}
}
});
}, waitTime * 1000);
}
}
);
});