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
187 lines (157 loc) Β· 5.88 KB
/
Copy pathgemini.js
File metadata and controls
187 lines (157 loc) Β· 5.88 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
'use strict';
var QEmitter = require('qemitter'),
debug = require('debug'),
util = require('util'),
path = require('path'),
_ = require('lodash'),
chalk = require('chalk'),
ScreenShooter = require('./screen-shooter'),
Tester = require('./tester'),
Config = require('./config'),
Suite = require('./suite'),
suiteUtils = require('./suite-util'),
pathUtils = require('./path-utils'),
publicApi = require('./public-api'),
RunnnerStats = require('./stats'),
RunnerEvents = require('./constants/runner-events'),
exposeTestsApi = require('./tests-api'),
GeminiError = require('./errors/gemini-error'),
Image = require('./image'),
DEFAULT_CFG_NAME = '.gemini.yml',
DEFAULT_SPECS_DIR = 'gemini';
function requireWithNoCache(moduleName) {
var result = require(moduleName);
delete require.cache[moduleName];
return result;
}
function Gemini(config) {
QEmitter.call(this);
config = config || DEFAULT_CFG_NAME;
this.config = new Config(config);
if (this.config.system.debug) {
debug.enable('gemini:*');
}
var _this = this;
require('./plugins').load(_this, this.config);
function getDefaultSpecsDir() {
return path.join(_this.config.system.projectRoot, DEFAULT_SPECS_DIR);
}
function executeRunner(runnerInstance, paths, options) {
if (!options) {
//if there are only two arguments, they are
//(runnerInstance, options) and paths are
//the default.
options = paths;
paths = [getDefaultSpecsDir()];
}
options = options || {};
options.reporters = options.reporters || [];
var envBrowsers = process.env.GEMINI_BROWSERS? process.env.GEMINI_BROWSERS.split(',') : null;
options.browsers = options.browsers || envBrowsers;
if (options.browsers) {
areAllBrowsersFromConfig(options.browsers, _this.browserIds);
runnerInstance.setTestBrowsers(options.browsers);
}
return _this.readTests(paths)
.then(function(rootSuite) {
return _this.emitAndWait(RunnerEvents.START_RUNNER, runnerInstance)
.thenResolve(rootSuite);
})
.then(function(rootSuite) {
var suites = suiteUtils.flattenSuites(rootSuite);
if (options.grep) {
suites = suites.filter(function(suite) {
return options.grep.test(suite.fullName);
});
}
options.reporters.forEach(_.partial(applyReporter, runnerInstance));
var stats = new RunnnerStats(runnerInstance);
return runnerInstance.run(suites)
.thenResolve(stats.get());
})
.then(function(data) {
return _this.emitAndWait(RunnerEvents.END_RUNNER, runnerInstance, data)
.thenResolve(data);
});
}
function areAllBrowsersFromConfig(browsers, browsersFromConfig) {
browsers.forEach(function(name) {
if (browsersFromConfig.indexOf(name) === -1) {
throw new GeminiError('Unknown browser id: ' + name,
'Use one of the browser ids specified in config file: ' +
browsersFromConfig.join(', '));
}
});
}
this.readTests = function(paths) {
if (_.isEmpty(paths)) {
paths = [getDefaultSpecsDir()];
}
return pathUtils.expandPaths(paths)
.then(function(expanded) {
var rootSuite = Suite.create('');
exposeTestsApi(publicApi, rootSuite);
expanded.forEach(requireWithNoCache);
return rootSuite;
});
};
this.gather = function(paths, options) {
return executeRunner(
new ScreenShooter(this.config),
paths,
options);
};
this.test = function(paths, options) {
return executeRunner(
new Tester(this.config, {tempDir: options.tempDir}),
paths,
options
);
};
this.getScreenshotPath = function(suite, stateName, browserId) {
return this.config.forBrowser(browserId).getScreenshotPath(suite, stateName);
};
this.buildDiff = util.deprecate(function(referencePath, currentPath, diffPath) {
return Image.buildDiff({
reference: referencePath,
current: currentPath,
diff: diffPath,
diffColor: this.config.diffColor,
strictComparison: this.config.strictComparison,
tolerance: this.config.tolerance
});
}, 'gemini.buildDiff' + chalk.red(' is deprecated.\n') +
'Use ' + chalk.green('testResult.saveDiffTo(path)') +
' in ' + chalk.green('endTest') + ' event handler for more accurate diff'
);
this.getBrowserCapabilites = function(browserId) {
return this.config.forBrowser(browserId).desiredCapabilities;
};
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);
}
module.exports = Gemini;