Skip to content

fix: wait up to --timeout for a missing screenshot selector - #139

Merged
cryptotavares merged 1 commit into
mainfrom
cryptotavares/screenshot-timeout-waits
Dec 2, 2026
Merged

cryptotavares merged 1 commit into
mainfrom
cryptotavares/screenshot-timeout-waits

Conversation

@cryptotavares

Copy link
Copy Markdown
Collaborator

Part of the mm CLI v2 effort.

Summary: mm screenshot --selector (and --frame) with an explicit --timeout now waits up to that timeout for a missing element, the same way mm click does. Without --timeout, a missing element still fails after 2 s.

Problem

mm click fails fast on a missing target only when --timeout is not set. With --timeout, it waits that long for the element to appear. mm screenshot always failed after 2 s, so --timeout did not help on a screen that is still loading.

# core 0.16.0, element added 3.5 s after the call
mm screenshot --selector '#late' --timeout 10000
exit 4  MM_TARGET_NOT_FOUND  Selector #late matched 0 elements after 2000ms

Solution

PlaywrightDriver.screenshot gives waitForTarget the explicit timeout as its missing-target wait, else MISSING_TARGET_WAIT_MS. screenshotTool already passes timeoutMs only when the user set it, so timeoutMs === undefined is the same rule that clickTool uses for failFastWhenMissing.

{ missingTargetWaitMs: options.timeoutMs ?? MISSING_TARGET_WAIT_MS },
flowchart TD
    A["mm screenshot --selector X (or --frame)"] --> B{"--timeout set?"}
    B -->|no| C["waitForTarget: 15 s budget, missing check 2 s"]
    B -->|yes| D["waitForTarget: missing check = --timeout"]
    C -->|"no match after 2 s"| P["MM_TARGET_NOT_FOUND"]
    D -->|"no match after --timeout"| P
    C -->|visible| E[capture element]
    D -->|"appears and is visible"| E
    P --> X["Exit 4. No image saved."]

    classDef new fill:#fff3cd,stroke:#d39e00
    classDef stop fill:#f8d7da,stroke:#c82333
    class D new
    class P,X stop
Loading

Yellow boxes are new. Red boxes stop the capture.

What changes

"After" is from the Extension e2e context, headless, with a yalc build of this branch. "Before" follows from the 0.16.0 code; the first row was also seen live on 0.16.0.

Call Before (0.16.0) After
--selector '#late' --timeout 10000, element added after 3.5 s MM_TARGET_NOT_FOUND after 2000ms, exit 4 Captured, exit 0 (4.2 s)
--selector '#late', element added after 3.5 s MM_TARGET_NOT_FOUND after 2000ms, exit 4 No change (2.2 s)
--selector '#late' --timeout 3000, never added MM_TARGET_NOT_FOUND after 2000ms, exit 4 matched 0 elements after 3000ms, exit 4 (3.2 s)
--selector '#late' --timeout 35000, never added MM_TARGET_NOT_FOUND after 2000ms, exit 4 matched 0 elements after 35000ms, exit 4 (35.2 s, no queue timeout)
--frame testid:late-frame --selector '#inner' --timeout 10000, iframe added after 3.5 s MM_TARGET_NOT_FOUND, exit 4 Captured, exit 0 (4.1 s)
--frame testid:does-not-exist … >> xpath=descendant-or-self::iframe matched 0 elements after 2000ms No change (2.2 s)
--frame testid:does-not-exist --timeout 3000 Same, after 2000ms Same message, after 3000ms (3.2 s)
Element exists but stays hidden, --timeout 3000 MM_SCREENSHOT_FAILED, exit 1 No change

Decisions for reviewers

