(helper, {
dir,
prefix = 'snapshot',
fullPage = false,
captureURL = true,
captureScreenshot = true,
captureHTML = true,
captureARIA = true,
captureBrowserLogs = true,
captureStorage = true,
} = {})
| 157 | } |
| 158 | |
| 159 | export async function captureSnapshot(helper, { |
| 160 | dir, |
| 161 | prefix = 'snapshot', |
| 162 | fullPage = false, |
| 163 | captureURL = true, |
| 164 | captureScreenshot = true, |
| 165 | captureHTML = true, |
| 166 | captureARIA = true, |
| 167 | captureBrowserLogs = true, |
| 168 | captureStorage = true, |
| 169 | } = {}) { |
| 170 | if (!helper) return {} |
| 171 | const out = {} |
| 172 | |
| 173 | if (captureURL) { |
| 174 | try { |
| 175 | if (helper.grabCurrentUrl) out.url = await helper.grabCurrentUrl() |
| 176 | } catch {} |
| 177 | } |
| 178 | |
| 179 | if (captureScreenshot && helper.saveScreenshot) { |
| 180 | try { |
| 181 | const file = `${prefix}_screenshot.png` |
| 182 | await helper.saveScreenshot(path.join(dir, file), fullPage) |
| 183 | out.screenshot = file |
| 184 | } catch {} |
| 185 | } |
| 186 | |
| 187 | if (captureHTML && helper.grabSource) { |
| 188 | try { |
| 189 | const html = await helper.grabSource() |
| 190 | // Universal funnel: every captured HTML snapshot flows through formatHtml |
| 191 | // (minify -> cleanHtml -> beautify). Don't add direct grabSource->writeFile |
| 192 | // paths elsewhere; route through this util so trash-class cleanup stays |
| 193 | // consistent across aiTrace, pageInfo, and MCP tools. |
| 194 | const formatted = await formatHtml(html) |
| 195 | const file = `${prefix}_page.html` |
| 196 | fs.writeFileSync(path.join(dir, file), formatted) |
| 197 | out.html = file |
| 198 | // Expose pre-cleanup HTML for consumers that need to inspect classes |
| 199 | // stripped by cleanHtml (e.g. pageInfo's error-class scan). |
| 200 | out.htmlRaw = html |
| 201 | } catch {} |
| 202 | } |
| 203 | |
| 204 | if (captureARIA && helper.grabAriaSnapshot) { |
| 205 | try { |
| 206 | const aria = await helper.grabAriaSnapshot() |
| 207 | const file = `${prefix}_aria.txt` |
| 208 | fs.writeFileSync(path.join(dir, file), aria) |
| 209 | out.aria = file |
| 210 | } catch {} |
| 211 | } |
| 212 | |
| 213 | if (captureBrowserLogs && helper.grabBrowserLogs) { |
| 214 | try { |
| 215 | const logs = await helper.grabBrowserLogs() |
| 216 | const normalized = normalizeBrowserLogs(logs) |
no test coverage detected