testcafe-reporter-cucumber-json
Version:
Cucumber JSON TestCafe reporter plugin.
308 lines • 13.2 kB
JavaScript
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __spreadArrays = (this && this.__spreadArrays) || function () {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CucumberJsonReport = void 0;
var command_line_args_1 = require("./command-line-args");
var cucumber_json_interfaces_1 = require("./cucumber-json-interfaces");
var fs_1 = require("./fs");
var tags_parser_1 = require("./tags-parser");
var user_agent_parser_1 = require("./user-agent-parser");
var device_parser_1 = require("./device-parser");
var CucumberJsonReport = (function () {
function CucumberJsonReport() {
var _this = this;
this._startTime = new Date();
this._endTime = new Date();
this._userAgents = [];
this._testCount = 0;
this._features = [];
this._currentApp = undefined;
this._storageFolder = command_line_args_1.cliArgs.reportFolder || 'cucumber-json-reports';
this.initializeWith = function (startTime, userAgents, testCount) {
_this._startTime = startTime;
_this._userAgents = userAgents;
_this._testCount = testCount;
_this._features = [];
_this._currentFeature = undefined;
_this._currentScenario = undefined;
_this._currentApp = undefined;
if (command_line_args_1.cliArgs.appName) {
_this._currentApp = {
name: command_line_args_1.cliArgs.appName,
};
}
if (_this._currentApp && command_line_args_1.cliArgs.appName && command_line_args_1.cliArgs.appVersion) {
_this._currentApp.version = command_line_args_1.cliArgs.appVersion;
}
return _this;
};
this.finalizeWith = function (endTime, passed, warnings) {
_this._endTime = endTime;
_this._passed = passed;
_this._warnings = warnings;
_this._userAgents.forEach(function (userAgent) {
if (_this.currentFeature && _this.currentFeature[userAgent]) {
_this.currentFeature[userAgent].runInfo = {
endTime: _this._endTime,
passed: _this._passed,
startTime: _this._startTime,
testCount: _this._testCount,
userAgents: _this._userAgents,
warnings: _this._warnings,
};
}
});
return _this;
};
this.toJson = function (userAgent) {
try {
_this._features.forEach(function (feature) {
if (feature[userAgent]) {
feature[userAgent].metadata.reportTime = new Date();
}
});
var features = _this._features.map(function (f) { return f[userAgent]; });
var json = JSON.stringify(features, null, 2);
return json;
}
catch (error) {
return JSON.stringify(error, null, 2);
}
};
this.writeJsonFiles = function () {
_this._userAgents.map(function (userAgent) {
var browser = fs_1.userAgentToFilename(userAgent);
var time = fs_1.dateToFilename(_this._endTime);
var fileName = [browser, '-', time, '.json'].join('');
fs_1.writeReportSync(_this.toJson(userAgent), _this._storageFolder, fileName);
});
};
this.toInfo = function () {
var result = "\n StartTime : " + _this._startTime + "\n EndTime : " + _this._endTime + "\n UserAgents: " + _this._userAgents + "\n TestCount : " + _this._testCount + "\n Passed : " + _this._passed + "\n Warnings : " + _this._warnings + "\n ";
return result;
};
this.createFeature = function (name, path) {
var index = 0;
var newFeature = {};
_this._userAgents.forEach(function (userAgent) {
var featureReport = {
description: name || '',
elements: [],
id: _this.getFeatureIdFrom(name),
keyword: 'Feature',
line: 0,
metadata: {
app: _this._currentApp,
browser: user_agent_parser_1.getBrowserFrom(userAgent),
device: user_agent_parser_1.getDeviceFrom(userAgent),
platform: user_agent_parser_1.getPlatformFrom(userAgent),
reportTime: _this._startTime,
startTime: _this._startTime,
},
name: name
? name.replace('Feature:', '').replace('Feature :', '').trim()
: 'undefined',
runInfo: undefined,
skipped: false,
tags: tags_parser_1.tagsFromDescription(name),
uri: path + ":" + (index + 1),
};
newFeature[userAgent] = featureReport;
});
_this.currentFeature = newFeature;
return _this;
};
this.createScenario = function (name, testRunInfo) {
var newScenario = {};
_this._userAgents.forEach(function (userAgent) {
var scenarioId = _this.getScenarioIdFrom(name);
var step = _this.createDefaultStep(name, testRunInfo, userAgent);
var scenarioTags = tags_parser_1.tagsFromDescription(name).map(function (tag) { return tag.name; });
var featureTags = _this.currentFeature[userAgent].tags.map(function (tag) { return tag.name; });
var aggregatedTags = tags_parser_1.distinct(__spreadArrays(scenarioTags, featureTags));
_this.currentFeature[userAgent].tags = aggregatedTags.map(function (tag) { return ({
line: 0,
name: tag,
}); });
var scenario = {
id: "Scenario" + scenarioId,
keyword: 'Scenario',
line: 0,
name: name
? name.replace('Scenario:', '').replace('Scenario :', '').trim()
: 'undefined',
skipped: testRunInfo.skipped,
sourceLine: 'undefined',
status: step.result.status,
steps: [step],
tags: [],
type: 'scenario',
uri: '',
};
newScenario[userAgent] = scenario;
});
_this.currentScenario = newScenario;
return _this;
};
this.withBrowserInfo = function (browser, browserInfo) {
var userAgent = _this.getUserAgentFromBrowser(browser);
var metadata = _this.currentFeature &&
_this.currentFeature[userAgent] &&
_this.currentFeature[userAgent].metadata;
var device = device_parser_1.getDeviceFromBrowserInfo(browserInfo);
if (metadata && device) {
metadata.device = device;
}
return _this;
};
this.withBrowserError = function (error, browser) {
if (!error) {
return _this;
}
var userAgent = _this.getUserAgentFromBrowser(browser);
var steps = _this.currentScenario &&
_this.currentScenario[userAgent] &&
_this.currentScenario[userAgent].steps;
if (!Array.isArray(steps)) {
return _this;
}
if (steps.length === 0) {
return _this;
}
var currentStep = steps[steps.length - 1];
currentStep.result.error_message = error;
return _this;
};
this.withBrowserScreenshots = function (paths, browser) {
if (!Array.isArray(paths)) {
return _this;
}
if (paths.length === 0) {
return _this;
}
var userAgent = _this.getUserAgentFromBrowser(browser);
var steps = _this.currentScenario &&
_this.currentScenario[userAgent] &&
_this.currentScenario[userAgent].steps;
if (!Array.isArray(steps)) {
return _this;
}
if (steps.length === 0) {
return _this;
}
var currentStep = steps[steps.length - 1];
currentStep.image = paths.map(fs_1.toBase64DataImageUrl).filter(isDefined);
return _this;
};
this.getTestRunErrorsForBrowser = function (testRunInfo, browserName) {
if (!Array.isArray(testRunInfo.errs)) {
return [];
}
if (testRunInfo.errs.length === 0) {
return [];
}
var errors = testRunInfo.errs.filter(function (err) { return err.userAgent === browserName; });
return errors;
};
this.createDefaultStep = function (name, testRunInfo, browserName) {
var stepStatus = testRunInfo.skipped ? 'skipped' : 'passed';
var errs = _this.getTestRunErrorsForBrowser(testRunInfo, browserName);
if (errs && errs.length > 0) {
stepStatus = 'failed';
}
var sourceLine = name;
var sourceLineIndex = 0;
var step = __assign(__assign({}, cucumber_json_interfaces_1.testcafeDefaultStep), { name: sourceLine || 'undefined', result: {
duration: testRunInfo.durationMs,
error_message: undefined,
status: stepStatus,
}, text: ["<a href=\"#\">" + (sourceLine || '') + "</a>"] });
if (_this.currentFeature) {
step.match = {
location: _this.currentFeature[browserName].uri + ":" + (sourceLineIndex || 0),
};
}
return step;
};
this.getScenarioIdFrom = function (scenarioName) {
var result = _this.currentFeature && _this.currentFeature.name
? _this.currentFeature.name + ";" + scenarioName
: scenarioName;
return result;
};
this.getFeatureIdFrom = function (featureName) {
var result = featureName ? featureName : "Feature" + (_this._features.length + 1);
return result;
};
}
Object.defineProperty(CucumberJsonReport.prototype, "currentFeature", {
get: function () {
return this._currentFeature;
},
set: function (v) {
if (v === undefined) {
return;
}
this._currentFeature = v;
this._features.push(v);
this._currentScenario = undefined;
},
enumerable: false,
configurable: true
});
Object.defineProperty(CucumberJsonReport.prototype, "currentScenario", {
get: function () {
return this._currentScenario;
},
set: function (v) {
var _this = this;
if (v === undefined) {
return;
}
this._currentScenario = v;
this._userAgents.forEach(function (userAgent) {
if (_this._currentFeature && _this._currentFeature[userAgent]) {
_this._currentFeature[userAgent].elements.push(v[userAgent]);
}
});
},
enumerable: false,
configurable: true
});
CucumberJsonReport.prototype.getUserAgentFromBrowser = function (browser) {
var userAgent = this._userAgents.find(function (ua) {
if (ua.includes('https://')) {
return ua.startsWith(browser + " ");
}
return ua === browser;
});
if (userAgent === undefined) {
console.warn("Cannot match browser '" + browser + "' with '" + this._userAgents.join(';') + "'");
return browser;
}
return userAgent;
};
return CucumberJsonReport;
}());
exports.CucumberJsonReport = CucumberJsonReport;
function isDefined(value) {
return value !== undefined;
}
//# sourceMappingURL=cucumber-json.js.map