UNPKG

html-reporter

Version:

Html-reporter and GUI for viewing and managing results of a tests run. Currently supports Testplane and Hermione.

190 lines 7.95 kB
"use strict"; 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 (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __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.TestplaneToolAdapter = void 0; const lodash_1 = __importDefault(require("lodash")); const testplane_1 = require("../../config/testplane"); const api_1 = require("../../../gui/api"); const config_1 = require("../../../config"); const plugin_api_1 = require("../../../plugin-api"); const runner_1 = require("./runner"); const test_results_handler_1 = require("./test-results-handler"); const constants_1 = require("../../../constants"); const server_utils_1 = require("../../../server-utils"); const SUPPORTED_TOOLS = [constants_1.ToolName.Testplane, 'hermione']; class TestplaneToolAdapter { static create(options) { return new this(options); } constructor(opts) { if ('tool' in opts) { this._tool = opts.tool; this._reporterConfig = opts.reporterConfig; } else { // in order to not use static report with gui simultaneously process.env['html_reporter_enabled'] = false.toString(); this._tool = createTool(opts.configPath); const pluginOpts = getPluginOptions(this._tool.config); this._reporterConfig = (0, config_1.parseConfig)(pluginOpts); } this._toolName = opts.toolName; this._config = testplane_1.TestplaneConfigAdapter.create(this._tool.config); this._browserConfigs = lodash_1.default.map(this._config.browserIds, (id) => this._config.getBrowserConfig(id)); this._htmlReporter = plugin_api_1.HtmlReporter.create(this._reporterConfig, { toolName: constants_1.ToolName.Testplane }); this._retryCache = {}; // in order to be able to use it from other plugins as an API this._tool.htmlReporter = this._htmlReporter; } get toolName() { return this._toolName; } get config() { return this._config; } get reporterConfig() { return this._reporterConfig; } get htmlReporter() { return this._htmlReporter; } get guiApi() { return this._guiApi; } get browserFeatures() { const result = {}; for (const browserConfig of this._browserConfigs) { const features = []; const TimeTravelMode = (0, server_utils_1.getTimeTravelModeEnumSafe)(); if (TimeTravelMode && browserConfig.timeTravel && browserConfig.timeTravel.mode === TimeTravelMode.On) { features.push(constants_1.BrowserFeature.LiveSnapshotsStreaming); } result[browserConfig.id] = features; } return result; } initGuiApi() { this._guiApi = api_1.GuiApi.create(); // in order to be able to use it from other plugins as an API this._tool.gui = this._guiApi.gui; } async readTests(paths, cliTool) { const { TestplaneTestCollectionAdapter } = await Promise.resolve().then(() => __importStar(require('../../test-collection/testplane'))); const { grep, tag, set: sets, browser: browsers } = cliTool; const replMode = getReplModeOption(cliTool); const testCollection = await this._tool.readTests(paths, { grep, sets, tag, browsers, replMode }); return TestplaneTestCollectionAdapter.create(testCollection, this._tool.config.saveHistoryMode); } async run(testCollectionAdapter, tests = [], cliTool) { const { grep, tag, set: sets, browser: browsers, inspect, inspectBrk, devtools = false, local = false, require: requireModules = [] } = cliTool; const replMode = getReplModeOption(cliTool); const runner = (0, runner_1.createTestRunner)(testCollectionAdapter.original, tests); const inspectMode = (inspect || inspectBrk) && { inspect, inspectBrk }; return runner.run((collection) => this._tool.run(collection, { grep, sets, tag, browsers, inspectMode, devtools, replMode, local, requireModules })); } async runWithoutRetries(...args) { this._disableRetries(); return this.run(...args) .finally(() => this._restoreRetries()); } updateReference(opts) { this._tool.emit(this._tool.events.UPDATE_REFERENCE, opts); } handleTestResults(reportBuilder, eventSource) { (0, test_results_handler_1.handleTestResults)(this._tool, reportBuilder, eventSource); } halt(err, timeout) { this._tool.halt(err, timeout); } async initGuiHandler() { const { customGui } = this._reporterConfig; await Promise.all((0, lodash_1.default)(customGui) .flatMap(lodash_1.default.identity) .map((ctx) => ctx.initialize?.({ testplane: this._tool, hermione: this._tool, ctx })) .value()); } async runCustomGuiAction(payload) { const { customGui } = this._reporterConfig; const { sectionName, groupIndex, controlIndex } = payload; const ctx = customGui[sectionName][groupIndex]; const control = ctx.controls[controlIndex]; await ctx.action({ testplane: this._tool, hermione: this._tool, control, ctx }); } _disableRetries() { this._browserConfigs.forEach((broConfig) => { this._retryCache[broConfig.id] = broConfig.retry; broConfig.retry = 0; }); } _restoreRetries() { this._browserConfigs.forEach((broConfig) => { broConfig.retry = this._retryCache[broConfig.id]; }); } } exports.TestplaneToolAdapter = TestplaneToolAdapter; function getPluginOptions(config) { const defaultOpts = {}; for (const toolName of SUPPORTED_TOOLS) { const opts = lodash_1.default.get(config.plugins, `html-reporter/${toolName}`, defaultOpts); if (!lodash_1.default.isEmpty(opts)) { return opts; } } return defaultOpts; } function createTool(configPath) { let tool; for (const toolName of SUPPORTED_TOOLS) { try { // eslint-disable-next-line @typescript-eslint/no-var-requires const Tool = require(toolName).default; tool = Tool.create(configPath); break; // eslint-disable-next-line @typescript-eslint/no-explicit-any } catch (err) { if (err.code !== 'MODULE_NOT_FOUND') { throw err; } } } if (!tool) { throw new Error(`Cannot find any of these modules: ${SUPPORTED_TOOLS.join(', ')}`); } return tool; } function getReplModeOption(cliTool) { const { repl = false, replBeforeTest = false, replOnFail = false } = cliTool; return { enabled: repl || replBeforeTest || replOnFail, beforeTest: replBeforeTest, onFail: replOnFail }; } //# sourceMappingURL=index.js.map