Prompt
How do I capture screenshots and logs when a synthetic test fails?
Latest observation
Nov 16, 2026
OpenAI APIWeb search: off
To capture screenshots and logs on synthetic test failure, the exact approach depends on the tooling you’re using, but the general pattern is:
- Detect failure in the test step
- On failure, capture evidence
- screenshot
- browser console logs
- network logs / HAR
- page source or DOM snapshot
- Attach or save the artifacts
- Upload/store them where your CI or monitoring system can collect them
Common ways to do it
If you’re using Playwright
Use testInfo.attach() and failure hooks:
import { test } from '@playwright/test';
test.afterEach(async ({ page }, testInfo) => {
if (testInfo.status !== testInfo.expectedStatus) {
await testInfo.attach('screenshot', {
path: await page.screenshot({ path: `artifacts/${testInfo.title}.png`, fullPage: true }),
contentType: 'image/png',
});
const logs = [];
page.on('console', msg => logs.push(`${msg.type()}: ${msg.text()}`));
await testInfo.attach('console-logs', {
body: logs.join('\n'),
contentType: 'text/plain',
});
}
});
Also enable built-in screenshot/video/trace recording:
use: {
screenshot: 'only-on-failure',
trace: 'on-first-retry',
video: 'retain-on-failure',
}
If you’re using Selenium
Wrap your assertions in a try/catch, then:
- take a screenshot with
getScreenshotAs() - collect browser console logs if supported
- save page source
Example:
try {
// test steps
} catch (Exception e) {
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshot, new File("artifacts/failure.png"));
String source = driver.getPageSource();
Files.writeString(Path.of("artifacts/page-source.html"), source);
throw e;
}
For browser logs:
LogEntries logs = driver.manage().logs().get(LogType.BROWSER);
If you’re using Cypress
Use afterEach() or the screenshotOnRunFailure setting:
describe('test', () => {
afterEach(function () {
if (this.currentTest.state === 'failed') {
cy.screenshot();
}
});
});
Console logs can be captured by stubbing console or using Cypress event hooks.
For synthetic monitoring platforms
Many platforms already support this:
- Datadog Synthetics: screenshots, step logs, HAR, and browser logs can be included automatically on failure
- New Relic Synthetics
- Pingdom / Checkly / Grafana Synthetic Monitoring
Usually you need to:
- enable failure artifacts in the monitor settings
- make sure the test runner is configured to store them
- attach custom logs if your platform supports it
Best practices
- Capture artifacts only on failure to reduce noise/storage
- Save artifacts with a timestamp / test name / run ID
- Include:
- screenshot
- console logs
- network/HAR
- DOM snapshot
- URL and timing info
- Upload to a shared location like S3, CI artifacts, or your observability tool
If you tell me which framework or synthetic monitoring tool you’re using, I can give you the exact code/config for that setup.