-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
148 lines (131 loc) · 3.77 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
145
146
147
148
#!/usr/bin/env node
'use strict';
const path = require('path');
const fs = require('fs');
const os = require('os');
const { URL } = require('url');
const chalk = require('chalk');
const backstopjs = require('backstopjs');
const xdgBasedir = require('xdg-basedir');
const dataDir = path.join(xdgBasedir.data || os.tmpdir(), 'visual-regression-testing');
const defaultConfig = {
scenarios: {
delay: 1000,
},
debug: false,
asyncCaptureLimit: 5,
asyncCompareLimit: 50,
onBeforeScript: path.join(__dirname, 'engine/scripts/puppet/onBefore.js'),
onReadyScript: path.join(__dirname, 'engine/scripts/puppet/onReady.js'),
viewports: [
{
label: 'mobile',
width: 375,
height: 812,
},
{
label: 'desktop',
width: 1024,
height: 768,
},
],
};
/**
* Get scenario url
* @param {String} url
* @param {Object} options
* @returns {Object} WHATWG URL Object
*/
const resolveUrl = (url, options) => {
const { host, base, user, pass, query } = options || {};
let urlObj;
if (url.includes('://')) {
urlObj = new URL(url);
} else if (url.startsWith('/')) {
urlObj = new URL(`${host}${url}`);
} else {
urlObj = new URL(`${host}${path.join(base || '/', url)}`);
}
if (user) {
urlObj.username = user;
}
if (pass) {
urlObj.password = pass;
}
// Append custom query params
if (query) {
const searchParams = new URLSearchParams(urlObj.search);
Array.from(query.replace(/^\?/, '').split('&')).forEach((param) => {
const [key, value] = param.split('=');
if (key) {
searchParams.append(key, value);
}
});
urlObj.search = searchParams.toString();
}
return urlObj;
};
/**
* Get backstop config for environment
* See: https://github.com/garris/BackstopJS#using-backstopjs
* @param {Object} environment test environment
* @returns {Object}
*/
const getConfig = async (environment) => {
const { referenceEnvironment, urls, uid, query, backstopjs = {}, outputDir, host, user, pass, base } = environment;
const globalOverwrites = {
...defaultConfig,
...backstopjs,
};
const scenarioOverwrites = {
...(defaultConfig.scenarios || {}),
...(backstopjs.scenarios || {}),
};
const dataPath = outputDir || path.join(dataDir, uid);
const { cookiePath } = scenarioOverwrites;
return {
id: uid,
report: ['browser', 'CI'],
engine: 'puppeteer',
engineFlags: [],
paths: {
engine_scripts: path.join(__dirname, 'engine/scripts'),
bitmaps_reference: path.join(dataPath, 'bitmaps_reference'),
bitmaps_test: path.join(dataPath, 'bitmaps_test'),
html_report: path.join(dataPath, 'report_html'),
ci_report: path.join(dataPath, 'report_ci'),
},
...globalOverwrites,
scenarios: urls.map((u) => {
const url = resolveUrl(u, environment);
const result = {
selectors: ['document'],
...scenarioOverwrites,
label: url.pathname.replace(/\.[^.]+$/, ''),
url: url.href,
};
if (cookiePath && fs.existsSync(cookiePath)) {
result.cookiePath = cookiePath;
}
if (referenceEnvironment) {
const referenceUrl = resolveUrl(u, referenceEnvironment);
result.referenceUrl = referenceUrl.href;
}
return result;
}),
};
};
/**
* Default export which runs ther specified backstop command
* @param {Object} environment Test environment
* @param {String} cmd Backstop command
* @returns {Promise}
*/
module.exports = async (environment, cmd = 'test') => {
const config = await getConfig(environment);
if (cmd === 'test' && !fs.existsSync(config.paths.bitmaps_reference)) {
console.log(chalk.yellow("Can't compare without references. Creating reference images now ..."));
cmd = 'reference';
}
return await backstopjs(cmd, { config });
};