UNPKG

html-reporter

Version:

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

166 lines 6.63 kB
"use strict"; 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("../../test-collection/testplane"); const testplane_2 = 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_2.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 { grep, set: sets, browser: browsers } = cliTool; const replMode = getReplModeOption(cliTool); const testCollection = await this._tool.readTests(paths, { grep, sets, browsers, replMode }); return testplane_1.TestplaneTestCollectionAdapter.create(testCollection); } async run(testCollectionAdapter, tests = [], cliTool) { const { grep, set: sets, browser: browsers, devtools = false, local = false } = cliTool; const replMode = getReplModeOption(cliTool); const runner = (0, runner_1.createTestRunner)(testCollectionAdapter.original, tests); return runner.run((collection) => this._tool.run(collection, { grep, sets, browsers, devtools, replMode, local })); } 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