UNPKG

@uuv/cypress

Version:

A solution to facilitate the writing and execution of E2E tests understandable by any human being using cucumber(BDD) and cypress

150 lines (149 loc) 6.21 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.UUVCliCypressRunner = void 0; const runner_commons_1 = require("@uuv/runner-commons"); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const chalk_1 = __importDefault(require("chalk")); const junit_report_merger_1 = require("junit-report-merger"); const multiple_cucumber_html_reporter_1 = __importDefault(require("multiple-cucumber-html-reporter")); const cypress_1 = __importDefault(require("cypress")); const utils_1 = require("@uuv/runner-commons/runner/utils"); class UUVCliCypressRunner { projectDir; name = "Cypress"; defaultBrowser = "chrome"; E2E_REPORT_DIR; JSON_REPORT_DIR; CYPRESS_JUNIT_REPORT; CUCUMBER_MESSAGES_FILE; constructor(projectDir) { this.projectDir = projectDir; } getCurrentVersion() { const pJsonStr = fs_1.default.readFileSync(`${__dirname}/../../package.json`, { encoding: "utf8", flag: "r" }); return JSON.parse(pJsonStr).version; } prepare(options) { if (options.baseUrl) { // eslint-disable-next-line dot-notation process.env["CYPRESS_BASE_URL"] = options.baseUrl; } if (options.report) { this.E2E_REPORT_DIR = path_1.default.join(options.report.outputDir, "e2e"); this.JSON_REPORT_DIR = path_1.default.join(this.E2E_REPORT_DIR, "json"); this.CYPRESS_JUNIT_REPORT = [`${this.projectDir}/reports/e2e/junit-report-*.xml`]; this.CUCUMBER_MESSAGES_FILE = path_1.default.join(this.projectDir, "cucumber-messages.ndjson"); this.createReportDirectories({ report: options.report }); } } executeE2eCommand(options) { const cypressOptions = { project: this.projectDir, browser: options.browser, env: { uuvOptions: options, ...options.extraArgs } }; if (options.report?.junit.enabled) { cypressOptions.reporter = "junit"; cypressOptions.reporterOptions = { mochaFile: "reports/e2e/junit-report-[hash].xml" }; } if (options.targetTestFile) { cypressOptions.spec = options.targetTestFile; } // Running Tests return this.getCypress() .run(cypressOptions) .then(async (result) => { if (options.report?.junit.enabled) { await this.mergeJunitReport(this.CYPRESS_JUNIT_REPORT, options.report.junit.outputFile); } if (options.report?.html.enabled) { await this.generateHtmlReport(options, options.report.html.outputDir); } if (fs_1.default.existsSync(this.CUCUMBER_MESSAGES_FILE)) { fs_1.default.rmSync(this.CUCUMBER_MESSAGES_FILE); } if ("totalFailed" in result) { console.log(`Status ${result.totalFailed ? chalk_1.default.red("failed") : chalk_1.default.green("success")}`); this.terminateProcess(result.totalFailed === 0 ? 0 : 2); } this.terminateProcess(0); }) .catch(async (err) => { console.error(chalk_1.default.red(err)); if (options.report?.junit.enabled) { await this.mergeJunitReport(this.CYPRESS_JUNIT_REPORT, options.report.junit.outputFile); } this.terminateProcess(2); }); } executeOpenCommand(options) { return this.getCypress().open({ project: this.projectDir, env: { uuvOptions: options, ...options.extraArgs }, }); } createReportDirectories(options) { if (!fs_1.default.existsSync(options.report.outputDir)) { fs_1.default.mkdirSync(options.report.outputDir); } if (fs_1.default.existsSync(this.E2E_REPORT_DIR)) { runner_commons_1.Common.deleteAllFileOfDirectory(this.E2E_REPORT_DIR); } else { fs_1.default.mkdirSync(this.E2E_REPORT_DIR, { recursive: true }); } if (!fs_1.default.existsSync(this.JSON_REPORT_DIR)) { fs_1.default.mkdirSync(this.JSON_REPORT_DIR, { recursive: true }); } } terminateProcess(exitCode) { process.exit(exitCode); } async mergeJunitReport(inputFiles, outputFile) { console.info(chalk_1.default.blueBright("Generating Junit Test Report...")); await (0, junit_report_merger_1.mergeFiles)(outputFile, inputFiles); console.info(chalk_1.default.blueBright(`Junit Test Report generated: ${outputFile}\n`)); } async generateHtmlReport(options, reportDir) { console.info(chalk_1.default.blueBright("Generating Html Test Report...")); this.generateHtmlReportFromJson(options, this.JSON_REPORT_DIR, reportDir); (0, utils_1.updateChartJsVersion)(reportDir, "2.5.0", "2.6.0"); } generateHtmlReportFromJson(options, jsonReportDir, htmlReportDir) { const UNKNOWN_VALUE = "unknown"; multiple_cucumber_html_reporter_1.default.generate({ jsonDir: jsonReportDir, reportPath: htmlReportDir, useCDN: true, metadata: { browser: { name: options.browser, version: options.extraArgs?.browserVersion ? options.extraArgs.browserVersion : "", }, device: options.extraArgs?.device ? options.extraArgs.device : UNKNOWN_VALUE, platform: { name: options.extraArgs?.platformName ? options.extraArgs.platformName : UNKNOWN_VALUE, version: options.extraArgs?.platformVersion ? options.extraArgs.platformVersion : "", }, }, }); } getCypress() { return cypress_1.default; } } exports.UUVCliCypressRunner = UUVCliCypressRunner;