Skip to content

fix: retain javascript errors and console logs across navigation - #317

Merged
MrPunyapal merged 4 commits into
pestphp:5.xfrom
helmipradita:fix/retain-js-errors-across-navigation
Nov 19, 2026
Merged

MrPunyapal merged 4 commits into
pestphp:5.xfrom
helmipradita:fix/retain-js-errors-across-navigation

Conversation

@helmipradita

Copy link
Copy Markdown
Contributor

Summary

Fixes pestphp/pest#2026.

When performing full-page navigations in browser tests (via $page->navigate(), clicking links, or submitting forms), the browser tears down the previous window object and creates a new one. InitScript.php runs on every document initialization and previously reset window.__pestBrowser to empty arrays:

window.__pestBrowser = {
    jsErrors: [],
    consoleLogs: []
};

As a result, any JavaScript errors or console logs from earlier pages in that same browser tab session were lost before assertNoJavaScriptErrors() or assertNoConsoleLogs() ran on the subsequent page.

Solution

  • In src/Playwright/InitScript.php, persist jsErrors and consoleLogs using sessionStorage in a defensive try / catch block. sessionStorage natively persists across full-page navigations within the same browser tab and remains isolated to that page session.
  • When new errors or console logs arrive, sync them back to sessionStorage.
  • Added regression tests in AssertNoJavaScriptErrorsTest.php and AssertNoConsoleLogsTest.php asserting that errors and logs originating from earlier pages remain detectable after $page->navigate().

Signed-off-by: Helmi Pradita <helmipraditaa@gmail.com>
@helmipradita
helmipradita force-pushed the fix/retain-js-errors-across-navigation branch from 7a2cf18 to bb8a1fc Compare November 16, 2026 20:50
@MrPunyapal

Copy link
Copy Markdown
Member

Please preserve errors across origin changes in the same tab. I reproduced one recorded error becoming zero after navigating from 127.0.0.1 to localhost on the same server. Add a cross-origin regression and retain the collected errors outside origin-scoped sessionStorage.

….name

sessionStorage is origin-scoped, so navigating 127.0.0.1 -> localhost on the
same server dropped recorded errors/logs. Carry the payload in window.name
(namespaced), which persists across same-tab navigations regardless of origin.
Add cross-origin regression tests for both jsErrors and consoleLogs.
@helmipradita

Copy link
Copy Markdown
Contributor Author

Thanks @MrPunyapal — addressed in 2aaf7f7:

  • Root cause: sessionStorage is origin-scoped, so navigating 127.0.0.1 → localhost on the same server wiped the recorded errors/logs.
  • Fix: the collected state is now carried in window.name (namespaced with a __pestBrowser: prefix so unrelated window.name usage is untouched), which persists across same-tab navigations regardless of origin. Unrelated/non-prefixed window.name values are ignored.
  • Cross-origin regression: added tests for both assertNoJavaScriptErrors and assertNoConsoleLogs that visit /first on the canonical host then navigate to the same server under the alternate host (127.0.0.1 ↔ localhost), asserting the recorded error/log survives.

I verified the persistence mechanism (payload survives an origin change; unrelated window.name ignored) with a standalone harness; the full browser suite runs in CI.

@MrPunyapal

Copy link
Copy Markdown
Member

Please avoid storing the collector in application-owned window.name. A single console.log() replaces an existing window name with Pest’s payload. Keep the collected state outside application-owned properties and add a regression proving the original name is preserved.

@helmipradita

Copy link
Copy Markdown
Contributor Author

Hi @MrPunyapal, addressed in 92a3d37:

  1. State moved out of application space: Removed the window.name storage and the window.__pestBrowser collector object from the page. Logs and errors are now reported to the PHP side through an internal Playwright binding (__pestBrowserReport) registered on the browser context.
  2. Persistence across cross-origin navigation: The PHP client stores records in BrowserLogCollector, keyed by the main frame GUID. Because the frame GUID is maintained across same-tab cross-origin navigations, entries from earlier origins remain available to assertions on the final page.
  3. Regression test added: Added it('does not overwrite an application-owned window.name while collecting logs') verifying that pre-existing application window.name values remain intact across cross-origin navigations while logs are still captured.
  4. Lint & static analysis: Fixed Pint import/class element order and resolved PHPStan typing; the full suite passes (249 passed, 1 skipped on Linux).

@MrPunyapal
MrPunyapal merged commit 102e9ea into pestphp:5.x Nov 19, 2026
@MrPunyapal

Copy link
Copy Markdown
Member

thanks

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.

[Browser] assertNoJavaScriptErrors() loses errors after full-page navigation

2 participants