This repository was archived by the owner on Sep 21, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathgemini.js
More file actions
198 lines (158 loc) Β· 6.15 KB
/
Copy pathgemini.js
File metadata and controls
198 lines (158 loc) Β· 6.15 KB
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
'use strict';
const q = require('q');
const qDebugMode = require('q-debug-mode');
const QEmitter = require('qemitter');
const debug = require('debug');
const _ = require('lodash');
const chalk = require('chalk');
const Runner = require('./runner');
const StateProcessor = require('./state-processor');
const Config = require('./config');
const readTests = require('./test-reader');
const SuiteCollection = require('./suite-collection');
const RunnnerStats = require('./stats');
const RunnerEvents = require('./constants/runner-events');
const GeminiError = require('./errors/gemini-error');
const temp = require('./temp');
// Hack for node@0.10 and lower
// Remove restriction for maximum open concurrent sockets
require('http').globalAgent.maxSockets = Infinity;
const parseBrowsers = (browsers) => {
return browsers && browsers.replace(/\s/g, '').split(',');
};
// Gemini needs to inherit from QEmitter rather then
// EventEmmiter because it needs to allow `startRunner`
// event handler delay the execution of tests
module.exports = class Gemini extends QEmitter {
constructor(config, allowOverrides) {
super();
this.config = new Config(config, allowOverrides);
this.SuiteCollection = SuiteCollection;
setupLog(this.config.system.debug);
require('./plugins').load(this, this.config);
}
_run(stateProcessor, paths, options) {
if (!options) {
//if there are only two arguments, they are
//(stateProcessor, options) and paths are
//the default.
options = paths;
paths = undefined;
}
options = options || {};
options.reporters = options.reporters || [];
temp.init(this.config.system.tempDir);
const runner = new Runner(this.config, stateProcessor);
const envBrowsers = parseBrowsers(process.env.GEMINI_BROWSERS);
const envSkipBrowsers = parseBrowsers(process.env.GEMINI_SKIP_BROWSERS);
const browsers = options.browsers || envBrowsers;
if (browsers) {
this.checkUnknownBrowsers(browsers);
runner.setTestBrowsers(this.getValidBrowsers(browsers));
}
const getTests = (source, options) => {
return source instanceof SuiteCollection
? q(source)
: this.readTests(source, options);
};
const notifyEndRunner = (data) => {
return this.emitAndWait(RunnerEvents.END_RUNNER, runner, data).thenResolve(data);
};
return getTests(paths, options)
.then((suiteCollection) => {
return this.emitAndWait(RunnerEvents.START_RUNNER, runner)
.thenResolve(suiteCollection);
})
.then((suiteCollection) => {
this.checkUnknownBrowsers(envSkipBrowsers);
const validSkippedBrowsers = this.getValidBrowsers(envSkipBrowsers);
suiteCollection.skipBrowsers(validSkippedBrowsers);
options.reporters.forEach((reporter) => applyReporter(runner, reporter));
const stats = new RunnnerStats(runner);
return runner.run(suiteCollection)
.thenResolve(stats.get());
})
.then(notifyEndRunner, (e) => notifyEndRunner().thenReject(e));
}
readTests(paths, options) {
if (typeof options !== 'string') {
options = options || {};
} else {
console.warn(chalk.yellow(
`Passing grep to readTests is deprecated. You should pass an object with options: {grep: '${options}'}.`
));
options = {grep: options};
}
return readTests({paths, sets: options.sets}, this.config, this)
.then((rootSuite) => {
if (options.grep) {
applyGrep_(options.grep, rootSuite);
}
return new SuiteCollection(rootSuite.children);
});
function applyGrep_(grep, suite) {
if (!suite.hasStates) {
_.clone(suite.children).forEach((child) => {
applyGrep_(grep, child, suite);
});
} else {
if (!grep.test(suite.fullName)) {
suite.parent.removeChild(suite);
}
}
if (!suite.hasStates && !suite.children.length && suite.parent) {
suite.parent.removeChild(suite);
}
}
}
update(paths, options) {
return this._run(StateProcessor.createScreenUpdater(options), paths, options);
}
test(paths, options) {
return this._run(StateProcessor.createTester(this.config), paths, options);
}
getScreenshotPath(suite, stateName, browserId) {
return this.config.forBrowser(browserId).getScreenshotPath(suite, stateName);
}
getBrowserCapabilites(browserId) {
return this.config.forBrowser(browserId).desiredCapabilities;
};
getValidBrowsers(browsers) {
return _.intersection(browsers, this.browserIds);
};
checkUnknownBrowsers(browsers) {
const browsersFromConfig = this.browserIds;
const unknownBrowsers = _.difference(browsers, browsersFromConfig);
if (unknownBrowsers.length) {
console.warn(
`${chalk.yellow('WARNING:')} Unknown browsers id: ${unknownBrowsers.join(', ')}.\n` +
`Use one of the browser ids specified in config file: ${browsersFromConfig.join(', ')}`
);
}
};
get browserIds() {
return this.config.getBrowserIds();
}
};
function applyReporter(runner, reporter) {
if (typeof reporter === 'string') {
try {
reporter = require('./reporters/' + reporter);
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND') {
throw new GeminiError('No such reporter: ' + reporter);
}
throw e;
}
}
if (typeof reporter !== 'function') {
throw new TypeError('Reporter must be a string or a function');
}
reporter(runner);
}
function setupLog(isDebug) {
if (isDebug) {
qDebugMode(q);
debug.enable('gemini:*');
}
}