forked from chenglou/node-huxley
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
205 lines (167 loc) · 6 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
'use strict';
var colors = require('colors');
var fs = require('fs');
var mkdirp = require('mkdirp');
var path = require('path');
var browser = require('./source/browser');
var consts = require('./source/constants');
var playback = require('./source/playback');
var record = require('./source/record');
var getPlaybackInfos = require('./source/getPlaybackInfos');
// TODO: integration with remote environment
// whenever 'path' is mentioned and it concerns a file, the file's name itself
// isn't included
// flow:
// recordTasks:
// _recordTasks -> getPlaybackInfos -> _openRunAndClose -> _runEachPlayback ->
// _runActionOrDisplaySkipMsg -> _recordAndSave -> record ->
// _saveTaskAsJsonToFolder -> _runActionOrDisplaySkipMsg
// playbackTasksAndCompareScreenshots:
// _playbackTasksAndXScreenshots -> getPlaybackInfos -> _openRunAndClose ->
// _runEachPlayback -> _runActionOrDisplaySkipMsg -> playback ->
// _runActionOrDisplaySkipMsg
function _saveTaskAsJsonToFolder(recordPath, taskEvents, next) {
mkdirp(recordPath, function(err) {
if (err) return next(err);
fs.writeFile(path.join(recordPath, consts.RECORD_FILE_NAME),
JSON.stringify(taskEvents, null, 2), // prettify, 2-space indent
next);
});
}
function _recordAndSave(playbackInfo, next) {
record(playbackInfo.driver, function(err, allEvents) {
_saveTaskAsJsonToFolder(playbackInfo.recordPath, allEvents, next);
});
}
function _runEachPlayback(playbackInfos, action, next) {
var currentIndex = 0;
_runActionOrDisplaySkipMsg(playbackInfos[currentIndex],
action,
function _next(err) {
if (err) return next(err);
if (currentIndex === playbackInfos.length - 1) return next();
_runActionOrDisplaySkipMsg(playbackInfos[++currentIndex], action, _next);
});
}
// does nothing but open and close the browser and handle its errors
// needs to know nothing but a command to run in-between, and the callback
function _openRunAndClose(playbackInfos, openDummy, action, next) {
var browserName = playbackInfos[0].browserName;
browser.open(browserName, function(err, driver) {
if (err) {
return browser.quit(driver, function(err2) {
next(err || err2 || null);
});
}
playbackInfos.forEach(function(info) {
info.driver = driver;
});
if (openDummy) {
return browser.openDummy(browserName, function(err, dummyDriver) {
if (err) {
return browser.quit(dummyDriver, function(err2) {
next(err || err2 || null);
});
}
action(function(err) {
browser.quit(driver, function(err2) {
browser.quit(dummyDriver, function(err3) {
next(err || err2 || err3 || null);
});
});
});
});
}
action(function(err) {
browser.quit(driver, function(err2) {
next(err || err2 || null);
});
});
});
}
function _runActionOrDisplaySkipMsg(playbackInfo, action, next) {
// console.log(
// '\nAt %s'.underline, path.relative(process.cwd(), playbackInfo.recordPath)
// );
if (playbackInfo.isSkipped) {
// console.log('Marked as skipped.');
return next();
}
browser.goToUrl(playbackInfo.driver,
playbackInfo.url,
playbackInfo.screenSize[0],
playbackInfo.screenSize[1],
function(err) {
action(playbackInfo, next);
});
}
function _recordTasks(browserName, globs, next) {
_getRunnableRecords(globs, false, function(err, playbackInfos) {
if (err) return next(err);
playbackInfos.forEach(function(info) {
info.browserName = browserName;
});
_openRunAndClose(playbackInfos,
false,
_runEachPlayback.bind(null, playbackInfos, _recordAndSave),
next);
});
}
// where `x` is either compare or update screenshot
function _playbackTasksAndXScreenshots(browserName,
globs,
saveInsteadOfCompare,
next) {
_getRunnableRecords(globs, true, function(err, playbackInfos) {
if (err) return next(err);
playbackInfos.forEach(function(info) {
info.browserName = browserName;
info.overrideScreenshots = saveInsteadOfCompare;
});
_openRunAndClose(playbackInfos,
true,
_runEachPlayback.bind(null, playbackInfos, playback),
next);
});
}
function _getRunnableRecords(globs, loadRecords, next) {
if (!globs.length) globs = ['**/'];
getPlaybackInfos(globs, loadRecords, function(err, playbackInfos) {
if (err) return next(err);
var hasRunnableRecords = playbackInfos.some(function(info) {
return !info.isSkipped;
});
if (!hasRunnableRecords) {
return next('Every task is marked as skipped.');
}
next(null, playbackInfos);
});
}
function recordTasks(browserName, globs, next) {
_recordTasks(browserName, globs, function(err) {
if (err) return next(err);
console.log('\nDon\'t move! Simulating the recording now...'.yellow);
playbackTasksAndSaveScreenshots(browserName, globs, next);
});
}
function playbackTasksAndCompareScreenshots(browserName, globs, next) {
_playbackTasksAndXScreenshots(browserName, globs, false, next);
}
function playbackTasksAndSaveScreenshots(browserName, globs, next) {
_playbackTasksAndXScreenshots(browserName, globs, true, next);
}
function defaultDoneCallback(err) {
if (err) {
console.error(typeof err === 'string' ? err.red : err.message.red);
console.error('\nThe tests now halt. You might have unfinished tasks.'.red);
} else {
console.log('\nAll done successfully!'.green);
}
}
module.exports = {
recordTasks: recordTasks,
playbackTasksAndSaveScreenshots: playbackTasksAndSaveScreenshots,
playbackTasksAndCompareScreenshots: playbackTasksAndCompareScreenshots,
// third-party's (e.g. grunt-huxley) entrance point. See bin/hux
defaultDoneCallback: defaultDoneCallback
};