Decision Reason
Reverse the #129 decision ("a missing target fails after the 2 s check even when --timeout is larger") A screen that is still loading is a common reason to pass --timeout. mm click already waits in that case, and the two commands now follow one rule.
Derive the wait from options.timeoutMs in the driver, not from a new failFastWhenMissing flag screenshotTool already forwards timeoutMs only when the user set it. A second flag would repeat the same fact.
No change to the queue or CLI budget resolveActionTimeoutMs already returns the explicit timeoutMs for a screenshot with selector or frame. The queue and the CLI HTTP wait derive from it (--timeout 35000 ran the full 35 s). The schema caps timeoutMs at 60000.

Review guide

# File What to check
1 src/platform/playwright-driver.ts missingTargetWaitMs: options.timeoutMs ?? MISSING_TARGET_WAIT_MS
2 src/platform/types.ts PlatformScreenshotOptions.timeoutMs docblock
3 src/tools/screenshot.test.ts, src/platform/playwright-driver.test.ts One test per row of the table, for --selector, --frame and both
4 SKILL.md, README.md The rule, stated as current behaviour
Testing
  • yarn build, yarn lint and CI=1 yarn test pass (76 files, 2949 tests). Coverage: 98.13 % statements, 95.52 % branches, 97.51 % functions, 98.22 % lines.
  • screenshot.test.ts runs the real waitForTarget against a locator mock whose element is attached after 3.5 s, never, or attached but hidden. For each of --selector, --frame, and --frame with --selector:
    • no timeoutMs, missing: MM_TARGET_NOT_FOUND, matched 0 elements after 2000ms;
    • timeoutMs: 10000, element appears after 3.5 s: captured;
    • timeoutMs: 3000, never appears: MM_TARGET_NOT_FOUND, attached wait of about 3000 ms;
    • hidden element: MM_SCREENSHOT_FAILED.
  • Mutation check: revert the driver line and 7 tests fail.
  • Live: Extension e2e context, headless, unlock screen (not unlocked). Elements were injected with mm cdp Runtime.evaluate and removed afterwards.
Docs changed
  • SKILL.md, mm screenshot: without --timeout, a missing selector or frame fails after 2s; with --timeout, the command waits that long.
  • README.md: the mm screenshot CLI row and the screenshot tool row say the same.

@cryptotavares
cryptotavares marked this pull request as ready for review December 2, 2026 13:17
@cryptotavares
cryptotavares requested a review from a team as a code owner December 2, 2026 13:17
@cryptotavares
cryptotavares merged commit b6ec1f0 into main Dec 2, 2026
27 checks passed
@cryptotavares
cryptotavares deleted the cryptotavares/screenshot-timeout-waits branch December 2, 2026 19:24
LabinatorSolutions pushed a commit to LabinatorSolutions/metamask-extension that referenced this pull request Dec 4, 2026
## **Description**

