Skip to content

feat: save page source on failed assertions - #240

Closed
jeremy-langevin-twc wants to merge 1 commit into
pestphp:5.xfrom
jeremy-langevin-twc:feat/source-on-failure
Closed

feat: save page source on failed assertions#240
jeremy-langevin-twc wants to merge 1 commit into
pestphp:5.xfrom
jeremy-langevin-twc:feat/source-on-failure

Conversation

@jeremy-langevin-twc

Copy link
Copy Markdown

This PR adds an opt-in capability to save the full HTML source of the page when a browser assertion fails, alongside the failure screenshot, console logs, and JavaScript errors that are already captured:

// tests/Pest.php
pest()->browser()->source();

When enabled and a browser assertion fails, the page source is written to tests/Browser/Source/ and the failure message gains a line pointing to it, mirroring the existing screenshot line:

A screenshot of the page has been saved to [Tests/Browser/Screenshots/...]. The source of the page has been saved to [Tests/Browser/Source/...].

Why: a screenshot cannot show data-test attributes, hidden or collapsed elements, hydration state, or the actual markup a failing selector was matched against. Laravel Dusk stores page source on failure for exactly this reason, so this also closes a Dusk-parity gap.

Why default-off: rendered pages can be large, and page HTML can expose PII more structurally than a screenshot does.

Naming: source() follows the existing terse config grammar (debug(), diff(), headed()) and reuses the term the API already uses for page HTML (assertSourceHas()); the directory matches Dusk's tests/Browser/source in this repo's capitalization.

Behavior details:

  • The source dump is skipped in the same cases the failure screenshot is skipped — debug mode and screenshot-diff failures — so both artifacts follow one predictable condition.
  • Saving the source can never mask the original failure: it is wrapped so that an error in fetching the page content or writing the file leaves the assertion message (including the screenshot and console-log lines) intact.
  • Source::cleanup() runs at suite boot exactly like Screenshot::cleanup(), and tests/Browser/Source is gitignored like the screenshots directory.
  • Zero behavior change for anyone who doesn't call source().

This is a backwards-compatible, opt-in addition (SemVer minor). Covered by unit tests for the support class and browser tests for both the enabled and disabled failure paths; composer test passes.

@jeremy-langevin-twc

Copy link
Copy Markdown
Author

Closing this is not ready

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant