UNPKG

@uuv/playwright

Version:

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

196 lines (195 loc) 7.59 kB
"use strict"; /** * Software Name : UUV * * SPDX-License-Identifier: MIT * * This software is distributed under the MIT License, * see the "LICENSE" file for more details * * Authors: NJAKO MOLOM Louis Fredice & SERVICAL Stanley * Software description: Make test writing fast, understandable by any human * understanding English or French. */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.UUVCliPlaywrightRunner = void 0; exports.executePreprocessor = executePreprocessor; const fs_1 = __importDefault(require("fs")); const chalk_1 = __importDefault(require("chalk")); const uuv_playwright_reporter_helper_1 = require("../reporter/uuv-playwright-reporter-helper"); const path_1 = __importDefault(require("path")); const child_process_1 = __importStar(require("child_process")); const lodash_1 = __importDefault(require("lodash")); class UUVCliPlaywrightRunner { projectDir; tempDir; name = "Playwright"; defaultBrowser = "chrome"; watchProcess; constructor(projectDir, tempDir) { this.projectDir = projectDir; this.tempDir = tempDir; } getCurrentVersion() { const pJsonStr = fs_1.default.readFileSync(`${__dirname}/../../package.json`, { encoding: "utf8", flag: "r" }); return JSON.parse(pJsonStr).version; } async prepare(options) { try { console.log("running preprocessor..."); this.executeSystemCommand(`npx bddgen -c ${this.projectDir}/playwright.config.ts`); console.log("preprocessor executed\n"); } catch (e) { console.warn(chalk_1.default.redBright("An error occured during preprocessor, please be sure to use existing step definitions")); if (options.command === "e2e") { process.exit(1); } } this.setEnvironmentVariables(options); } setEnvironmentVariables(options) { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore process.env.REPORT_TYPE = options.report?.html.enabled ? uuv_playwright_reporter_helper_1.GeneratedReportType.HTML : uuv_playwright_reporter_helper_1.GeneratedReportType.CONSOLE; // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore process.env.CONFIG_DIR = this.projectDir; // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore process.env.browser = options.browser; if (options.extraArgs) { Object.keys(options.extraArgs).forEach(key => process.env[key] = options.extraArgs[key]); } } executeE2eCommand(options) { this.runPlaywright(options); } executeOpenCommand(options) { this.watchProcess = child_process_1.default.fork(path_1.default.join(__dirname, "watch-test-files"), [this.tempDir, this.projectDir]); process.on("exit", () => this.cleanup(this.watchProcess)); process.on("SIGINT", () => { this.cleanup(this.watchProcess); process.exit(); }); process.on("SIGTERM", () => { this.cleanup(this.watchProcess); process.exit(); }); process.on("uncaughtException", (err) => { this.cleanup(this.watchProcess); throw err; }); this.runPlaywright(options); } getTargetTestFileForPlaywright(targetTestFile) { if (!targetTestFile) { return ""; } return `${targetTestFile .replaceAll("uuv/e2e/", ".uuv-features-gen/") .replaceAll("e2e/", ".features-gen/") .replaceAll(/\.feature$/g, ".feature.spec.js")}`; } runPlaywright(options) { const configFile = `${this.projectDir}/playwright.config.ts`; try { let reporter = options.command === "e2e" ? "--reporter=@uuv/playwright/uuv-playwright-reporter" : ""; if (options.report?.junit.enabled) { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore process.env.PLAYWRIGHT_JUNIT_OUTPUT_FILE = options.report?.junit.outputFile; reporter = `${reporter},junit`; } const command = this.buildCommand(options, configFile, reporter); this.executeSystemCommand(command); } catch (err) { process.exit(2); } } buildCommand(options, configFile, reporter) { return lodash_1.default.trimEnd([ "npx", "playwright", "test", `--project="${options.browser}"`, "-c", configFile, lodash_1.default.trimStart([ options.command === "open" ? "--ui" : "", reporter ].join(" ")), lodash_1.default.trimStart([ this.getTargetTestFileForPlaywright(options.targetTestFile), options.extraArgs.TAGS ? `--grep ${options.extraArgs.TAGS}` : "" ].join(" ")) ].join(" ")); } executeSystemCommand(command) { executeSystemCommandHelper(command); } cleanup(watchProcess) { if (watchProcess) { console.log("Stopping file watcher..."); watchProcess.kill(); console.log("File watcher stopped"); } } ; } exports.UUVCliPlaywrightRunner = UUVCliPlaywrightRunner; function executeSystemCommandHelper(command) { console.log(chalk_1.default.gray(`Running command: ${command}`)); (0, child_process_1.execSync)(command, { stdio: "inherit" }); } function executePreprocessor(projectDir) { console.log("running preprocessor..."); try { executeSystemCommandHelper(`npx bddgen -c ${projectDir}/playwright.config.ts`); console.log("preprocessor executed\n"); return true; } catch (e) { console.warn(chalk_1.default.redBright("An error occured during preprocessor, please be sure to use existing step definitions")); return false; } }