> **Summary:** Bumps the dev-only `@metamask/client-mcp-core` from
0.16.0 to
[0.16.1](https://github.com/MetaMask/client-mcp-core/releases/tag/v0.16.1).
On the Extension, `mm <command> --help` prints help instead of running
the command, and `mm screenshot --selector --timeout` waits for a late
element. Refs stay stamped after a navigation. No app or test code
changes.

Follows
[MetaMask#48425](MetaMask#48425)
(0.16.0). Sibling skill PR:
[MetaMask/skills#328](MetaMask/skills#328).

### What changes

| File | Change |
|---|---|
| `package.json` | `@metamask/client-mcp-core` `0.16.0` → `0.16.1`
(exact pin, `devDependencies`) |
| `yarn.lock` | `client-mcp-core` 0.16.1; its dependency
`@metamask/device-mcp` `^0.9.0` → `^0.10.0` (resolves to 0.10.0). No
other entries change. |
| `test/e2e/playwright/llm-workflow/README.md` | The `--preset` note
drops the version. The daemon-log example names the target (`testid=…`).
|

### What 0.16.1 changes for the Extension (checked live, see Testing)

| Area | 0.16.0 | 0.16.1 |
|---|---|---|
| `mm launch --help`, `mm stop --help`, `mm click --help` | Runs the
command (launches, stops the daemon, clicks test ID `--help`) | Prints
help, exit 0, runs nothing
([MetaMask#137](MetaMask/client-mcp-core#137)) |
| `mm screenshot --selector X --timeout <ms>`, X appears late |
`MM_TARGET_NOT_FOUND` after 2 s | Waits up to `<ms>` and captures
([MetaMask#139](MetaMask/client-mcp-core#139)) |
| Refs and observation after a navigation | Refs could fall back to
`role=`/`text=`; the observation could show the old route | Refs are
stamped; the observation shows the new route
([MetaMask#131](MetaMask/client-mcp-core#131)) |

### Decisions

| Decision | Why |
|---|---|
| No `allow-scripts` / LavaMoat policy change | Both packages are
dev-only. `yarn allow-scripts list` reports no unconfigured packages. |
| No change to `test/e2e/playwright/llm-workflow` code | `yarn lint:tsc`
and the llm-workflow jest suites pass without code changes. |

### Review guide

1. `package.json` and `yarn.lock`: a version bump only (+10/−10 in the
lockfile).
2. `README.md`: two sentences. Both match the live run below.

## **Changelog**

CHANGELOG entry: null

## **Related issues**

Fixes: N/A. Core release:
https://github.com/MetaMask/client-mcp-core/releases/tag/v0.16.1

## **Manual testing steps**

1. `yarn install`, then `yarn webpack:tsc && yarn build:test`.
2. Run `yarn mm launch --help`. Check that it prints help and that no
`.mm-server` file appears.
3. `MM_HEADLESS=true yarn mm launch --context e2e --state default`, then
unlock.
4. Add an element after a few seconds with `yarn mm cdp
Runtime.evaluate` (`setTimeout`). Run `yarn mm screenshot --selector
'<css>' --timeout 10000`. Check that it captures the element.
5. `yarn mm navigate-settings --observe full`. Check that the
observation lists the settings test IDs.
6. `yarn mm cleanup && yarn mm stop`.

<!--
## **Screenshots/Recordings**

### **Before**

### **After**
-->

<details>
<summary>Testing details</summary>

Node v26.13.1 (`.nvmrc`).

| Check | Result |
|---|---|
| `yarn install --immutable` | exit 0 |
| `yarn dedupe --check` | exit 0 |
| `yarn lint:lockfile` | pass |
| `yarn allow-scripts list` | unconfigured packages: none |
| `yarn lint:tsc` | exit 0 |
| `yarn jest test/e2e/playwright/llm-workflow` | 13 suites, 169 tests
pass |
| oxfmt (README), prettier (`package.json`) | pass |

Live run with the 0.16.1 `mm` against a 13.60.0.0 test build (headless,
`--context e2e --state default`). `.mm-server` reports
`"version":"0.16.1"`. The run followed only the sibling skill
([skills#328](MetaMask/skills#328)).

| Command | Result |
|---|---|
| `mm launch --help`, `mm stop --help`, `mm click --help` (no daemon) |
exit 0, usage, no `.mm-server` |
| `mm stop --help`, `mm cleanup --help` (daemon up) | exit 0; `mm
status` shows the same daemon PID |
| `mm frobnicate` | exit 2, `MM_INVALID_INPUT` |
| `screenshot --selector '#late' --timeout 10000`, element added after 4
s | exit 0, 4.26 s |
| `screenshot --selector '#late'`, same injection | exit 4,
`MM_TARGET_NOT_FOUND`, `matched 0 elements after 2000ms`, 2.18 s |
| `navigate-home` → `navigate-settings --observe full` (×5) | 5 of 5
show the settings test IDs and no home test IDs |
| `navigate-home --observe none`, then `describe-screen` (×5) | 16
nodes, 16 elements with `data-mm-ref` each time |
| `mock-network requests --url esplora` |
`https://bitcoin-mainnet.infura.io/v3/[REDACTED]/esplora/blocks?apiKey=[REDACTED]`
|
| `mm launch --state custom --preset withMultipleAccounts` | `Warning:
unknown launch flag '--preset'`, then `MM_LAUNCH_FAILED` |
| `.mm-daemon.log` | `POST /tool/click 200 ok=false
code=MM_TARGET_NOT_FOUND testid=does-not-exist 2078ms` |

</details>

## **Pre-merge author checklist**

- [x] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've completed the PR template to the best of my ability
- [ ] I’ve included tests if applicable
- [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [ ] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **Pre-merge reviewer checklist**

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> **Low Risk**
> Dev-only dependency and documentation updates; no production app logic
or security-sensitive paths changed.
> 
> **Overview**
> Bumps the dev-only **`@metamask/client-mcp-core`** pin from `0.16.0`
to **`0.16.1`** in `package.json` and `yarn.lock`, which also resolves
its dependency **`@metamask/device-mcp`** to `0.10.0`. No extension or
test source changes.
> 
> The **`llm-workflow` README** is updated to match 0.16.1 behavior:
daemon log lines now document the **target** (e.g. `testid=…`), and the
custom-launch note drops a stale version reference for `--preset`.
> 
> Downstream, **`mm --help`** on subcommands prints usage instead of
executing the command, **`mm screenshot --selector --timeout`** honors
the wait window for late elements, and **refs/observation** stay correct
after navigation—behavior that lives in the bumped package, not this
repo.
> 
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
fe29d1d. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
cryptotavares added a commit to MetaMask/skills that referenced this pull request Dec 4, 2026
## Description

Part of the mm CLI v2 effort.

> **Summary:** Updates the Extension `testing/visual-testing` skill for
`@metamask/client-mcp-core`
[0.16.1](https://github.com/MetaMask/client-mcp-core/releases/tag/v0.16.1).
`mm screenshot --selector` with `--timeout` now waits for a late
element, so the skill stops telling agents to `mm wait-for` first. The
chart notes now say that the canvas is in a nested iframe.

Pairs with the Extension bump
[metamask-extension#48494](MetaMask/metamask-extension#48494).
Follows [#316](#316) (0.16.0).

### Problem

| Skill text (0.16.0) | 0.16.1 behaviour (live) |
|---|---|
| "A selector that matches no element within 2 s fails with
`MM_TARGET_NOT_FOUND`, also with a longer `--timeout`; … `mm wait-for`
it first" | `screenshot --selector '#late' --timeout 10000`, element
added after 4 s: captured, exit 0, 4.1 s
([#139](MetaMask/client-mcp-core#139)) |
| A canvas-only chart iframe: use `get-text --frame … --selector body` |
The canvas is in a nested iframe. `--frame testid:advanced-chart-iframe
--selector canvas` matches 0 elements; `--selector iframe` and
`#tv_chart_container` work
([#138](MetaMask/client-mcp-core#138)) |

### What changes

All files are under `domains/testing/skills/visual-testing/`.

| File | Change | Core PR |
|---|---|---|
| `references/cli-reference.md` › Why a click failed, screenshots |
Without `--timeout`: 2 s `MM_TARGET_NOT_FOUND`. With it: waits that long
| #139 |
| `references/cli-reference.md` › Iframes | `--frame` enters one level;
target the nested iframe or `#tv_chart_container` | #138 |
| `references/error-recovery.md` | `MM_TARGET_NOT_FOUND` row; screenshot
row: retry with `--timeout` | #139 |
| `repos/metamask-extension.md` | `MM_SCREENSHOT_FAILED` bullet; chart
paragraph | #139, #138 |
| `CHANGELOG.md` | `[Unreleased]` entry | |

### Decisions

| Decision | Why |
|---|---|
| No change for **#131** (refs after a navigation) | The skill names
`role=`/`text=` refs only for elements that cannot be stamped, which is
still true. It never says refs fall back after a page load, or that the
observation shows the old screen. |
| No change for **#137** (`mm <command> --help`) | The skill does not
mention `<command> --help`. |
| No change for **#134** (device-mcp `^0.10.0`) | Mobile only. |

Review: read `references/cli-reference.md` › Element, iframe and
viewport screenshots first.

## Type of Change

- [ ] New skill
- [x] Skill improvement/update
- [ ] Bug fix
- [ ] Documentation update
- [ ] Other (please describe):

## Skill Details (if adding a new skill)

N/A

## Checklist

- [x] I have read the
[CONTRIBUTING.md](https://github.com/MetaMask/skills/blob/main/CONTRIBUTING.md)
guidelines
- [x] My skill follows the
[SKILL_TEMPLATE.md](https://github.com/MetaMask/skills/blob/main/.github/SKILL_TEMPLATE.md)
format
- [x] I have tested this skill with an AI agent
- [x] My skill does not contain any secrets, private keys, or sensitive
data
- [x] I have added appropriate documentation
- [x] My changes don't break existing skills

## Testing

| Check | Result |
|---|---|
| `yarn lint` | exit 0 |
| `yarn test` | 106 tests pass, 0 fail |
| `yarn audit:skills` | 100 skills, 0 errors, 134 warnings (same count
as `main`) |
| `git diff --check` | clean |

<details>
<summary>Live check</summary>

Extension worktree of metamask-extension#48494 (npm
`@metamask/client-mcp-core` 0.16.1; `.mm-server` `"version":"0.16.1"`),
test build 13.60.0.0, headless, `--context e2e --state default`,
unlocked. Nothing was confirmed or signed. The agent followed only this
skill.

| Command | Result |
|---|---|
| `screenshot --selector '#late' --timeout 10000`, element added after 4
s | exit 0, 200×40, 4.26 s |
| `screenshot --selector '#late'`, same injection | exit 4, `Selector
#late matched 0 elements after 2000ms`, 2.18 s |
| `screenshot --selector '#never' --timeout 3000` | exit 4, `matched 0
elements after 3000ms` |
| `screenshot --selector head --timeout 3000` | exit 1,
`MM_SCREENSHOT_FAILED` |
| `screenshot --frame testid:late-frame --selector '#inner' --timeout
10000`, iframe added after 3.5 s | exit 0, 3.52 s |
| `screenshot --frame testid:does-not-exist` | exit 4 after 2 s |
| Chart (overlay recipe): `hover --frame testid:advanced-chart-iframe
--selector iframe` | `hovered: true` |
| `get-text` / `wait-for --frame testid:advanced-chart-iframe --selector
'#tv_chart_container'` | exit 0 |
| `hover … --selector canvas --timeout 3000` | exit 4, `… >> canvas
matched 0 elements after 3000ms` |
| `describe-screen --frame testid:advanced-chart-iframe` | `nodes: []` |
| `screenshot --frame testid:advanced-chart-iframe` | 798×321, candles
drawn |
| `navigate-home` → `navigate-settings --observe full` (×5) | settings
test IDs, 24 nodes, no home test IDs |
| `navigate-home --observe none`, then `describe-screen` (×5) | 16
nodes, 16 `[data-mm-ref]` elements |
| `mm launch --help`, `mm stop --help`, `mm click --help` | exit 0,
usage, no daemon started or stopped |
| `mm frobnicate` | exit 2, `MM_INVALID_INPUT`, no `.mm-server` |
| `mock-network requests --url esplora` |
`…/v3/[REDACTED]/esplora/blocks?apiKey=[REDACTED]` |
| `.mm-daemon.log` | `POST /tool/screenshot 200 ok=true selector=#late
file=late-timeout-….png 4117ms` |

</details>

## Additional Context

- Core release:
https://github.com/MetaMask/client-mcp-core/releases/tag/v0.16.1
- Extension bump:
MetaMask/metamask-extension#48494
- Previous update (0.16.0): #316
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