UNPKG

wdio-allure-reporter

Version:

A WebdriverIO plugin. Report results in Allure format.

579 lines (492 loc) 20.3 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); 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); var _process = require('process'); var _process2 = _interopRequireDefault(_process); var _events = require('events'); var _events2 = _interopRequireDefault(_events); var _allureJsCommons = require('allure-js-commons'); var _allureJsCommons2 = _interopRequireDefault(_allureJsCommons); var _step2 = require('allure-js-commons/beans/step'); var _step3 = _interopRequireDefault(_step2); var _utils = require('./utils'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var LOGGING_HOOKS = ['"before all" hook', '"after all" hook']; var STEP_STATUSES = [_utils.FAILED, _utils.PASSED, _utils.BROKEN]; /** * Initialize a new `Allure` test reporter. * * @param {Runner} runner * @api public */ var AllureReporter = function (_events$EventEmitter) { (0, _inherits3.default)(AllureReporter, _events$EventEmitter); function AllureReporter(baseReporter, config) { var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; (0, _classCallCheck3.default)(this, AllureReporter); var _this = (0, _possibleConstructorReturn3.default)(this, (AllureReporter.__proto__ || (0, _getPrototypeOf2.default)(AllureReporter)).call(this)); _this.baseReporter = baseReporter; _this.config = config; _this.isMultiremote = false; _this.options = options; _this.allures = {}; var epilogue = _this.baseReporter.epilogue; if (_this.options.useCucumberStepReporter) { // Hook events _this.on('hook:start', _this.cucumberHookStart.bind(_this)); _this.on('hook:end', _this.cucumberHookEnd.bind(_this)); // Test framework events _this.on('suite:start', _this.cucumberSuiteStart.bind(_this)); _this.on('suite:end', _this.cucumberSuiteEnd.bind(_this)); _this.on('test:start', _this.cucumberTestStart.bind(_this)); _this.on('test:pass', _this.cucumberTestPass.bind(_this)); _this.on('test:fail', _this.cucumberTestFail.bind(_this)); _this.on('test:pending', _this.cucumberTestPending.bind(_this)); } else { // Hook events _this.on('hook:start', _this.hookStart.bind(_this)); _this.on('hook:end', _this.hookEnd.bind(_this)); // Test framework events _this.on('suite:start', _this.suiteStart.bind(_this)); _this.on('suite:end', _this.suiteEnd.bind(_this)); _this.on('test:start', _this.testStart.bind(_this)); _this.on('test:pass', _this.testPass.bind(_this)); _this.on('test:fail', _this.testFail.bind(_this)); _this.on('test:pending', _this.testPending.bind(_this)); } // Runner events (webdriver) _this.on('start', _this.start.bind(_this)); _this.on('runner:command', _this.runnerCommand.bind(_this)); _this.on('runner:step', _this.step.bind(_this)); _this.on('runner:result', _this.runnerResult.bind(_this)); _this.on('end', function () { epilogue.call(baseReporter); }); // Allure events _this.on('allure:feature', _this.allureFeature.bind(_this)); _this.on('allure:addEnvironment', _this.allureAddEnvironment.bind(_this)); _this.on('allure:addDescription', _this.allureAddDescription.bind(_this)); _this.on('allure:attachment', _this.allureAttachment.bind(_this)); _this.on('allure:story', _this.allureStory.bind(_this)); _this.on('allure:severity', _this.allureSeverity.bind(_this)); _this.on('allure:issue', _this.allureIssue.bind(_this)); _this.on('allure:testId', _this.allureTestId.bind(_this)); return _this; } (0, _createClass3.default)(AllureReporter, [{ key: 'suiteStart', value: function suiteStart(suite) { var allure = this.getAllure(suite.cid); var currentSuite = allure.getCurrentSuite(); var prefix = currentSuite ? currentSuite.name + ' ' : ''; allure.startSuite(prefix + suite.title); } }, { key: 'suiteEnd', value: function suiteEnd(suite) { this.getAllure(suite.cid).endSuite(); } }, { key: 'testStart', value: function testStart(test) { var allure = this.getAllure(test.cid); allure.startCase(test.title); var currentTest = allure.getCurrentTest(); var browserName = test.runner[test.cid].browserName || test.cid; var version = test.runner[test.cid].version || ''; currentTest.addParameter('argument', 'browser', browserName + '-' + version); currentTest.addParameter('environment-variable', 'capabilities', (0, _stringify2.default)(test.runner[test.cid])); currentTest.addParameter('environment-variable', 'spec files', (0, _stringify2.default)(test.specs)); if (test.featureName && test.scenarioName) { currentTest.addLabel('feature', test.featureName); currentTest.addLabel('story', test.scenarioName); } // Analytics labels More: https://github.com/allure-framework/allure2/blob/master/Analytics.md currentTest.addLabel('language', 'javascript'); currentTest.addLabel('framework', 'wdio'); currentTest.addLabel('thread', test.cid); } }, { key: 'testPass', value: function testPass(test) { this.getAllure(test.cid).endCase(_utils.PASSED); } }, { key: 'testFail', value: function testFail(test) { var allure = this.getAllure(test.cid); if (!allure.getCurrentTest()) { allure.startCase(test.title); } else { allure.getCurrentTest().name = test.title; } var status = (0, _utils.getTestStatus)(test, this.config); while (allure.getCurrentSuite().currentStep instanceof _step3.default) { allure.endStep(status); } allure.endCase(status, test.err); } }, { key: 'testPending', value: function testPending(test) { var allure = this.getAllure(test.cid); if (allure.getCurrentTest() && allure.getCurrentTest().status !== 'pending') { allure.endCase('pending'); } else { allure.pendingCase(test.title); } } }, { key: 'runnerCommand', value: function runnerCommand(command) { if (this.options.disableWebdriverStepsReporting || this.isMultiremote) { return; } var allure = this.getAllure(command.cid); if (!this.isAnyTestRunning(allure)) { return; } allure.startStep(command.method + ' ' + command.uri.path); if (!(0, _utils.isEmpty)(command.data)) { this.dumpJSON(allure, 'Request', command.data); } } }, { key: 'step', value: function step(_ref) { var event = _ref.event, cid = _ref.cid, _step = _ref.step; var allure = this.getAllure(cid); if (!this.isAnyTestRunning(allure)) { return; } allure.startStep(_step.title); if (!(0, _utils.isEmpty)(_step.body)) { allure.addAttachment(_step.bodyLabel, _step.body); } if (STEP_STATUSES.indexOf(_step.status) === -1) { throw new Error('Step status must be ' + STEP_STATUSES.join(' or ') + '. You tried to set "' + _step.status + '"'); } allure.endStep(_step.status); } }, { key: 'start', value: function start(event) { this.isMultiremote = event.isMultiremote; } }, { key: 'runnerResult', value: function runnerResult(command) { if (this.isMultiremote) { return; } var allure = this.getAllure(command.cid); if (!this.isAnyTestRunning(allure)) { return; } if (command.requestOptions.uri.path.match(/\/session\/[^/]*\/screenshot/) && command.body.value) { if (!this.options.disableWebdriverScreenshotsReporting) { allure.addAttachment('Screenshot', Buffer.from(command.body.value, 'base64')); } } else if (!this.options.disableWebdriverStepsReporting) { if (command.body) { this.dumpJSON(allure, 'Response', command.body); } allure.endStep(_utils.PASSED); } } }, { key: 'hookStart', value: function hookStart(hook) { var allure = this.getAllure(hook.cid); if (!allure.getCurrentSuite() || LOGGING_HOOKS.indexOf(hook.title) === -1) { return; } allure.startCase(hook.title); } }, { key: 'hookEnd', value: function hookEnd(hook) { var allure = this.getAllure(hook.cid); if (!allure.getCurrentSuite() || LOGGING_HOOKS.indexOf(hook.title) === -1) { return; } allure.endCase(_utils.PASSED); if (allure.getCurrentTest().steps.length === 0) { allure.getCurrentSuite().testcases.pop(); } } }, { key: 'cucumberHookStart', value: function cucumberHookStart(hook) { var allure = this.getAllure(hook.cid); if (hook.title) { allure.startStep(hook.title); } } }, { key: 'cucumberHookEnd', value: function cucumberHookEnd(hook) { var allure = this.getAllure(hook.cid); if (hook.title) { allure.endStep(hook.title); } allure.endCase(_utils.PASSED); } }, { key: 'cucumberSuiteStart', value: function cucumberSuiteStart(suite) { var allure = this.getAllure(suite.cid); if (!suite.parent) { var currentSuite = allure.getCurrentSuite(); var prefix = currentSuite ? currentSuite.name + ' ' : ''; allure.startSuite(prefix + suite.title); } else { if (suite.title) { allure.startCase(suite.title); var _currentSuite = allure.getCurrentSuite(); var test = allure.getCurrentTest(); var browserName = suite.runner[suite.cid].browserName || suite.cid; var version = suite.runner[suite.cid].version || ''; test.addParameter('argument', 'browser', browserName + '-' + version); test.addLabel('feature', _currentSuite.name); // Analytics labels More: https://github.com/allure-framework/allure2/blob/master/Analytics.md test.addLabel('language', 'javascript'); test.addLabel('framework', 'wdio'); test.addLabel('thread', suite.cid); } } } }, { key: 'cucumberSuiteEnd', value: function cucumberSuiteEnd(suite) { if (!suite.parent) { this.getAllure(suite.cid).endSuite(); } else { if (suite.title) { this.getAllure(suite.cid).endCase(_utils.PASSED); } } } }, { key: 'cucumberTestStart', value: function cucumberTestStart(test) { var allure = this.getAllure(test.cid); if (!this.isAnyTestRunning(allure)) { return; } if (test.title) { allure.startStep(test.title); } } }, { key: 'cucumberTestPass', value: function cucumberTestPass(test) { var allure = this.getAllure(test.cid); while (allure.getCurrentSuite().currentStep instanceof _step3.default) { allure.endStep(_utils.PASSED); } } }, { key: 'cucumberTestFail', value: function cucumberTestFail(test) { if (test.title) { var allure = this.getAllure(test.cid); if (!this.isAnyTestRunning(allure)) { return; } var status = (0, _utils.getTestStatus)(test, this.config); while (allure.getCurrentSuite().currentStep instanceof _step3.default) { allure.endStep(status); } allure.endCase(status, test.err); } } }, { key: 'cucumberTestPending', value: function cucumberTestPending(test) { var allure = this.getAllure(test.cid); if (test.title) { allure.endStep('pending'); } } }, { key: 'allureFeature', value: function allureFeature(_ref2) { var cid = _ref2.cid, featureName = _ref2.featureName; var allure = this.getAllure(cid); var test = allure.getCurrentTest(); test.addLabel('feature', featureName); } }, { key: 'allureAddEnvironment', value: function allureAddEnvironment(_ref3) { var cid = _ref3.cid, name = _ref3.name, value = _ref3.value; var allure = this.getAllure(cid); var test = allure.getCurrentTest(); test.addParameter('environment-variable', name, value); } }, { key: 'allureAddDescription', value: function allureAddDescription(_ref4) { var cid = _ref4.cid, description = _ref4.description, type = _ref4.type; var allure = this.getAllure(cid); var test = allure.getCurrentTest(); test.setDescription(description, type); } }, { key: 'allureAttachment', value: function allureAttachment(_ref5) { var cid = _ref5.cid, name = _ref5.name, content = _ref5.content, type = _ref5.type; var allure = this.getAllure(cid); if (!content || !allure) { return; } if (type === 'application/json') { this.dumpJSON(allure, name, content); } else { allure.addAttachment(name, Buffer.from(content), type); } } }, { key: 'allureStory', value: function allureStory(_ref6) { var cid = _ref6.cid, storyName = _ref6.storyName; var test = this.getAllure(cid).getCurrentTest(); test.addLabel('story', storyName); } }, { key: 'allureSeverity', value: function allureSeverity(_ref7) { var cid = _ref7.cid, severity = _ref7.severity; var test = this.getAllure(cid).getCurrentTest(); test.addLabel('severity', severity); } }, { key: 'allureIssue', value: function allureIssue(_ref8) { var cid = _ref8.cid, issue = _ref8.issue; var test = this.getAllure(cid).getCurrentTest(); test.addLabel('issue', issue); } }, { key: 'allureTestId', value: function allureTestId(_ref9) { var cid = _ref9.cid, testId = _ref9.testId; var test = this.getAllure(cid).getCurrentTest(); test.addLabel('testId', testId); } }, { key: 'getAllure', value: function getAllure(cid) { if (this.allures[cid]) { return this.allures[cid]; } var allure = new _allureJsCommons2.default(); allure.setOptions({ targetDir: this.options.outputDir || 'allure-results' }); this.allures[cid] = allure; return this.allures[cid]; } }, { key: 'isAnyTestRunning', value: function isAnyTestRunning(allure) { return allure.getCurrentSuite() && allure.getCurrentTest(); } }, { key: 'dumpJSON', value: function dumpJSON(allure, name, json) { allure.addAttachment(name, (0, _stringify2.default)(json, null, ' '), 'application/json'); } }], [{ key: 'createStep', value: function createStep(title, body) { var bodyLabel = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'attachment'; var status = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : _utils.PASSED; var step = { title: title, body: body, bodyLabel: bodyLabel, status: status }; AllureReporter.tellReporter('runner:step', { step: step }); } }, { key: 'feature', value: function feature(featureName) { AllureReporter.tellReporter('allure:feature', { featureName: featureName }); } }, { key: 'addEnvironment', value: function addEnvironment(name, value) { AllureReporter.tellReporter('allure:addEnvironment', { name: name, value: value }); } }, { key: 'addDescription', value: function addDescription(description, type) { AllureReporter.tellReporter('allure:addDescription', { description: description, type: type }); } }, { key: 'createAttachment', value: function createAttachment(name, content) { var type = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'text/plain'; AllureReporter.tellReporter('allure:attachment', { name: name, content: content, type: type }); } }, { key: 'story', value: function story(storyName) { AllureReporter.tellReporter('allure:story', { storyName: storyName }); } }, { key: 'severity', value: function severity(_severity) { AllureReporter.tellReporter('allure:severity', { severity: _severity }); } }, { key: 'issue', value: function issue(_issue) { AllureReporter.tellReporter('allure:issue', { issue: _issue }); } }, { key: 'testId', value: function testId(_testId) { AllureReporter.tellReporter('allure:testId', { testId: _testId }); } }, { key: 'tellReporter', value: function tellReporter(event) { var msg = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; _process2.default.send((0, _extends3.default)({ event: event }, msg)); } }]); return AllureReporter; }(_events2.default.EventEmitter); exports.default = AllureReporter; module.exports = exports['default'];