AGENT SKILL

Canvas 游戏浏览器交互测试

browser-canvas-testing

VS Code 原生浏览器工具在基于 canvas 的游戏(Phaser 3 等)中的行为指南。适用于与 HTML canvas 元素交互、点击游戏对象、通过浏览器工具验证 canvas 状态,或当 `clickElement` 在 canvas 上失败时。不适用于:React 组件测试(使用 ui-testing)或 Playwright E2E 测试(使用 webapp-testing)。

适用职业测试工程师
License 许可
MIT
最近更新
2026-08-16
Stars
★ 3
Forks
0

01 前言 Front Matter

name
browser-canvas-testing
description
VS Code native browser tool behavior for canvas-based games (Phaser 3, etc.). Use when interacting with HTML canvas elements, clicking game objects, verifying canvas state via browser tools, or when `clickElement` fails on canvas. DO NOT USE FOR: React component tests (use ui-testing) or Playwright E2E tests (use webapp-testing).

03 SKILL.md 正文

Browser Canvas Testing

Domain knowledge for using VS Code native browser tools against HTML canvas applications verified via VS Code source code review.

When to Use

  • Clicking game objects or UI elements rendered inside an HTML <canvas>
  • Verifying canvas visual state with screenshotPage
  • Discovering why readPage returns empty on a canvas-only page
  • Any Phaser 3 / WebGL / 2D canvas game interaction in browser tools

1. Why clickElement Cannot Target Canvas Objects

clickElement is selector-based only. The VS Code source (clickBrowserTool.ts) defines these parameters:

pageId, selector, ref, dblClick, button

There is no coordinate or position parameter. The implementation does exactly:

page.locator(selector).click();

A <canvas> element renders its content via GPU/CPU drawing calls — there are no child DOM elements for selectors to target. Even canvas as a CSS selector only clicks the canvas border box center, not a specific game object inside it.

Conclusion: clickElement cannot be used for canvas game interaction. Use runPlaywrightCode instead.

Source: clickBrowserTool.ts


2. Canvas Coordinate Conversion Formula

Canvas games use an internal game-space coordinate system that does not map directly to CSS page coordinates. Use getBoundingClientRect to convert.

// 1. Query canvas bounding rect (accounts for scaling and letterboxing)
const rect = await page.evaluate(() => {
  const c = document.querySelector("canvas");
  if (!c) throw new Error("Canvas element not found");
  const r = c.getBoundingClientRect();
  return { left: r.left, top: r.top, width: r.width, height: r.height };
});

// 2. Convert game-space coords → CSS page coords
const cssX = rect.left + (gameX / GAME_WIDTH) * rect.width;
const cssY = rect.top + (gameY / GAME_HEIGHT) * rect.height;

// 3. Click via Playwright mouse API
await page.mouse.click(cssX, cssY);

If the page has multiple <canvas> elements (e.g., a main scene canvas plus a HUD overlay), document.querySelector('canvas') returns the first one, which may not be the target. Use a specific selector instead: document.querySelector('#game canvas') or document.getElementById('game-canvas'). Inspect with page.evaluate(() => document.querySelectorAll('canvas').length) to detect multiple canvases.

Prerequisite: The canvas must be rendered before calling getBoundingClientRect(). An unrendered canvas returns {0,0,0,0}, causing the formula to click (0,0) silently. See Section 3 for the render-ready wait pattern.

Variables:

  • gameX, gameY — coordinates in the game's internal resolution (e.g., from game design docs or layout)
  • GAME_WIDTH, GAME_HEIGHT — the game's declared resolution (e.g., 1920, 1080 for a 1920×1080 game)
  • cssX, cssY — CSS page coordinates passed to Playwright

This formula is verified working for Phaser 3 in FIT scale mode at 1920×1080.

