Skip to content

Instantly share code, notes, and snippets.

@darrinholst
Last active August 2, 2019 11:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darrinholst/bd00aa0f40d5978a84c47b012215b080 to your computer and use it in GitHub Desktop.
Save darrinholst/bd00aa0f40d5978a84c47b012215b080 to your computer and use it in GitHub Desktop.
const SHARD_COUNT = exports.SHARD_COUNT = +(process.env.PROTRACTOR_SHARD_COUNT || 4);
const _ = require('lodash');
const glob = require('glob');
const fs = require('fs');
exports.config = _.tap(_.clone(require('./protractor.conf.js').config), function (config) {
config.getMultiCapabilities = function () {
const shards = getShardedScenarios(getAllScenarios(config.capabilities.specs));
return _.map(shards, function (files, i) {
const shardNumber = ++i;
return {
browserName: process.env.PROTRACTOR_BROWSER || 'chrome',
specs: files,
cucumberOpts: _.tap(_.clone(config.cucumberOpts), function (options) {
options.format = _.map(options.format, function (format) {
return format.replace(/\.json/, `-${shardNumber}.json`);
});
})
};
});
};
function getAllScenarios (specs) {
return _.flatten(_.map(glob.sync(specs), function (filename) {
return _.reduce(fs.readFileSync(filename, {encoding: 'utf8'}).split('\n'), function (memo, line, i) {
if (line.match(/^ *Scenario:/)) memo.push(`${filename}:${i + 1}`);
return memo;
}, []);
}));
}
function getShardedScenarios (allScenarios) {
const scenariosPerShard = Math.ceil(allScenarios.length / SHARD_COUNT);
return _.reduce(allScenarios, function (memo, scenario) {
let lastShard = _.last(memo);
if (lastShard.length > scenariosPerShard) {
lastShard = [];
memo.push(lastShard);
}
lastShard.push(scenario);
return memo;
}, [[]]);
}
});
exports.config = {
framework: 'custom',
frameworkPath: 'node_modules/protractor-cucumber-framework',
cucumberOpts: {
format: ['json:./build/test-results/protractor.json', 'pretty'],
require: ['features/step_definitions/**/*.js', 'features/support/**/*.js'],
strict: true
},
capabilities: {
browserName: process.env.PROTRACTOR_BROWSER || 'chrome',
specs: 'features/**/*.feature'
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment