Skip to content

Commit 88d9a0a

Browse files
voidstackloopclaude
andcommitted
Attach a screenshot to every non-Pass check result
Feature 1 of 4 requested large improvements. A report reader investigating a Fail/Warn previously had to re-run formwatch against the form (possibly already fixed, or a different failure by then) just to see what the page actually looked like. Every check that comes back non-Pass — a real Fail/Warn, or a synthetic one from a timeout/error — now carries a full-page PNG screenshot, embedded as a data:image/png;base64 URI directly in --html/--json output. Design: added the `base64` crate (std has no encoder; hand-rolling one correctly is exactly the kind of standard-wheel ponytail says not to reinvent) and a `capture_screenshot` helper in checks.rs. Split run_safely's error/timeout-conversion logic into a new pure `run_with_timeout` so the existing browser-free unit tests (run_with_timeout_converts_an_error_into_a_warn_result_instead_of_propagating etc.) keep testing that logic without needing a real Page just because run_safely itself now does — run_safely is a thin wrapper that also attaches a screenshot to any non-Pass result. run_custom_checks gets the same treatment for consistency. CheckResult gained `screenshot: Option<String>`, `#[serde(default)]` so existing history files without the field still deserialize. Verified the "non-Pass only" condition is real by inverting it (`== Pass` instead of `!= Pass`) and watching the new end-to-end test catch it both ways — a real Fail with no screenshot, and (implicitly) a Pass that would carry one. Wired into templates/report.html (an <img> per non-Pass check) and a short "(screenshot captured — see --html/--json report)" note in the plain-text/colored output, since a terminal can't render an image inline. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
1 parent 8a0e94e commit 88d9a0a

11 files changed

Lines changed: 158 additions & 15 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ project doesn't have a release yet, so everything below is grouped under
2525
(Linux/macOS/Windows binaries on a version tag).
2626
- Full rustdoc coverage of the public API, enforced going forward via
2727
`#![warn(missing_docs)]`.
28+
- Every check that comes back Fail or Warn (including a custom check)
29+
now carries a full-page screenshot of the moment it finished, embedded
30+
in `--html`/`--json` output — a report reader can see what the check
31+
actually saw without re-running formwatch against a possibly-
32+
already-changed page. A Pass carries no screenshot: it needs no
33+
evidence, and capturing one for every check on every run would just
34+
bloat history for no benefit.
2835

2936
### Fixed
3037

‎Cargo.lock‎

Lines changed: 11 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ anyhow = "1"
1919
askama = "0.12"
2020
chrono = { version = "0.4", default-features = false, features = ["clock"] }
2121
owo-colors = "4"
22+
base64 = "0.23.1"

‎README.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ otherwise) — safe to gate a CI job or a cron alert on. `--json` prints the
8989
run(s) as JSON instead of colored text for anything scripting against
9090
formwatch's output.
9191

92+
Any check that comes back WARN or FAIL carries a full-page screenshot of
93+
the moment it finished, embedded directly in `--html`/`--json` output (a
94+
PASS carries none — it needs no evidence). The plain-text/colored output
95+
just notes that one was captured, since a terminal can't render it.
96+
9297
## Custom checks
9398

9499
Anything specific to your forms that the built-in checks don't cover — a

‎src/checks.rs‎

Lines changed: 82 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,21 @@ pub struct CheckResult {
3838
/// Human-readable explanation of the verdict — what was found, and
3939
/// often the raw counts/booleans a report reader would want to see.
4040
pub detail: String,
41+
/// A `data:image/png;base64,...` screenshot of the page at the moment
42+
/// this check finished, present only when `status` isn't `Pass` — a
43+
/// Pass needs no evidence, and capturing one for every check on every
44+
/// run would bloat history for no benefit. `#[serde(default)]` so
45+
/// history files written before this field existed still deserialize.
46+
#[serde(default, skip_serializing_if = "Option::is_none")]
47+
pub screenshot: Option<String>,
4148
}
4249

4350
fn result(name: &str, status: Status, detail: impl Into<String>) -> CheckResult {
4451
CheckResult {
4552
name: name.to_string(),
4653
status,
4754
detail: detail.into(),
55+
screenshot: None,
4856
}
4957
}
5058