Note — scale mode matters: In FIT mode the canvas CSS box scales to the viewport while GAME_WIDTH/GAME_HEIGHT stay constant, so this formula is exact. In RESIZE mode the game's internal resolution changes to match the viewport; use canvas.width and canvas.height read via page.evaluate() for GAME_WIDTH/GAME_HEIGHT instead of hardcoded constants. Other modes (NONE, ENVELOP) may require similar adjustments.


3. runPlaywrightCode Usage Pattern

runPlaywrightCode executes arbitrary Playwright code against the current browser page — the correct tool for all canvas interaction.

Full worked example — click a game object at game-space position (960, 540) in a 1920×1080 game:

// Wait for canvas to be rendered and sized
await page.waitForFunction(() => {
  const c = document.querySelector("canvas");
  return c !== null && c.getBoundingClientRect().width > 0;
});

const rect = await page.evaluate(() => {
  const c = document.querySelector("canvas");
  const r = c.getBoundingClientRect();
  return { left: r.left, top: r.top, width: r.width, height: r.height };
});

const GAME_WIDTH = 1920;
const GAME_HEIGHT = 1080;
const gameX = 960;
const gameY = 540;

const cssX = rect.left + (gameX / GAME_WIDTH) * rect.width;
const cssY = rect.top + (gameY / GAME_HEIGHT) * rect.height;

await page.mouse.click(cssX, cssY);

Also useful via runPlaywrightCode:

  • await page.mouse.move(cssX, cssY) — hover without clicking
  • await page.keyboard.press('Space') — send key events to the game
  • await page.evaluate(() => window.gameState) — read exposed JS globals for state assertions

4. screenshotPage — Works for Canvas Verification

screenshotPage does work on canvas content. It uses VS Code's internal browserViewModel.captureScreenshot() — not Playwright's .screenshot() method. This internal capture reads the rendered frame buffer directly, so canvas pixels are captured correctly.

Use screenshotPage to:

  • Verify game state visually after an interaction
  • Confirm UI transitions or animations completed
  • Capture evidence of pass/fail for test reports

Screenshot output: write captured images to .tmp/issue-{N}-{name}.png per skills/terminal-hygiene/SKILL.md ## Scratch & Temp-File Hygiene — never pass a C:\... absolute path to a Bash redirect.

Source: screenshotBrowserTool.ts


5. readPage — Returns Empty for Canvas-Only Pages

readPage calls playwrightService.getSummary() which returns a DOM accessibility/semantic snapshot. Canvas elements render no accessible child nodes — the entire visual scene is opaque to the accessibility tree.

Result: readPage returns an empty or near-empty response for canvas-only applications.

Do not use readPage to:

  • Detect game objects or UI state in a canvas game
  • Assert text labels rendered inside the canvas
  • Navigate or introspect canvas content

Use runPlaywrightCode with page.evaluate() to read game state from exposed JS APIs, or use screenshotPage for visual verification.

Source: readBrowserTool.ts


VS Code Source Citations

All behaviors above are verified from VS Code 1.110 source code:

Tool Source File
click_element / clickElement clickBrowserTool.ts
screenshot_page / screenshotPage screenshotBrowserTool.ts
read_page / readPage readBrowserTool.ts
run_playwright_code / runPlaywrightCode VS Code native Playwright code execution (no single tool source file — functionality available when workbench.browser.enableChatTools: true)

Gotchas

