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
164 lines (135 loc) Β· 5.33 KB
/
Copy pathgemini.js
File metadata and controls
164 lines (135 loc) Β· 5.33 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
'use strict';
var QEmitter = require('qemitter'),
debug = require('debug'),
util = require('util'),
_ = require('lodash'),
chalk = require('chalk'),
SuiteRunner = require('./runner'),
Config = require('./config'),
readTests = require('./test-reader'),
suiteUtils = require('./suite-util'),
RunnnerStats = require('./stats'),
RunnerEvents = require('./constants/runner-events'),
GeminiError = require('./errors/gemini-error'),
Image = require('./image'),
DEFAULT_CFG_NAME = '.gemini.yml';
function Gemini(config, allowOverrides) {
QEmitter.call(this);
config = config || DEFAULT_CFG_NAME;
this.config = new Config(config, allowOverrides);
if (this.config.system.debug) {
debug.enable('gemini:*');
}
var _this = this;
require('./plugins').load(_this, this.config);
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 = undefined;
}
options = options || {};
options.reporters = options.reporters || [];
var envBrowsers = process.env.GEMINI_BROWSERS? process.env.GEMINI_BROWSERS.replace(/\s/g, '').split(',') : null;
options.browsers = options.browsers || envBrowsers;
if (options.browsers) {
var browsersFromConfig = _this.browserIds,
validBrowsers = _.intersection(options.browsers, browsersFromConfig),
unknownBrowsers = _.difference(options.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(', ')
));
}
runnerInstance.setTestBrowsers(validBrowsers);
}
function notifyEndRunner(data) {
return _this.emitAndWait(RunnerEvents.END_RUNNER, runnerInstance, data).thenResolve(data);
}
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(notifyEndRunner, function(e) {
return notifyEndRunner().thenReject(e);
});
}
this.readTests = function(paths) {
return readTests(paths, this.config);
};
this.gather = function(paths, options) {
return executeRunner(
new SuiteRunner.createScreenShooter(this.config),
paths,
options);
};
this.test = function(paths, options) {
return executeRunner(
new SuiteRunner.createTester(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;