This repository has been archived by the owner on Nov 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·144 lines (124 loc) · 3.74 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
142
143
144
#! /usr/bin/env node
const puppeteer = require("puppeteer");
const axios = require("axios");
const ora = require("ora");
const pages = require("../../ui-diff-pages");
const config = require("../../ui-diff-config");
// Parsed command line args
const args = require("minimist")(process.argv.slice(2));
// Find out if to launch headless or headful
const headless =
!args.headless || args.headless === "true" || args.headless === true;
// Main function
async function main() {
process.setMaxListeners(Infinity);
// Launch headless/headful browser
const browser = await puppeteer.launch({
headless
});
// Initate progress bar
const spinner = ora(
`Spinning up head${headless ? "less" : "ful"} browser`
).start();
// Keep track of amount of pages screenshotted
let progress = 0;
// Async function for taking a screenshot
async function run(url, name, variant, actions) {
const page = await browser.newPage();
// Set browser dimensions for screenshot
const sizes = {
phone: {
width: 375,
height: 812
},
tablet: {
width: 768,
height: 1024
},
computer: {
width: 1920,
height: 1080
}
};
await page.setViewport(
args.size ? sizes[args.size] || sizes.computer : sizes.computer
);
// Go to page and if fails close browser
try {
await page.goto(url, { timeout: 0, waitUntil: "networkidle2" });
} catch (e) {
browser.close();
}
// Take screenshot and close page. On last page also close browser
try {
if (actions) {
const runPossibleEvent = async (element, event, value) => {
const events = {
click: await element.click(),
type: await element.type(value || "")
};
return events[event];
};
actions.forEach(async action => {
const element = await page.$(action.element);
if (element) {
await runPossibleEvent(element, action.event, action.value);
}
});
}
await page
.screenshot({
encoding: "binary",
fullPage: true,
type: "jpeg",
quality: 65
})
.then(imageBuffer => {
progress = progress + 1;
page.close().then(async () => {
// Update spinner
spinner.text = `Taking screenshots [${progress}/${pages.length *
(args.env ? 1 : 2)}]`;
// Upload image to ui-diff
await axios.post(
"https://ui-diff-api.herokuapp.com/projects/images",
{ image: imageBuffer, env: variant, name },
{ headers: { "api-token": config.token } }
);
// On last page close browser and stop spinner
if (progress === pages.length * (!args.env ? 2 : 1)) {
browser
.close()
.then(() => {
spinner.stop();
// eslint-disable-next-line no-console
console.log("Results available on https://app.ui-diff.com/");
})
.catch(e => {
// eslint-disable-next-line no-console
console.error(e);
});
}
});
});
} catch (e) {
browser.close();
}
}
const envs = config.envs;
if (!args.env || !envs[args.env]) {
// Screenshot pages on localhost
pages.forEach(p => {
run(`${envs.local}${p.path}`, p.name, "local", p.actions);
});
// Screenshot pages on live
pages.forEach(p => {
run(`${envs.live}${p.path}`, p.name, "live", p.actions);
});
} else {
pages.forEach(p => {
run(`${envs[args.env]}${p.path}`, p.name, args.env, p.actions);
});
}
}
main();