@microfocus/alm-octane-test-result-convertion
Version:
A NodeJS library for converting different kinds of test reports into OpenText SDP / SDM format.
156 lines (155 loc) • 7.02 kB
JavaScript
;
/*
* Copyright 2024-2025 Open Text.
*
* The only warranties for products and services of Open Text and
* its affiliates and licensors (“Open Text”) are as may be set forth
* in the express warranty statements accompanying such products and services.
* Nothing herein should be construed as constituting an additional warranty.
* Open Text shall not be liable for technical or editorial errors or
* omissions contained herein. The information contained herein is subject
* to change without notice.
*
* Except as specifically indicated otherwise, this document contains
* confidential information and a valid license is required for possession,
* use or copying. If this work is provided to the U.S. Government,
* consistent with FAR 12.211 and 12.212, Commercial Computer Software,
* Computer Software Documentation, and Technical Data for Commercial Items are
* licensed to the U.S. Government under vendor's standard commercial license.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const xml_escape_1 = __importDefault(require("xml-escape"));
const xml_js_1 = require("xml-js");
const TestRun_1 = require("../model/octane/TestRun");
const FAILED_STATUS_LOWER_CASE = TestRun_1.TestRunResult.FAILED.toLowerCase();
/**
* Convert Gherkin format XML to OpenText SDP / SDM format XML
* @param {string} gherkinXML - string containing Gherkin format XML
* @param {OctaneBuildConfig} octaneBuildConfig - OpenText SDP / SDM build configuration data (eg.: job id, buiild id, server id etc.)
* @param {FrameworkType} framework - Testing framework used to run the automated tests
* @returns {string} - string containing converted XML (returns the OpenText SDP / SDM format XML)
*/
const convertGherkinXMLToOctaneXML = (gherkinXML, octaneBuildConfig, framework) => {
const gherkinReportJSON = (0, xml_js_1.xml2js)(gherkinXML, { compact: true });
const octaneReportJSON = createOctaneTestsResult(gherkinReportJSON, octaneBuildConfig, framework);
return (0, xml_js_1.js2xml)(octaneReportJSON, { compact: true });
};
/**
* Creates OpenText SDP / SDM test results object from Gherkin XML root object
* @param {MultipleFeaturesRoot} gherkinReport - Gherkin XML root object
* @param {OctaneBuildConfig} buildConfig - OpenText SDP / SDM build configuration data (eg.: job id, build id, server id etc.)
* @param {FrameworkType} framework - Testing framework used to run the automated tests
* @returns {TestsResult} - OpenText SDP / SDM tests result object to be converted to XML
*/
const createOctaneTestsResult = (gherkinReport, buildConfig, framework) => {
return {
test_result: {
build: {
_attributes: Object.assign({}, buildConfig)
},
test_fields: {
test_field: [
{
_attributes: {
type: 'Test_Level',
value: 'Gherkin Test'
}
},
{
_attributes: {
type: 'Test_Type',
value: 'Sanity'
}
},
{
_attributes: {
type: 'Framework',
value: framework
}
}
]
},
test_runs: {
gherkin_test_run: convertGherkinSuiteToOctaneRuns(gherkinReport)
}
}
};
};
/**
* Converts Gherkin root object to a list of OpenText SDP / SDM Test Run objects
* @param {MultipleFeaturesRoot} reportRoot - Gherkin XML root object
* @returns {GherkinTestRun[]} - list of Gherkin Test Run OpenText SDP / SDM objects
*/
const convertGherkinSuiteToOctaneRuns = (reportRoot) => {
const octaneTestRuns = [];
const features = convertToArray(reportRoot.features.feature);
features.forEach(featureElement => {
octaneTestRuns.push(mapTestCaseToOctaneRun(featureElement));
});
return octaneTestRuns;
};
/**
* Maps a Gherkin Feature object to an OpenText SDP / SDM TestRun object
* @param {Feature} feature - Gherkin Feature object
* @returns {GherkinTestRun} - resulted OpenText SDP / SDM TestRun object
*/
const mapTestCaseToOctaneRun = (featureElement) => {
let featureDuration = 0;
let featureStatus = TestRun_1.TestRunResult.PASSED;
featureElement._attributes.name = (0, xml_escape_1.default)(featureElement._attributes.name);
const scenarios = convertToArray(featureElement.scenarios.scenario);
scenarios.forEach(scenarioElement => {
scenarioElement._attributes.name = (0, xml_escape_1.default)(scenarioElement._attributes.name);
if (scenarioElement.steps) {
const steps = convertToArray(scenarioElement.steps.step);
if (steps && steps.length) {
let scenarioStatus = TestRun_1.TestRunResult.PASSED;
steps.forEach(stepElement => {
stepElement._attributes.name = (0, xml_escape_1.default)(stepElement._attributes.name);
featureDuration += Number(stepElement._attributes.duration);
if (stepElement._attributes.status.toLowerCase() === FAILED_STATUS_LOWER_CASE) {
scenarioStatus = TestRun_1.TestRunResult.FAILED;
}
});
scenarioElement._attributes.status = scenarioStatus;
if (scenarioStatus.toLowerCase() === FAILED_STATUS_LOWER_CASE) {
featureStatus = scenarioStatus;
}
}
}
});
const testRun = {
_attributes: {
name: featureElement._attributes.name,
duration: featureDuration,
status: featureStatus
},
feature: featureElement
};
return testRun;
};
/**
* Ensures an element is an array. If element is not already an array, then it is wrapped inside an array.
* @param {Type} elem - element to be checked
* @returns {Type[]} - the element array
*/
const convertToArray = (elem) => {
if (!Array.isArray(elem)) {
return [elem];
}
return elem;
};
exports.default = convertGherkinXMLToOctaneXML;