UNPKG

3.12 kBJavaScriptView Raw
1var q = require('q'),
2 path = require('path'),
3 glob = require('glob'),
4 assign = require('object-assign'),
5 debug = require('debug')('protractor-cucumber-framework'),
6 Cucumber = require('cucumber'),
7 state = require('./lib/runState');
8
9/**
10 * Execute the Runner's test cases through Cucumber.
11 *
12 * @param {Runner} runner The current Protractor Runner.
13 * @param {Array} specs Array of Directory Path Strings.
14 * @return {q.Promise} Promise resolved with the test results
15 */
16exports.run = function(runner, specs) {
17 var results = {};
18
19 return runner.runTestPreparer().then(function() {
20 var config = runner.getConfig();
21 var opts = assign({}, config.cucumberOpts, config.capabilities.cucumberOpts);
22 state.initialize(runner, results, opts.strict);
23
24 return q.promise(function(resolve, reject) {
25 var cliArguments = convertOptionsToCliArguments(opts);
26 cliArguments.push('--require', path.resolve(__dirname, 'lib', 'resultsCapturer.js'));
27 cliArguments = cliArguments.concat(specs);
28
29 debug('cucumber command: "' + cliArguments.join(' ') + '"');
30
31 Cucumber.Cli(cliArguments).run(function (isSuccessful) {
32 try {
33 var complete = q();
34 if (runner.getConfig().onComplete) {
35 complete = q(runner.getConfig().onComplete());
36 }
37 complete.then(function() {
38 resolve(results);
39 });
40 } catch (err) {
41 reject(err);
42 }
43 });
44 });
45 });
46
47 function convertOptionsToCliArguments(options) {
48 var cliArguments = ['node', 'cucumberjs'];
49
50 for (var option in options) {
51 var cliArgumentValues = convertOptionValueToCliValues(option, options[option]);
52
53 if (Array.isArray(cliArgumentValues)) {
54 cliArgumentValues.forEach(function (value) {
55 cliArguments.push('--' + option, value);
56 });
57 } else if (cliArgumentValues) {
58 cliArguments.push('--' + option);
59 }
60 }
61
62 return cliArguments;
63 }
64
65 function convertRequireOptionValuesToCliValues(values) {
66 var configDir = runner.getConfig().configDir;
67
68 return toArray(values).map(function(path) {
69 // Handle glob matching
70 return glob.sync(path, {cwd: configDir});
71 }).reduce(function(opts, globPaths) {
72 // Combine paths into flattened array
73 return opts.concat(globPaths);
74 }, []).map(function(requirePath) {
75 // Resolve require absolute path
76 return path.resolve(configDir, requirePath);
77 }).filter(function(item, pos, orig) {
78 // Make sure requires are unique
79 return orig.indexOf(item) == pos;
80 });
81 }
82
83 function convertGenericOptionValuesToCliValues(values) {
84 if (values === true || !values) {
85 return values;
86 } else {
87 return toArray(values);
88 }
89 }
90
91 function convertOptionValueToCliValues(option, values) {
92 if (option === 'require') {
93 return convertRequireOptionValuesToCliValues(values);
94 } else {
95 return convertGenericOptionValuesToCliValues(values);
96 }
97 }
98
99 function toArray(values) {
100 return Array.isArray(values) ? values : [values];
101 }
102};