Skip to content

feat(cli): add inspect command to dump live accessibility tree#112

Open
farhanlabib wants to merge 2 commits into
mobile-next:mainfrom
farhanlabib:feat/inspect-command
Open

feat(cli): add inspect command to dump live accessibility tree#112
farhanlabib wants to merge 2 commits into
mobile-next:mainfrom
farhanlabib:feat/inspect-command

Conversation

@farhanlabib
Copy link
Copy Markdown
Contributor

@farhanlabib farhanlabib commented May 11, 2026

Motivation

When writing tests, the main friction is not knowing the exact label, text, or type of an element on screen. The only option was to add screen.viewTree() inside a test and read the console output — which requires running a test just to inspect the UI.

What changed

Added a new inspect CLI command (packages/mobilewright/src/commands/inspect.ts) that connects to the device, calls getViewHierarchy(), and renders the accessibility tree directly in the terminal — no test needed.

npx mobilewright inspect
CMF by Nothing Phone 2 Pro  47 nodes

└── FrameLayout  [visible]
    ├── LinearLayout  [visible]
    │   └── TextView  "Pathao"  [visible]
    ├── RecyclerView  [visible]
    │   ├── LinearLayout  label="Bike"  [visible]
    │   │   └── TextView  "Bike"  [visible]
    │   └── LinearLayout  label="Car"  [visible]
    └── BottomNavigationView  [visible]

Pass --json to output raw ViewNode[] — useful for piping to jq or saving a snapshot to diff between two app states:

npx mobilewright inspect --json
npx mobilewright inspect --json | jq '.[] | select(.label != null)'
npx mobilewright inspect --json > before.json

Options

Flag Description
-d, --device <id> Target a specific device (same as screenshot)
--url <url> mobilecli server URL
--json Output raw ViewNode[] JSON instead of the terminal tree

Device resolution respects deviceName from mobilewright.config.js for automatic matching when multiple devices are connected.

Test plan

  • Run npx mobilewright inspect with a device connected and an app open — tree renders in the terminal with correct labels, text, and visibility
  • Run with --json — outputs valid JSON array of ViewNode[]
  • Run npx mobilewright inspect --json | jq 'length' — returns node count
  • Run with no device connected — clear error message directing to mobilewright doctor
  • Run with multiple devices and no --device flag — lists devices and exits cleanly

…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.
When writing tests, the main friction is not knowing the exact label,
text, or type of an element on screen. The only option was to add
screen.viewTree() inside a test and read the console output — which
requires running a test just to inspect the UI.

The inspect command solves this directly: run it with the app open on
the screen you want to target, and it prints the full accessibility tree
in the terminal with element types, labels, text, and visibility — no
test needed.

  npx mobilewright inspect

Supports --json to output raw ViewNode[] for piping to jq or saving as
a snapshot to diff between two app states:

  npx mobilewright inspect --json | jq '.[] | select(.label != null)'

Respects deviceName from config for automatic device resolution, and
accepts -d/--device to target a specific device when multiple are connected.
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