|
| 1 | +#!/usr/bin/env tsx |
| 2 | +import fs from 'fs' |
| 3 | +import path from 'path' |
| 4 | + |
| 5 | +interface TestStats { |
| 6 | + expected?: number |
| 7 | + unexpected?: number |
| 8 | + flaky?: number |
| 9 | + skipped?: number |
| 10 | + finished?: number |
| 11 | +} |
| 12 | + |
| 13 | +interface ReportData { |
| 14 | + stats?: TestStats |
| 15 | +} |
| 16 | + |
| 17 | +interface TestCounts { |
| 18 | + passed: number |
| 19 | + failed: number |
| 20 | + flaky: number |
| 21 | + skipped: number |
| 22 | + total: number |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Extract test counts from Playwright HTML report |
| 27 | + * @param reportDir - Path to the playwright-report directory |
| 28 | + * @returns Test counts { passed, failed, flaky, skipped, total } |
| 29 | + */ |
| 30 | +function extractTestCounts(reportDir: string): TestCounts { |
| 31 | + const counts: TestCounts = { |
| 32 | + passed: 0, |
| 33 | + failed: 0, |
| 34 | + flaky: 0, |
| 35 | + skipped: 0, |
| 36 | + total: 0 |
| 37 | + } |
| 38 | + |
| 39 | + try { |
| 40 | + // First, try to find report.json which Playwright generates with JSON reporter |
| 41 | + const jsonReportFile = path.join(reportDir, 'report.json') |
| 42 | + if (fs.existsSync(jsonReportFile)) { |
| 43 | + const reportJson: ReportData = JSON.parse( |
| 44 | + fs.readFileSync(jsonReportFile, 'utf-8') |
| 45 | + ) |
| 46 | + if (reportJson.stats) { |
| 47 | + const stats = reportJson.stats |
| 48 | + counts.total = stats.expected || 0 |
| 49 | + counts.passed = |
| 50 | + (stats.expected || 0) - |
| 51 | + (stats.unexpected || 0) - |
| 52 | + (stats.flaky || 0) - |
| 53 | + (stats.skipped || 0) |
| 54 | + counts.failed = stats.unexpected || 0 |
| 55 | + counts.flaky = stats.flaky || 0 |
| 56 | + counts.skipped = stats.skipped || 0 |
| 57 | + return counts |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + // Try index.html - Playwright HTML report embeds data in a script tag |
| 62 | + const indexFile = path.join(reportDir, 'index.html') |
| 63 | + if (fs.existsSync(indexFile)) { |
| 64 | + const content = fs.readFileSync(indexFile, 'utf-8') |
| 65 | + |
| 66 | + // Look for the embedded report data in various formats |
| 67 | + // Format 1: window.playwrightReportBase64 |
| 68 | + let dataMatch = content.match( |
| 69 | + /window\.playwrightReportBase64\s*=\s*["']([^"']+)["']/ |
| 70 | + ) |
| 71 | + if (dataMatch) { |
| 72 | + try { |
| 73 | + const decodedData = Buffer.from(dataMatch[1], 'base64').toString( |
| 74 | + 'utf-8' |
| 75 | + ) |
| 76 | + const reportData: ReportData = JSON.parse(decodedData) |
| 77 | + |
| 78 | + if (reportData.stats) { |
| 79 | + const stats = reportData.stats |
| 80 | + counts.total = stats.expected || 0 |
| 81 | + counts.passed = |
| 82 | + (stats.expected || 0) - |
| 83 | + (stats.unexpected || 0) - |
| 84 | + (stats.flaky || 0) - |
| 85 | + (stats.skipped || 0) |
| 86 | + counts.failed = stats.unexpected || 0 |
| 87 | + counts.flaky = stats.flaky || 0 |
| 88 | + counts.skipped = stats.skipped || 0 |
| 89 | + return counts |
| 90 | + } |
| 91 | + } catch (e) { |
| 92 | + // Continue to try other formats |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + // Format 2: window.playwrightReport |
| 97 | + dataMatch = content.match(/window\.playwrightReport\s*=\s*({[\s\S]*?});/) |
| 98 | + if (dataMatch) { |
| 99 | + try { |
| 100 | + // Use Function constructor instead of eval for safety |
| 101 | + const reportData = new Function( |
| 102 | + 'return ' + dataMatch[1] |
| 103 | + )() as ReportData |
| 104 | + |
| 105 | + if (reportData.stats) { |
| 106 | + const stats = reportData.stats |
| 107 | + counts.total = stats.expected || 0 |
| 108 | + counts.passed = |
| 109 | + (stats.expected || 0) - |
| 110 | + (stats.unexpected || 0) - |
| 111 | + (stats.flaky || 0) - |
| 112 | + (stats.skipped || 0) |
| 113 | + counts.failed = stats.unexpected || 0 |
| 114 | + counts.flaky = stats.flaky || 0 |
| 115 | + counts.skipped = stats.skipped || 0 |
| 116 | + return counts |
| 117 | + } |
| 118 | + } catch (e) { |
| 119 | + // Continue to try other formats |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + // Format 3: Look for stats in the HTML content directly |
| 124 | + // Playwright sometimes renders stats in the UI |
| 125 | + const statsMatch = content.match( |
| 126 | + /(\d+)\s+passed[^0-9]*(\d+)\s+failed[^0-9]*(\d+)\s+flaky[^0-9]*(\d+)\s+skipped/i |
| 127 | + ) |
| 128 | + if (statsMatch) { |
| 129 | + counts.passed = parseInt(statsMatch[1]) || 0 |
| 130 | + counts.failed = parseInt(statsMatch[2]) || 0 |
| 131 | + counts.flaky = parseInt(statsMatch[3]) || 0 |
| 132 | + counts.skipped = parseInt(statsMatch[4]) || 0 |
| 133 | + counts.total = |
| 134 | + counts.passed + counts.failed + counts.flaky + counts.skipped |
| 135 | + return counts |
| 136 | + } |
| 137 | + |
| 138 | + // Format 4: Try to extract from summary text patterns |
| 139 | + const passedMatch = content.match(/(\d+)\s+(?:tests?|specs?)\s+passed/i) |
| 140 | + const failedMatch = content.match(/(\d+)\s+(?:tests?|specs?)\s+failed/i) |
| 141 | + const flakyMatch = content.match(/(\d+)\s+(?:tests?|specs?)\s+flaky/i) |
| 142 | + const skippedMatch = content.match(/(\d+)\s+(?:tests?|specs?)\s+skipped/i) |
| 143 | + const totalMatch = content.match( |
| 144 | + /(\d+)\s+(?:tests?|specs?)\s+(?:total|ran)/i |
| 145 | + ) |
| 146 | + |
| 147 | + if (passedMatch) counts.passed = parseInt(passedMatch[1]) || 0 |
| 148 | + if (failedMatch) counts.failed = parseInt(failedMatch[1]) || 0 |
| 149 | + if (flakyMatch) counts.flaky = parseInt(flakyMatch[1]) || 0 |
| 150 | + if (skippedMatch) counts.skipped = parseInt(skippedMatch[1]) || 0 |
| 151 | + if (totalMatch) { |
| 152 | + counts.total = parseInt(totalMatch[1]) || 0 |
| 153 | + } else if ( |
| 154 | + counts.passed || |
| 155 | + counts.failed || |
| 156 | + counts.flaky || |
| 157 | + counts.skipped |
| 158 | + ) { |
| 159 | + counts.total = |
| 160 | + counts.passed + counts.failed + counts.flaky + counts.skipped |
| 161 | + } |
| 162 | + } |
| 163 | + } catch (error) { |
| 164 | + console.error(`Error reading report from ${reportDir}:`, error) |
| 165 | + } |
| 166 | + |
| 167 | + return counts |
| 168 | +} |
| 169 | + |
| 170 | +// Main execution |
| 171 | +const reportDir = process.argv[2] |
| 172 | + |
| 173 | +if (!reportDir) { |
| 174 | + console.error('Usage: extract-playwright-counts.ts <report-directory>') |
| 175 | + process.exit(1) |
| 176 | +} |
| 177 | + |
| 178 | +const counts = extractTestCounts(reportDir) |
| 179 | + |
| 180 | +// Output as JSON for easy parsing in shell script |
| 181 | +console.log(JSON.stringify(counts)) |
| 182 | + |
| 183 | +export { extractTestCounts } |
0 commit comments