UNPKG

omni-dashboard-playwright-reporter

Version:

Playwright client for publishing test results to Omni Dashboard - Transform Your Test Automation with AI-Powered Insights

114 lines (113 loc) 5.23 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const chalk_1 = __importDefault(require("chalk")); const omni_reporter_config_1 = require("./types/omni-reporter-config"); const omni_playwright_client_1 = require("./omni-playwright-client"); class OmniPlaywrightReporter { constructor() { this.queue = Promise.resolve(); this.buildId = ''; this.omniConfig = (0, omni_reporter_config_1.getOmniReporterConfig)(); this.client = new omni_playwright_client_1.OmniPlaywrightClient(this.omniConfig); } async enqueue(operation) { this.queue = this.queue.then(operation); return this.queue; } async onBegin(config, suite) { await this.enqueue(async () => { // Validate configuration if (!this.omniConfig.projectId || !this.omniConfig.apiKey || !this.omniConfig.environment) { console.error(chalk_1.default.red('[OmniPlaywrightReporter][OnTestEnd] Missing required configuration: projectId, apiKey, environment, or buildId')); return; } this.buildId = await this.onBeginHandler(config, suite); }); } async onTestBegin(test) { } async onTestEnd(test, result) { await this.enqueue(async () => { // Validate configuration if (!this.omniConfig.projectId || !this.omniConfig.apiKey || !this.omniConfig.environment || !this.buildId) { console.error(chalk_1.default.red('[OmniPlaywrightReporter][OnTestEnd] Missing required configuration: projectId, apiKey, environment, or buildId')); return; } const title = test.title; try { const uploadTestCaseResponse = await this.client.uploadTestCase(test, result, this.buildId); const test_case = uploadTestCaseResponse.test_cases?.[0]; // Upload screenshots (if any) await this.client.uploadScreenshots(test_case); // Upload traces (if any) await this.client.uploadTraces(test_case); console.log("Reaching end of uploadTestCase"); console.log(chalk_1.default.green(`[OmniPlaywrightReporter] Test case uploaded successfully: ${title}`)); } catch (error) { console.error(chalk_1.default.red(`[OmniPlaywrightReporter] Test upload failed: ${title} - ${error.message}`)); throw error; } }); } async onEnd(result) { await this.enqueue(async () => { // Validate configuration if (!this.omniConfig.projectId || !this.omniConfig.apiKey || !this.omniConfig.environment || !this.buildId) { console.error(chalk_1.default.red('[OmniPlaywrightReporter][OnTestEnd] Missing required configuration: projectId, apiKey, environment, or buildId')); return; } await this.onEndHandler(result); }); } async onBeginHandler(config, suite) { try { const response = await this.client.createBuild({ duration: 0, environment: this.omniConfig.environment, status: 'in_progress', }); console.log(chalk_1.default.green('[OmniPlaywrightReporter][OnBegin] Build created with ID:'), response.build.build_id); return response.build.build_id; } catch (error) { console.error(chalk_1.default.red('[OmniPlaywrightReporter][OnBegin] Error creating build:'), error.message); throw error; } } async onEndHandler(result) { try { await this.client.completeBuild({ build_id: this.buildId, duration: Math.round(result.duration), status: result.status }); console.log(chalk_1.default.green('[OmniPlaywrightReporter][OnEnd] Build completed successfully')); } catch (error) { console.error(chalk_1.default.red('[OmniPlaywrightReporter][OnEnd] Error completing build:'), error.message); throw error; } } async onTestEndHandler(test, result) { const title = test.title; try { const uploadTestCaseResponse = await this.client.uploadTestCase(test, result, this.buildId); const test_case = uploadTestCaseResponse.test_cases?.[0]; // Upload screenshots (if any) await this.client.uploadScreenshots(test_case); // Upload traces (if any) await this.client.uploadTraces(test_case); console.log("Reaching end of uploadTestCase"); console.log(chalk_1.default.green(`[OmniPlaywrightReporter] Test case uploaded successfully: ${title}`)); } catch (error) { console.error(chalk_1.default.red(`[OmniPlaywrightReporter] Test upload failed: ${title} - ${error.message}`)); throw error; } } } exports.default = OmniPlaywrightReporter;