import { Reporter, TestCase, TestResult, FullConfig, Suite, TestStep, FullResult } from '@playwright/test/reporter'; import { ReporterConfig } from './types'; /** * PlaywrightTestReporter is a modern, maintainable reporter for Playwright tests. * It provides detailed, colorized output of test results with comprehensive metrics * and configurable options for better visibility into test execution. * * Features: * - Colorized output for different test states (passed, failed, skipped, retried) * - Detailed metrics including test duration and slow test identification * - Configurable thresholds for slow tests and timeouts * - Comprehensive error reporting with stack traces * - Support for test retries with clear status indication * - Complete monitoring of all error types including setup/teardown errors * - JSON output files for CI integration and historical tracking */ export default class PlaywrightTestReporter implements Reporter { private suite; private outputDir; private readonly _testRecords; private _startTime; private readonly _config; private _nonTestErrors; private _hasInterruptedTests; private _fileHandler; /** * Creates a new instance of the PlaywrightTestReporter. * * @param config - Optional configuration object to customize reporter behavior */ constructor(config?: ReporterConfig); /** * Called when the test run begins. * Initializes the start time and displays a start message. * * @param config - The full Playwright configuration * @param suite - The root test suite */ onBegin(config: FullConfig, suite: Suite): void; /** * Recursively counts the total number of tests in a suite and its children * * @param suite - The test suite to count tests from * @returns The total number of tests * @private */ private _countTests; /** * Called when an error occurs during test setup or teardown. * Logs the error with optional stack trace based on configuration. * Now tracks errors to ensure they affect final exit code. * * @param error - The error that occurred */ onError(error: Error): void; /** * Called when a test completes (whether passed, failed, or skipped). * Records the test result and logs appropriate output based on the test status. * Now tracks all test statuses including interrupted ones. * * @param test - The test case that completed * @param result - The result of the test execution */ onTestEnd(test: TestCase, result: TestResult): void; /** * Called when a test begins. * * @param test - The test case that is starting */ onTestBegin(test: TestCase, result: TestResult): Promise; /** * Called when a test step begins. * * @param test - The test case * @param result - The current test result * @param step - The test step that is starting */ onStepBegin(test: TestCase, result: TestResult, step: TestStep): Promise; /** * Called when a test step ends. * * @param test - The test case * @param result - The current test result * @param step - The test step that ended */ onStepEnd(test: TestCase, result: TestResult, step: TestStep): Promise; /** * Called when all tests have completed. * Processes results, displays summary statistics, and sets appropriate exit code. * Now properly handles all error conditions including non-test errors. */ onEnd(result: FullResult): Promise; /** * Exits the process with a success code. * Extracted to a method to make the flow clearer and more maintainable. * @private */ private _exitWithSuccess; /** * Exits the process with an error code. * Extracted to a method to make the flow clearer and more maintainable. * @private */ private _exitWithError; /** * Helper method to log messages with appropriate coloring. * * @param level - The log level ('debug' or 'error') * @param message - The message to log * @private */ private _log; /** * Formats and logs the outcome of a single test with appropriate coloring. * Handles different test states (passed, failed, skipped) and retry attempts. * Now includes handling for interrupted tests and other unexpected statuses. * * @param title - The title of the test * @param result - The result of the test execution * @param timeTakenSec - The time taken by the test in seconds * @private */ private _logTestOutcome; /** * Records the status of the last test run in a JSON file * @param hasFailed - Whether any tests failed */ private saveLastRunStatus; /** * Generates AI-powered fix suggestions for test failures * * @param failures - Array of test failures * @private */ private _generateFixSuggestions; /** * Generates a pull request with the AI-generated fix * * @param failure - The test failure information * @param fixResult - The result from GenAIUtils containing prompt and fix paths * @private */ private _generatePullRequest; /** * Creates bugs in the configured bug tracker for test failures * * @param failures - Array of test failures * @private */ private _createBugsForFailures; /** * Publishes test results to database * * @param summary - Test run summary * @param allTestCases - All test cases details * @param failures - Test failures * @private */ private _publishToDatabase; /** * Sends email notification with test results * * @param summary - Test run summary * @param failures - Test failures * @private */ private _sendEmailNotification; }