wdio-cucumber-framework
Version:
A WebdriverIO plugin. Adapter for Cucumber testing framework.
564 lines (448 loc) • 18.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.__RewireAPI__ = exports.__ResetDependency__ = exports.__set__ = exports.__Rewire__ = exports.__GetDependency__ = exports.__get__ = exports.CucumberEventListener = undefined;
var _keys = require('babel-runtime/core-js/object/keys');
var _keys2 = _interopRequireDefault(_keys);
var _typeof2 = require('babel-runtime/helpers/typeof');
var _typeof3 = _interopRequireDefault(_typeof2);
var _defineProperty = require('babel-runtime/core-js/object/define-property');
var _defineProperty2 = _interopRequireDefault(_defineProperty);
var _create = require('babel-runtime/core-js/object/create');
var _create2 = _interopRequireDefault(_create);
var _extends2 = require('babel-runtime/helpers/extends');
var _extends3 = _interopRequireDefault(_extends2);
var _stringify = require('babel-runtime/core-js/json/stringify');
var _stringify2 = _interopRequireDefault(_stringify);
var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');
var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require('babel-runtime/helpers/createClass');
var _createClass3 = _interopRequireDefault(_createClass2);
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
var _inherits2 = require('babel-runtime/helpers/inherits');
var _inherits3 = _interopRequireDefault(_inherits2);
exports.compareScenarioLineWithSourceLine = compareScenarioLineWithSourceLine;
exports.getStepFromFeature = getStepFromFeature;
var _events = require('events');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var CucumberEventListener = exports.CucumberEventListener = function (_get__2) {
(0, _inherits3.default)(CucumberEventListener, _get__2);
function CucumberEventListener(eventBroadcaster) {
(0, _classCallCheck3.default)(this, CucumberEventListener);
// attachEventLogger(eventBroadcaster)
var _this = (0, _possibleConstructorReturn3.default)(this, (CucumberEventListener.__proto__ || (0, _getPrototypeOf2.default)(CucumberEventListener)).call(this));
_this.gherkinDocEvents = [];
_this.acceptedPickles = [];
_this.currentPickle = null;
_this.testCasePreparedEvents = [];
eventBroadcaster.on('gherkin-document', _this.onGherkinDocument.bind(_this)).on('pickle-accepted', _this.onPickleAccepted.bind(_this)).on('test-case-prepared', _this.onTestCasePrepared.bind(_this)).on('test-case-started', _this.onTestCaseStarted.bind(_this)).on('test-step-started', _this.onTestStepStarted.bind(_this)).on('test-step-finished', _this.onTestStepFinished.bind(_this)).on('test-case-finished', _this.onTestCaseFinished.bind(_this)).on('test-run-finished', _this.onTestRunFinished.bind(_this));
return _this;
}
// gherkinDocEvent = {
// uri: string,
// document: {
// type: 'GherkinDocument',
// feature: {
// type: 'Feature',
// tags: [{ name: string }],
// location: { line: 0, column: 0 },
// language: string,
// keyword: 'Feature',
// name: string,
// description: string,
// children: [{
// type: 'Scenario',
// tags: [],
// location: { line: 0, column: 0 },
// keyword: 'Scenario',
// name: string,
// steps: [{
// type: 'Step',
// location: { line: 0, column: 0 },
// keyword: 'Given' | 'When' | 'Then',
// text: string
// }]
// }]
// }
// },
// comments: [{
// type: 'Comment',
// location: { line: 0, column: 0 },
// text: string
// }]
// }
(0, _createClass3.default)(CucumberEventListener, [{
key: 'onGherkinDocument',
value: function onGherkinDocument(gherkinDocEvent) {
this.gherkinDocEvents.push(gherkinDocEvent);
var uri = gherkinDocEvent.uri;
var doc = gherkinDocEvent.document;
var feature = doc.feature;
this.emit('before-feature', uri, feature);
}
// pickleEvent = {
// uri: string,
// pickle: {
// tags: [{ name: string }],
// name: string,
// locations: [{ line: 0, column: 0 }],
// steps: [{
// locations: [{ line: 0, column: 0 }],
// keyword: 'Given' | 'When' | 'Then',
// text: string
// }]
// }
// }
}, {
key: 'onPickleAccepted',
value: function onPickleAccepted(pickleEvent) {
// because 'pickle-accepted' events are emitted together in forEach loop
this.acceptedPickles.push(pickleEvent);
}
}, {
key: 'onTestCaseStarted',
value: function onTestCaseStarted() {
var pickleEvent = this.acceptedPickles.shift();
var uri = pickleEvent.uri;
var doc = this.gherkinDocEvents.find(function (gde) {
return gde.uri === uri;
}).document;
var feature = doc.feature;
var scenario = pickleEvent.pickle;
this.currentPickle = scenario;
this.emit('before-scenario', uri, feature, scenario);
}
// testStepStartedEvent = {
// index: 0,
// testCase: {
// sourceLocation: { uri: string, line: 0 }
// }
// }
}, {
key: 'onTestStepStarted',
value: function onTestStepStarted(testStepStartedEvent) {
var sourceLocation = testStepStartedEvent.testCase.sourceLocation;
var uri = sourceLocation.uri;
var doc = this.gherkinDocEvents.find(function (gde) {
return gde.uri === uri;
}).document;
var feature = doc.feature;
var scenario = feature.children.find(function (child) {
return _get__('compareScenarioLineWithSourceLine')(child, sourceLocation);
});
var step = _get__('getStepFromFeature')(feature, this.currentPickle, testStepStartedEvent.index, sourceLocation);
this.emit('before-step', uri, feature, scenario, step, sourceLocation);
}
// testCasePreparedEvent = {
// sourceLocation: { uri: string, line: 0 }
// steps: [
// {
// actionLocation: {
// uri: string
// line: 0
// }
// }
// ]
// }
}, {
key: 'onTestCasePrepared',
value: function onTestCasePrepared(testCasePreparedEvent) {
this.testCasePreparedEvents.push(testCasePreparedEvent);
var sourceLocation = testCasePreparedEvent.sourceLocation;
var uri = sourceLocation.uri;
var doc = this.gherkinDocEvents.find(function (gde) {
return gde.uri === uri;
}).document;
var scenario = doc.feature.children.find(function (child) {
return _get__('compareScenarioLineWithSourceLine')(child, sourceLocation);
});
var scenarioHasHooks = scenario.steps.filter(function (step) {
return step.type === 'Hook';
}).length > 0;
if (scenarioHasHooks) {
return;
}
var allSteps = testCasePreparedEvent.steps;
allSteps.forEach(function (step, idx) {
if (!step.sourceLocation) {
step.sourceLocation = { line: step.actionLocation.line, column: 0, uri: step.actionLocation.uri };
var hook = {
type: 'Hook',
location: step.sourceLocation,
keyword: 'Hook',
text: ''
};
scenario.steps.splice(idx, 0, hook);
}
});
}
// testStepFinishedEvent = {
// index: 0,
// result: { duration: 0, status: string, exception?: Error },
// testCase: {
// sourceLocation: { uri: string, line: 0 }
// }
// }
}, {
key: 'onTestStepFinished',
value: function onTestStepFinished(testStepFinishedEvent) {
var sourceLocation = testStepFinishedEvent.testCase.sourceLocation;
var uri = sourceLocation.uri;
var doc = this.gherkinDocEvents.find(function (gde) {
return gde.uri === uri;
}).document;
var feature = doc.feature;
var scenario = feature.children.find(function (child) {
return _get__('compareScenarioLineWithSourceLine')(child, sourceLocation);
});
var step = _get__('getStepFromFeature')(feature, this.currentPickle, testStepFinishedEvent.index, sourceLocation);
var result = testStepFinishedEvent.result;
this.emit('after-step', uri, feature, scenario, step, result, sourceLocation);
}
// testCaseFinishedEvent = {
// result: { duration: 0, status: string },
// sourceLocation: { uri: string, line: 0 }
// }
}, {
key: 'onTestCaseFinished',
value: function onTestCaseFinished(testCaseFinishedEvent) {
var sourceLocation = testCaseFinishedEvent.sourceLocation;
var uri = sourceLocation.uri;
var doc = this.gherkinDocEvents.find(function (gde) {
return gde.uri === uri;
}).document;
var feature = doc.feature;
var scenario = feature.children.find(function (child) {
return _get__('compareScenarioLineWithSourceLine')(child, sourceLocation);
});
this.emit('after-scenario', uri, feature, scenario, sourceLocation);
this.currentPickle = null;
}
// testRunFinishedEvent = {
// result: { duration: 4004, success: true }
// }
}, {
key: 'onTestRunFinished',
value: function onTestRunFinished(testRunFinishedEvent) {
var gherkinDocEvent = this.gherkinDocEvents.pop(); // see .push() in `handleBeforeFeature()`
var uri = gherkinDocEvent.uri;
var doc = gherkinDocEvent.document;
var feature = doc.feature;
this.emit('after-feature', uri, feature);
}
}]);
return CucumberEventListener;
}(_get__('EventEmitter'));
// eslint-disable-next-line no-unused-vars
function attachEventLogger(eventBroadcaster) {
// for debugging purposed
// from https://github.com/cucumber/cucumber-js/blob/v4.1.0/src/formatter/event_protocol_formatter.js
var EVENTS = ['source', 'attachment', 'gherkin-document', 'pickle', 'pickle-accepted', 'pickle-rejected', 'test-run-started', 'test-case-prepared', 'test-case-started', 'test-step-started', 'test-step-attachment', 'test-step-finished', 'test-case-finished', 'test-run-finished'];
EVENTS.forEach(function (e) {
eventBroadcaster.on(e, function (x) {
console.log('\n-----' + e + ' -----\n' + (0, _stringify2.default)(x, null, 2));
});
});
}
function compareScenarioLineWithSourceLine(scenario, sourceLocation) {
if (scenario.type.indexOf('ScenarioOutline') > -1) {
return scenario.examples[0].tableBody.some(function (tableEntry) {
return tableEntry.location.line === sourceLocation.line;
});
} else {
return scenario.location.line === sourceLocation.line;
}
}
function getStepFromFeature(feature, pickle, stepIndex, sourceLocation) {
var combinedSteps = [];
feature.children.forEach(function (child) {
if (child.type.indexOf('Scenario') > -1 && !_get__('compareScenarioLineWithSourceLine')(child, sourceLocation)) {
return;
}
combinedSteps = combinedSteps.concat(child.steps);
});
var targetStep = combinedSteps[stepIndex];
if (targetStep.type === 'Step') {
var stepLine = targetStep.location.line;
var pickleStep = pickle.steps.find(function (s) {
return s.locations.some(function (loc) {
return loc.line === stepLine;
});
});
if (pickleStep) {
return (0, _extends3.default)({}, targetStep, { text: pickleStep.text });
}
}
return targetStep;
}
function _getGlobalObject() {
try {
if (!!global) {
return global;
}
} catch (e) {
try {
if (!!window) {
return window;
}
} catch (e) {
return this;
}
}
}
;
var _RewireModuleId__ = null;
function _getRewireModuleId__() {
if (_RewireModuleId__ === null) {
var globalVariable = _getGlobalObject();
if (!globalVariable.__$$GLOBAL_REWIRE_NEXT_MODULE_ID__) {
globalVariable.__$$GLOBAL_REWIRE_NEXT_MODULE_ID__ = 0;
}
_RewireModuleId__ = __$$GLOBAL_REWIRE_NEXT_MODULE_ID__++;
}
return _RewireModuleId__;
}
function _getRewireRegistry__() {
var theGlobalVariable = _getGlobalObject();
if (!theGlobalVariable.__$$GLOBAL_REWIRE_REGISTRY__) {
theGlobalVariable.__$$GLOBAL_REWIRE_REGISTRY__ = (0, _create2.default)(null);
}
return __$$GLOBAL_REWIRE_REGISTRY__;
}
function _getRewiredData__() {
var moduleId = _getRewireModuleId__();
var registry = _getRewireRegistry__();
var rewireData = registry[moduleId];
if (!rewireData) {
registry[moduleId] = (0, _create2.default)(null);
rewireData = registry[moduleId];
}
return rewireData;
}
(function registerResetAll() {
var theGlobalVariable = _getGlobalObject();
if (!theGlobalVariable['__rewire_reset_all__']) {
theGlobalVariable['__rewire_reset_all__'] = function () {
theGlobalVariable.__$$GLOBAL_REWIRE_REGISTRY__ = (0, _create2.default)(null);
};
}
})();
var INTENTIONAL_UNDEFINED = '__INTENTIONAL_UNDEFINED__';
var _RewireAPI__ = {};
(function () {
function addPropertyToAPIObject(name, value) {
(0, _defineProperty2.default)(_RewireAPI__, name, {
value: value,
enumerable: false,
configurable: true
});
}
addPropertyToAPIObject('__get__', _get__);
addPropertyToAPIObject('__GetDependency__', _get__);
addPropertyToAPIObject('__Rewire__', _set__);
addPropertyToAPIObject('__set__', _set__);
addPropertyToAPIObject('__reset__', _reset__);
addPropertyToAPIObject('__ResetDependency__', _reset__);
addPropertyToAPIObject('__with__', _with__);
})();
function _get__(variableName) {
var rewireData = _getRewiredData__();
if (rewireData[variableName] === undefined) {
return _get_original__(variableName);
} else {
var value = rewireData[variableName];
if (value === INTENTIONAL_UNDEFINED) {
return undefined;
} else {
return value;
}
}
}
function _get_original__(variableName) {
switch (variableName) {
case 'compareScenarioLineWithSourceLine':
return compareScenarioLineWithSourceLine;
case 'getStepFromFeature':
return getStepFromFeature;
case 'EventEmitter':
return _events.EventEmitter;
}
return undefined;
}
function _assign__(variableName, value) {
var rewireData = _getRewiredData__();
if (rewireData[variableName] === undefined) {
return _set_original__(variableName, value);
} else {
return rewireData[variableName] = value;
}
}
function _set_original__(variableName, _value) {
switch (variableName) {}
return undefined;
}
function _update_operation__(operation, variableName, prefix) {
var oldValue = _get__(variableName);
var newValue = operation === '++' ? oldValue + 1 : oldValue - 1;
_assign__(variableName, newValue);
return prefix ? newValue : oldValue;
}
function _set__(variableName, value) {
var rewireData = _getRewiredData__();
if ((typeof variableName === 'undefined' ? 'undefined' : (0, _typeof3.default)(variableName)) === 'object') {
(0, _keys2.default)(variableName).forEach(function (name) {
rewireData[name] = variableName[name];
});
} else {
if (value === undefined) {
rewireData[variableName] = INTENTIONAL_UNDEFINED;
} else {
rewireData[variableName] = value;
}
return function () {
_reset__(variableName);
};
}
}
function _reset__(variableName) {
var rewireData = _getRewiredData__();
delete rewireData[variableName];
if ((0, _keys2.default)(rewireData).length == 0) {
delete _getRewireRegistry__()[_getRewireModuleId__];
}
;
}
function _with__(object) {
var rewireData = _getRewiredData__();
var rewiredVariableNames = (0, _keys2.default)(object);
var previousValues = {};
function reset() {
rewiredVariableNames.forEach(function (variableName) {
rewireData[variableName] = previousValues[variableName];
});
}
return function (callback) {
rewiredVariableNames.forEach(function (variableName) {
previousValues[variableName] = rewireData[variableName];
rewireData[variableName] = object[variableName];
});
var result = callback();
if (!!result && typeof result.then == 'function') {
result.then(reset).catch(reset);
} else {
reset();
}
return result;
};
}
exports.__get__ = _get__;
exports.__GetDependency__ = _get__;
exports.__Rewire__ = _set__;
exports.__set__ = _set__;
exports.__ResetDependency__ = _reset__;
exports.__RewireAPI__ = _RewireAPI__;
exports.default = _RewireAPI__;