@@ -903,6 +911,27 @@ pub async fn check_autofill_hints(page: &Page) -> Result<CheckResult> {
903911
/// user-requested `--wait`, rather than sharing this default.
904912
const CHECK_TIMEOUT: Duration = Duration::from_secs(20);
905913

914+
/// Captures a full-page PNG screenshot as a ready-to-embed
915+
/// `data:image/png;base64,...` URI. Best-effort: a screenshot is
916+
/// supporting evidence, not the check result itself, so a capture
917+
/// failure (e.g. a page that navigated away mid-check) returns `None`
918+
/// rather than turning a real check result into an error.
919+
async fn capture_screenshot(page: &Page) -> Option<String> {
920+
use base64::Engine;
921+
let png = page
922+
.screenshot(
923+
chromiumoxide::page::ScreenshotParams::builder()
924+
.full_page(true)
925+
.build(),
926+
)
927+
.await
928+
.ok()?;
929+
Some(format!(
930+
"data:image/png;base64,{}",
931+
base64::engine::general_purpose::STANDARD.encode(png)
932+
))
933+
}
934+
906935
/// Runs one check's future to completion, converting an Err *or* a hang
907936
/// past `timeout` into a Warn result instead of letting it propagate or
908937
/// block forever. `run_all` checks many real-world pages unattended
@@ -912,7 +941,12 @@ const CHECK_TIMEOUT: Duration = Duration::from_secs(20);
912941
/// erase every other check's result for that form, or hang the whole
913942
/// tool indefinitely — previously there was no timeout anywhere in the
914943
/// check engine at all.
915-
async fn run_safely(
944+
///
945+
/// Pure conversion logic, deliberately kept separate from [`run_safely`]
946+
/// so its error/timeout-handling can be unit tested without a real Page
947+
/// (a synthetic future is enough) — [`run_safely`]'s extra step of
948+
/// capturing a screenshot genuinely needs one.
949+
async fn run_with_timeout(
916950
name: &str,
917951
timeout: Duration,
918952
fut: impl std::future::Future<Output = Result<CheckResult>>,
@@ -931,6 +965,24 @@ async fn run_safely(
931965
}
932966
}
933967

968+
/// [`run_with_timeout`], additionally attaching a screenshot to any
969+
/// non-Pass result (a real Fail/Warn, or a synthetic one from
970+
/// `run_with_timeout`'s own error/timeout branches) — a report reader
971+
/// shouldn't have to re-run formwatch against a possibly-already-changed
972+
/// page just to see what the check actually saw.
973+
async fn run_safely(
974+
page: &Page,
975+
name: &str,
976+
timeout: Duration,
977+
fut: impl std::future::Future<Output = Result<CheckResult>>,
978+
) -> CheckResult {
979+
let mut check = run_with_timeout(name, timeout, fut).await;
980+
if check.status != Status::Pass {
981+
check.screenshot = capture_screenshot(page).await;
982+
}
983+
check
984+
}
985+
934986
/// Runs every built-in check against `page` and returns all of their
935987
/// results — always, even if one check errors or times out internally,
936988
/// or a real `--submit` navigates away from the form partway through.
@@ -967,6 +1019,7 @@ pub async fn run_all(page: &Page, allow_submit: bool, wait_secs: u64) -> Vec<Che
9671019
};
9681020

9691021
let submission = run_safely(
1022+
page,
9701023
"Submission flow",
9711024
WIZARD_TIMEOUT,
9721025
check_submission_flow(page, allow_submit),
@@ -981,27 +1034,43 @@ pub async fn run_all(page: &Page, allow_submit: bool, wait_secs: u64) -> Vec<Che
9811034

9821035
vec![
9831036
submission,
984-
run_safely("Accessibility", CHECK_TIMEOUT, check_accessibility(page)).await,
9851037
run_safely(
1038+
page,
1039+
"Accessibility",
1040+
CHECK_TIMEOUT,
1041+
check_accessibility(page),
1042+
)
1043+
.await,
1044+
run_safely(
1045+
page,
9861046
"Mobile usability",
9871047
CHECK_TIMEOUT,
9881048
check_mobile_usability(page),
9891049
)
9901050
.await,
9911051
run_safely(
1052+
page,
9921053
"Validation errors",
9931054
CHECK_TIMEOUT,
9941055
check_validation_errors(page),
9951056
)
9961057
.await,
9971058
run_safely(
1059+
page,
9981060
"Required documents",
9991061
CHECK_TIMEOUT,
10001062
check_required_documents(page),
10011063
)
10021064
.await,
1003-
run_safely("Autofill hints", CHECK_TIMEOUT, check_autofill_hints(page)).await,
10041065
run_safely(
1066+
page,
1067+
"Autofill hints",
1068+
CHECK_TIMEOUT,
1069+
check_autofill_hints(page),
1070+
)
1071+
.await,
1072+
run_safely(
1073+
page,
10051074
"Input persistence",
10061075
input_persistence_timeout,
10071076
check_input_persistence(page, wait_secs),
@@ -1046,7 +1115,7 @@ pub async fn run_custom_checks(page: &Page, dir: &Path) -> Result<Vec<CheckResul
10461115
})
10471116
.await;
10481117

1049-
results.push(match outcome {
1118+
let mut check = match outcome {
10501119
Ok(Ok(value)) => match value.get("status").and_then(|s| s.as_str()) {
10511120
Some("Pass") => result(&name, Status::Pass, detail_of(&value)),
10521121
Some("Warn") => result(&name, Status::Warn, detail_of(&value)),
@@ -1066,7 +1135,11 @@ pub async fn run_custom_checks(page: &Page, dir: &Path) -> Result<Vec<CheckResul
10661135
Status::Warn,
10671136
format!("custom check timed out after {}s", CHECK_TIMEOUT.as_secs()),
10681137
),
1069-
});
1138+
};
1139+
if check.status != Status::Pass {
1140+
check.screenshot = capture_screenshot(page).await;
1141+
}
1142+
results.push(check);
10701143
}
10711144
Ok(results)
10721145
}
@@ -1084,21 +1157,21 @@ mod tests {
10841157
use super::*;
10851158

10861159
#[tokio::test]
1087-
async fn run_safely_converts_an_error_into_a_warn_result_instead_of_propagating() {
1160+
async fn run_with_timeout_converts_an_error_into_a_warn_result_instead_of_propagating() {
10881161
let failing = async { Err::<CheckResult, _>(anyhow::anyhow!("boom")) };
1089-
let outcome = run_safely("Some check", Duration::from_secs(5), failing).await;
1162+
let outcome = run_with_timeout("Some check", Duration::from_secs(5), failing).await;
10901163
assert_eq!(outcome.name, "Some check");
10911164
assert_eq!(outcome.status, Status::Warn);
10921165
assert!(outcome.detail.contains("boom"));
10931166
}
10941167

10951168
#[tokio::test]
1096-
async fn run_safely_converts_a_hang_into_a_warn_result_instead_of_blocking_forever() {
1169+
async fn run_with_timeout_converts_a_hang_into_a_warn_result_instead_of_blocking_forever() {
10971170
// A future that never resolves (an infinite JS loop, a Promise
10981171
// that never settles) used to hang run_all — and by extension
10991172
// the whole `monitor` run — indefinitely, with no way to recover.
11001173
let hangs = std::future::pending::<Result<CheckResult>>();
1101-
let outcome = run_safely("Some check", Duration::from_millis(50), hangs).await;
1174+
let outcome = run_with_timeout("Some check", Duration::from_millis(50), hangs).await;
11021175
assert_eq!(outcome.name, "Some check");
11031176
assert_eq!(outcome.status, Status::Warn);
11041177
assert!(

‎src/history.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ mod tests {
165165
name: name.to_string(),
166166
status,
167167
detail: String::new(),
168+
screenshot: None,
168169
}
169170
}
170171

‎src/main.rs‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,15 @@ fn print_check(check: &checks::CheckResult) {
329329
checks::Status::Warn => check.status.label().yellow().to_string(),
330330
checks::Status::Fail => check.status.label().red().to_string(),
331331
};
332-
println!(" [{label}] {}: {}", check.name, check.detail);
332+
let screenshot_note = if check.screenshot.is_some() {
333+
" (screenshot captured — see --html/--json report)"
334+
} else {
335+
""
336+
};
337+
println!(
338+
" [{label}] {}: {}{screenshot_note}",
339+
check.name, check.detail
340+
);
333341
}
334342

335343
fn print_changes(changes: &[history::CheckChange]) {
@@ -378,6 +386,7 @@ mod tests {
378386
name: "Accessibility".to_string(),
379387
status,
380388
detail: String::new(),
389+
screenshot: None,
381390
}
382391
}
383392

‎src/report.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ mod tests {
8585
name: "Accessibility".to_string(),
8686
status,
8787
detail: String::new(),
88+
screenshot: None,
8889
};
8990
let older = RunResult {
9091
name: "x".into(),

‎src/runner.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub async fn run_one(
2929
name: "Custom checks".to_string(),
3030
status: checks::Status::Warn,
3131
detail: format!("couldn't run --checks-dir {}: {e:#}", dir.display()),
32+
screenshot: None,
3233
}),
3334
}
3435
}
@@ -39,6 +40,7 @@ pub async fn run_one(
3940
name: "Page load".to_string(),
4041
status: checks::Status::Fail,
4142
detail: format!("{e:#}"),
43+
screenshot: None,
4244
}],
4345
};
4446

