Visitar URL original
test: migrate screenshot handler tests to mocks by Patrick-SCH03 · Pull Request #3183 · ChromeDevTools/chrome-devtools-mcp · GitHub
Skip to content

test: migrate screenshot handler tests to mocks - #3183

Merged
OrKoN merged 2 commits into
ChromeDevTools:mainfrom
Patrick-SCH03:test/migrate-screenshot-tests-to-mocks
Oct 16, 2026
Merged

OrKoN merged 2 commits into
ChromeDevTools:mainfrom
Patrick-SCH03:test/migrate-screenshot-tests-to-mocks

Conversation

@Patrick-SCH03

Copy link
Copy Markdown
Contributor

Part of #2639.

Five take_screenshot handler tests launched a real browser only to check which arguments reach page.screenshot / the element handle and what ends up in the response. This migrates them in place to createHandlerMocks and asserts the exact calls with sinon.assert, following AGENTS.md:

  • ignores quality: asserts a PNG capture is requested with quality: undefined even when quality: 0 is passed, plus the response line and the attached image/png data.
  • with jpeg: asserts the requested quality is forwarded to page.screenshot with type: 'jpeg', and the image is attached as image/jpeg.
  • with webp: asserts the --screenshot-quality CLI value is used when no quality param is given, and the image is attached as image/webp.
  • disposes the element handle after an element screenshot / when the capture fails: use createMockElementHandle() instead of spying on a real handle, and assert dispose is called once in both the success and the rejection path (and no image is attached on failure).

Kept on the real browser:

  • with default options, with full page, with element uid, with filePath: the end-to-end checks that a real capture/file write works.
  • The downscales ... / honors screenshotMaxWidth ... / does not resize ... tests: they check real pixel sizes; the clip math is already covered by the mock tests in the downscale clip block.
  • with unwritable filePath and with malformed filePath: they are currently the only tests that exercise the real McpContext.saveFile write failure, so moving them belongs with tests/McpContext.test.ts rather than here.
  • honors screenshotFormat default from CLI args: left untouched because chore: declare type on properties #3082 edits the same lines.

Testing

  • npm run build && NODE_TEST_REPORTER=spec node scripts/test.js tests/tools/screenshot.test.ts: all migrated tests pass; each now takes about 30–100 ms instead of about 80–700 ms. (On my Windows machine with full page resulting in a large screenshot fails with Page is too large both before and after this change; it is not touched here.)
  • Sanity check that they still catch regressions: forwarding quality for PNG, dropping the request quality, dropping the screenshotQuality fallback, hard-coding the image/png MIME type, and skipping the using disposal of the element handle each make the corresponding migrated test(s) fail.
  • eslint and prettier are clean; only tests/tools/screenshot.test.ts changes.

Part of ChromeDevTools#2639. Moves the format/quality and element handle disposal
tests in tests/tools/screenshot.test.ts from a real browser to
createHandlerMocks and asserts the exact page.screenshot,
element.screenshot, dispose and response calls with sinon.assert.
@OrKoN
OrKoN self-requested a review October 16, 2026 08:07

@OrKoN OrKoN left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of minor improvements:

  1. tests/tools/screenshot.test.ts (disposes the element handle ..., both cases): add sinon.assert.notCalled(page.pptrPage.screenshot). It pins that the element branch was taken rather than the page branch, which the old real-browser version implied by capturing an actual element.

  2. tests/tools/screenshot.test.ts (with jpeg): the old test ran JPEG with no quality from either the request or the CLI; that branch (quality: undefined for a lossy format) is now uncovered. Either keep with jpeg without quality and move the quality: 50 forwarding into its own case, or accept the gap; it is a one-line branch.

Keep the jpeg case without any quality, as the real-browser test had,
and move the quality forwarding check into its own case. Assert that
page.screenshot is not called in the element handle disposal tests.
@Patrick-SCH03

Copy link
Copy Markdown
Contributor Author

Thanks! Done in 0b0455a: both disposal tests now assert sinon.assert.notCalled(page.pptrPage.screenshot), and with jpeg is back to no quality from either the request or the CLI (asserting quality: undefined), with the quality: 50 forwarding moved to its own forwards the requested quality for jpeg case.

@OrKoN
OrKoN enabled auto-merge October 16, 2026 08:57
@OrKoN
OrKoN added this pull request to the merge queue Oct 16, 2026
Merged via the queue into ChromeDevTools:main with commit 80b395f Oct 16, 2026
14 checks passed
zjdCander pushed a commit to zjdCander/chrome-devtools-mcp that referenced this pull request Oct 19, 2026
## Summary

Add a **Pre-submit checklist** section to `AGENTS.md`. Each item comes
from a recent review finding or a landed bug fix in this repository.

The checklist has five groups:

- **Scope and PR description**: one change per PR, title and commit type
match, description lists every behavior change and matches the diff,
`npm run gen` and `npm run format` ran.
- **Design**: fix in the lowest layer that owns the data, fix upstream
(Puppeteer, devtools-frontend) first, one helper for repeated code,
options objects instead of positional `undefined` arguments, validation
in Zod schemas.
- **Correctness and edge cases**: no stale state after a failed call,
artifacts written before a throw, abandoned async continuations, mutex
guards, file removal races, `dispose()` releases every resource, secrets
never reach logs, path parameters validated even when they parse as
URLs.
- **Tests**: every new function has a test, no vacuous assertions,
`notCalled` targets the first call after validation, exact-argument
assertions, reusable stubs in `tests/mocks.ts`, bounded timeouts, no
second CDP session for event waits.
- **Documentation and output**: tool descriptions state when a parameter
is ignored, skills stay generic, one-sentence inline comments.

Sources: review comments on ChromeDevTools#2679, ChromeDevTools#2797, ChromeDevTools#2859, ChromeDevTools#2881, ChromeDevTools#2901, ChromeDevTools#3048,
ChromeDevTools#3082, ChromeDevTools#3109, ChromeDevTools#3120, ChromeDevTools#3132, ChromeDevTools#3134, ChromeDevTools#3139, ChromeDevTools#3148, ChromeDevTools#3156, ChromeDevTools#3158, ChromeDevTools#3161,
ChromeDevTools#3182, ChromeDevTools#3183, ChromeDevTools#3186, ChromeDevTools#3187, ChromeDevTools#3188, ChromeDevTools#3189, ChromeDevTools#3192, ChromeDevTools#3196, ChromeDevTools#3197, ChromeDevTools#3198,
ChromeDevTools#3203, ChromeDevTools#3204, and fixes ChromeDevTools#3150, ChromeDevTools#3160, ChromeDevTools#3185, ChromeDevTools#3202, ChromeDevTools#3207, ChromeDevTools#3208.

Existing content in `AGENTS.md` is unchanged.

Closes ChromeDevTools#3222
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