Trigger Gotcha Fix
Using clickElement to interact with canvas game objects clickElement is selector-based only — no coordinate parameter exists; canvas internals are not addressable via selector Use runPlaywrightCode with page.mouse.click(cssX, cssY) and the canvas coordinate formula
Querying getBoundingClientRect() before the canvas has rendered Returns {0, 0, 0, 0} silently; click lands at top-left corner of page Add await page.waitForFunction(() => document.querySelector('canvas')?.width > 0) before querying the rect
Hardcoding GAME_WIDTH/GAME_HEIGHT in RESIZE scale mode FIT formula doesn't apply; clicks land at wrong position Read canvas.width/canvas.height via page.evaluate() rather than using known design dimensions
Using document.querySelector('canvas') when multiple canvases exist Always returns the first canvas — may be HUD overlay, not game scene Use a specific selector (#game canvas) or verify canvas count first
Using readPage on canvas-only pages Returns empty — canvas pixels are not DOM text Use screenshotPage for visual state verification instead

04 同仓库更多 Skills

Grimblaz
Grimblaz/agent-orchestra

Playwright 端到端测试

webapp-testing

面向 Web 应用的 Playwright 端到端测试指南,包含实用模式和设置步骤。在创建或改进基于浏览器的 E2E 覆盖率、测试稳定性和 CI 执行时使用。不要用于:React 组件测试(使用 ui-testing)、Canvas 游戏交互(使用 browser-canvas-testing)、TDD 工作流(使用 test-driven-development)、失败调试(使用 systematic-debugging)或随机化属性测试(使用 property-based-testing)。

测试工程师测试与质量保障
★ 3
Grimblaz
Grimblaz/agent-orchestra

任务完成前验证

verification-before-completion

在将工作标记为完成前使用的基于证据的验证清单。适用于提交 PR、发布、将工单标记为完成,或任何「我已完成」的声明之前;也适用于为差异测试套件的主张指定基线提交、设计变异测试活动或红灯运行证据、做出「不存在」的主张、普遍性主张,或将一次性演示作为长期保证提供时、归因某项测量结果或声称某次扫描或验证循环已完成时,以及陈述某项检查或 CI 套件将如何处理某个变更时。不要用于:合并后清理或归档(使用 post-pr-review),或处理 GitHub 审查意见(使用 code-review-intake)。

测试工程师成果验证与价值评估
★ 3
Grimblaz
Grimblaz/agent-orchestra

验证与审查方法论

validation-methodology

用于分阶段验证、故障分诊和追查深度设置的可复用验证与审查方法论。在运行验证阶梯、对失败进行分诊或执行对抗性审查时使用。不要用于:CE Gate 编排、专家派发的职责归属或步骤执行流程(这些保留在 Code-Conductor 中)。

测试工程师成果验证与价值评估
★ 3
Grimblaz
Grimblaz/agent-orchestra

上游智能体接手协议

upstream-onboarding

供上游智能体(Experience-Owner、Solution-Designer、Issue-Planner)与 Code-Conductor 在针对现有 GitHub issue 被调用时使用的共享开场阶段协议。它会生成适配规模的上下文简报,并在每个阶段边界对承接的工作执行标准检查。当用户可调用的智能体收到引用现有 GitHub issue 的请求、且必须在开场阶段加载 solution-authoring 与 upstream-onboarding 时使用。不要用于:子智能体派发(其已在经评估的会话上下文中运行)、合并后审查(使用 post-pr-review)、研究类或非树形子智能体。

AI 工程师智能体开发
★ 3
Grimblaz
Grimblaz/agent-orchestra

React 组件测试策略

ui-testing

以用户行为为核心的弹性 React 组件测试策略。适用于编写或审阅组件级 React 测试、修复不稳定的组件测试,或建立 React 测试模式。不适用于:Playwright E2E 测试(使用 webapp-testing)、canvas 游戏交互(使用 browser-canvas-testing)、TDD 工作流(使用 test-driven-development)、失败调试(使用 systematic-debugging)、UI 组件设计(使用 frontend-design),或随机属性测试(使用 property-based-testing)。

测试工程师测试与质量保障
★ 3
Grimblaz
Grimblaz/agent-orchestra

UI 界面视觉迭代优化

ui-iteration

可复用的截图驱动 UI 打磨工作流,用于迭代式视觉优化。适用于通过反复审阅与调整来改进布局、层级、间距、反馈或基于 Tailwind 的呈现效果。不适用于:功能性 Bug 修复、仅针对无障碍的审计,或重大重设计策略工作(使用 Code-Smith 或 frontend-design)。

界面设计师前端开发与界面实现
★ 3