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
191 lines (155 loc) Β· 5.74 KB
/
Copy pathgemini.js
File metadata and controls
191 lines (155 loc) Β· 5.74 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
'use strict';
var Q = require('q'),
qDebugMode = require('q-debug-mode'),
QEmitter = require('qemitter'),
debug = require('debug'),
util = require('util'),
_ = require('lodash'),
chalk = require('chalk'),
Runner = require('./runner'),
StateProcessor = require('./state-processor'),
Config = require('./config'),
readTests = require('./test-reader'),
SuiteCollection = require('./suite-collection'),
RunnnerStats = require('./stats'),
RunnerEvents = require('./constants/runner-events'),
GeminiError = require('./errors/gemini-error'),
temp = require('./temp');
// Hack for node@0.10 and lower
// Remove restriction for maximum open concurrent sockets
require('http').globalAgent.maxSockets = Infinity;
function Gemini(config, allowOverrides) {
QEmitter.call(this);
this.config = new Config(config, allowOverrides);
setupLog(this.config.system.debug);
var _this = this;
require('./plugins').load(_this, this.config);
function 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);
var runner = new Runner(_this.config, stateProcessor),
envBrowsers = process.env.GEMINI_BROWSERS
&& process.env.GEMINI_BROWSERS.replace(/\s/g, '').split(','),
browsers = options.browsers || envBrowsers;
if (browsers) {
var browsersFromConfig = _this.browserIds,
validBrowsers = _.intersection(browsers, browsersFromConfig),
unknownBrowsers = _.difference(browsers, browsersFromConfig);
if (unknownBrowsers.length) {
console.warn(util.format(
'%s Unknown browsers id: %s. Use one of the browser ids specified in config file: %s',
chalk.yellow('WARNING:'), unknownBrowsers.join(', '), browsersFromConfig.join(', ')
));
}
runner.setTestBrowsers(validBrowsers);
}
return getTests_(paths, options.grep)
.then(function(suiteCollection) {
return _this.emitAndWait(RunnerEvents.START_RUNNER, runner)
.thenResolve(suiteCollection);
})
.then(function(suiteCollection) {
var suites = suiteCollection.allSuites();
options.reporters.forEach(_.partial(applyReporter, runner));
var stats = new RunnnerStats(runner);
return runner.run(suites)
.thenResolve(stats.get());
})
.then(notifyEndRunner, function(e) {
return notifyEndRunner().thenReject(e);
});
function getTests_(source, grep) {
return source instanceof SuiteCollection
? Q.resolve(source)
: _this.readTests(source, grep);
}
function notifyEndRunner(data) {
return _this.emitAndWait(RunnerEvents.END_RUNNER, runner, data).thenResolve(data);
}
}
this.readTests = function(paths, grep) {
function applyGrep_(grep, suite) {
if (!suite.hasStates) {
_.clone(suite.children).forEach(function(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);
}
}
return readTests(paths, this.config)
.then(function(rootSuite) {
if (grep) {
applyGrep_(grep, rootSuite);
}
return new SuiteCollection(rootSuite.children);
});
};
this.update = function(paths, options) {
return run(
StateProcessor.createScreenUpdater(options),
paths,
options
);
};
this.test = function(paths, options) {
return run(
StateProcessor.createTester(this.config),
paths,
options
);
};
this.getScreenshotPath = function(suite, stateName, browserId) {
return this.config.forBrowser(browserId).getScreenshotPath(suite, stateName);
};
this.getBrowserCapabilites = function(browserId) {
return this.config.forBrowser(browserId).desiredCapabilities;
};
this.SuiteCollection = SuiteCollection;
Object.defineProperty(this, 'browserIds', {
enumerable: true,
get: function() {
return this.config.getBrowserIds();
}
});
}
// Gemini needs to inherit from QEmitter rather then
// EventEmmiter because it needs to allow `startRunner`
// event handler delay the execution of tests
util.inherits(Gemini, QEmitter);
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:*');
}
}
module.exports = Gemini;