Skip to content

feat(test): attach accessibility tree on test failure via saveTreeOnFailure#111

Open
farhanlabib wants to merge 2 commits into
mobile-next:mainfrom
farhanlabib:feat/save-tree-on-failure
Open

feat(test): attach accessibility tree on test failure via saveTreeOnFailure#111
farhanlabib wants to merge 2 commits into
mobile-next:mainfrom
farhanlabib:feat/save-tree-on-failure

Conversation

@farhanlabib
Copy link
Copy Markdown
Contributor

@farhanlabib farhanlabib commented May 11, 2026

Motivation

When a test fails, the screenshot tells you what the screen looks like, but not why the locator didn't match. The accessibility tree has that answer — element labels, types, visibility flags, and bounds — but there's currently no way to capture it automatically on failure.

What changed

  • Added saveTreeOnFailure?: boolean to MobilewrightConfig in packages/mobilewright/src/config.ts
  • Added a saveTreeOnFailure fixture option in @mobilewright/test (same pattern as autoAppLaunch) that reads the value from config, defaulting to false
  • When a test fails and saveTreeOnFailure: true, the screen fixture calls viewTree() and attaches the result as view-tree-on-failure (application/json) to the test report — visible alongside the screenshot in the HTML report

The feature is opt-in and off by default, so existing projects are unaffected.

Usage

// mobilewright.config.js
module.exports = defineConfig({
  saveTreeOnFailure: true,
});

The view-tree-on-failure attachment appears in the HTML report under the failed test, containing the full ViewNode[] tree serialised as indented JSON.

Test plan

  • Run a passing test with saveTreeOnFailure: true — no attachment added
  • Run a failing test with saveTreeOnFailure: trueview-tree-on-failure JSON attachment appears in the HTML report alongside the screenshot
  • Run a failing test with saveTreeOnFailure: false (default) — no JSON attachment, existing screenshot-on-failure still works normally
  • Device disconnect mid-test — failure is reported cleanly, no crash from the viewTree call (covered by the inner try/catch)

…ailure

Screenshots show what the screen looks like when a test fails, but not
why a locator didn't match. The accessibility tree carries that context —
element labels, types, visibility flags, and bounds — which is exactly
what you need to debug a failing getBy* call.

Add saveTreeOnFailure: boolean to MobilewrightConfig. When enabled, the
screen fixture calls viewTree() after a failure and attaches the result
as view-tree-on-failure (JSON) to the HTML report, next to the existing
screenshot. Opt-in, defaults to false, no impact on existing projects.
@farhanlabib farhanlabib force-pushed the feat/save-tree-on-failure branch from 4dcb977 to dd23065 Compare May 11, 2026 07:03
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.

2 participants