‎templates/report.html‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
.badge.fail { background: #c62828; }
1919
.changes { padding: 0.5rem 1rem; font-size: 0.85rem; background: #fffbea; border-top: 1px solid #eee; }
2020
.changes ul { margin: 0.3rem 0 0 1.1rem; padding: 0; }
21+
.screenshot { max-width: 100%; max-height: 20rem; margin-top: 0.5rem; border: 1px solid #ddd; border-radius: 4px; display: block; }
2122
</style>
2223
</head>
2324
<body>
@@ -32,7 +33,12 @@ <h2>{{ form.run.name }}<small>{{ form.run.url }} &middot; last checked {{ form.w
3233
{% for check in form.run.checks %}
3334
<tr>
3435
<td><span class="badge {{ check.status.css_class() }}">{{ check.status.label() }}</span></td>
35-
<td><strong>{{ check.name }}</strong><br>{{ check.detail }}</td>
36+
<td>
37+
<strong>{{ check.name }}</strong><br>{{ check.detail }}
38+
{% if let Some(shot) = check.screenshot %}
39+
<img class="screenshot" src="{{ shot }}" alt="{{ check.name }} screenshot">
40+
{% endif %}
41+
</td>
3642
</tr>
3743
{% endfor %}
3844
</tbody>

0 commit comments

Comments
 (0)