Core Concepts
Accessibility Tree vs DOM: Visually hiding an element (e.g., CSS opacity: 0) behaves differently for screen readers than display: none or aria-hidden="true". The take_snapshot tool returns the accessibility tree of the page, which represents what assistive technologies "see", making it the most reliable source of truth for semantic structure.
Reading web.dev documentation: If you need to research specific accessibility guidelines (like https://web.dev/articles/accessible-tap-targets), you can append .md.txt to the URL (e.g., https://web.dev/articles/accessible-tap-targets.md.txt) to fetch the clean, raw markdown version. This is much easier to read!
Workflow Patterns
1. Automated Audit (Lighthouse)
Start by running a Lighthouse accessibility audit to get a comprehensive baseline. This tool provides a high-level score and lists specific failing elements with remediation advice.
- 1. Run the audit:
- Set
mode to
"navigation" to refresh the page and capture load issues.
- Set
outputDirPath (e.g.,
/tmp/lh-report) to save the full JSON report.
- 2. Analyze the Summary:
- Check
scores (0-1 scale). A score < 1 indicates violations.
- Review
audits.failed count.
- 3. Review the Report (CRITICAL):
-
Parsing: Do not read the entire file line-by-line. Use a CLI tool like
jq or a Node.js one-liner to filter for failures:
# Extract failing audits with their details
node -e "const r=require('./report.json'); Object.values(r.audits).filter(a=>a.score!==null && a.score<1).forEach(a=>console.log(JSON.stringify({id:a.id, title:a.title, items:a.details?.items})))"
- This efficiently extracts the
selector and
snippet of failing elements without loading the full report into context.
2. Browser Issues & Audits
Chrome automatically checks for common accessibility problems. Use list_console_messages to check for these native audits:
- -
types: INLINECODE18 - INLINECODE19 :
true (to catch issues that occurred during page load)
This often reveals missing labels, invalid ARIA attributes, and other critical errors without manual investigation.
3. Semantics & Structure
The accessibility tree exposes the heading hierarchy and semantic landmarks.
- 1. Navigate to the page.
- Use
take_snapshot to capture the accessibility tree. - Check Heading Levels: Ensure heading levels (
h1, h2, h3, etc.) are logical and do not skip levels. The snapshot will include heading roles. - Content Reordering: Verify that the DOM order (which drives the accessibility tree) matches the visual reading order. Use
take_screenshot to inspect the visual layout and compare it against the snapshot structure to catch CSS floats or absolute positioning that jumbles the logical flow.
4. Labels, Forms & Text Alternatives
- 1. Locate buttons, inputs, and images in the
take_snapshot output. - Ensure interactive elements have an accessible name (e.g., a button should not just say
"" if it only contains an icon). - Orphaned Inputs: Verify that all form inputs have associated labels. Use
evaluate_script with the "Find Orphaned Form Inputs" snippet found in references/a11y-snippets.md. - Check images for
alt text.
5. Focus & Keyboard Navigation
Testing "keyboard traps" and proper focus management without visual feedback relies on tracking the focused element.
- 1. Use the
press_key tool with "Tab" or "Shift+Tab" to move focus. - Use
take_snapshot to capture the updated accessibility tree. - Locate the element marked as focused in the snapshot to verify focus moved to the expected interactive element.
- If a modal opens, focus must move into the modal and "trap" within it until closed.
6. Tap Targets and Visuals
According to web.dev, tap targets should be at least 48x48 pixels with sufficient spacing. Since the accessibility tree doesn't show sizes, use evaluate_script with the "Measure Tap Target Size" snippet found in references/a11y-snippets.md.
Pass the element's uid from the snapshot as an argument to evaluate_script.
7. Color Contrast
To verify color contrast ratios, start by checking for native accessibility issues:
- 1. Call
list_console_messages with types: ["issue"]. - Look for "Low Contrast" issues in the output.
If native audits do not report issues (which may happen in some headless environments) or if you need to check a specific element manually, use evaluate_script with the "Check Color Contrast" snippet found in references/a11y-snippets.md.
8. Global Page Checks
Verify document-level accessibility settings often missed in component testing using the "Global Page Checks" snippet found in references/a11y-snippets.md.
Troubleshooting
If standard a11y queries fail or the evaluate_script snippets return unexpected results:
- - Visual Inspection: If automated scripts cannot determine contrast (e.g., text over gradient images or complex backgrounds), use
take_screenshot to capture the element. While models cannot measure exact contrast ratios from images, they can visually assess legibility and identify obvious issues.
核心概念
无障碍树 vs DOM:对屏幕阅读器而言,通过CSS opacity: 0等方式在视觉上隐藏元素,与使用display: none或aria-hidden=true的表现截然不同。take_snapshot工具返回页面的无障碍树,它代表了辅助技术看到的内容,是语义结构最可靠的信息来源。
阅读web.dev文档:如需研究特定无障碍指南(如https://web.dev/articles/accessible-tap-targets),可在URL后添加.md.txt(例如https://web.dev/articles/accessible-tap-targets.md.txt)获取纯净的原始Markdown版本,阅读体验更佳!
工作流程模式
1. 自动化审计(Lighthouse)
首先运行Lighthouse无障碍审计,获取全面的基线数据。该工具提供总体评分,并列出具体失败元素及修复建议。
- 1. 运行审计:
- 将mode设置为navigation以刷新页面并捕获加载问题。
- 设置outputDirPath(如/tmp/lh-report)保存完整JSON报告。
- 2. 分析摘要:
- 检查scores(0-1分制)。分数<1表示存在违规。
- 查看audits.failed计数。
- 3. 审查报告(关键步骤):
-
解析:不要逐行读取整个文件。使用jq等CLI工具或Node.js单行命令筛选失败项:
bash
# 提取失败审计项及其详情
node -e const r=require(./report.json); Object.values(r.audits).filter(a=>a.score!==null && a.score<1).forEach(a=>console.log(JSON.stringify({id:a.id, title:a.title, items:a.details?.items})))
- 此方法高效提取失败元素的selector和snippet,无需将完整报告加载到上下文中。
2. 浏览器问题与审计
Chrome会自动检查常见无障碍问题。使用listconsolemessages查看这些原生审计结果:
- - types:[issue]
- includePreservedMessages:true(捕获页面加载期间出现的问题)
此方法无需手动调查即可发现缺失标签、无效ARIA属性等关键错误。
3. 语义与结构
无障碍树展示了标题层级和语义地标。
- 1. 导航至目标页面。
- 使用takesnapshot捕获无障碍树。
- 检查标题层级:确保标题级别(h1、h2、h3等)逻辑合理且不跳级。快照会包含标题角色。
- 内容重排:验证DOM顺序(驱动无障碍树)与视觉阅读顺序一致。使用takescreenshot检查视觉布局,并与快照结构对比,发现CSS浮动或绝对定位导致的逻辑流混乱。
4. 标签、表单与文本替代
- 1. 在takesnapshot输出中定位按钮、输入框和图片。
- 确保交互元素具有无障碍名称(例如,仅包含图标的按钮不应显示为)。
- 孤立输入框:使用references/a11y-snippets.md中的查找孤立表单输入框代码片段,通过evaluate_script验证所有表单输入框是否有关联标签。
- 检查图片的alt文本。
5. 焦点与键盘导航
测试键盘陷阱和正确的焦点管理,需在无视觉反馈的情况下追踪焦点元素。
- 1. 使用presskey工具配合Tab或Shift+Tab移动焦点。
- 使用takesnapshot捕获更新后的无障碍树。
- 在快照中定位标记为焦点的元素,验证焦点是否移动到预期的交互元素。
- 如果模态框打开,焦点必须移入模态框并在其中循环,直到关闭。
6. 点击目标与视觉效果
根据web.dev指南,点击目标应至少为48x48像素且间距充足。由于无障碍树不显示尺寸,请使用references/a11y-snippets.md中的测量点击目标尺寸代码片段,通过evaluatescript执行。
将快照中元素的uid作为参数传递给evaluatescript。_
7. 色彩对比度
验证色彩对比度时,首先检查原生无障碍问题:
- 1. 调用listconsolemessages,设置types: [issue]。
- 在输出中查找低对比度问题。
如果原生审计未报告问题(某些无头环境中可能发生),或需要手动检查特定元素,请使用references/a11y-snippets.md中的检查色彩对比度代码片段,通过evaluatescript执行。
8. 全局页面检查
使用references/a11y-snippets.md中的全局页面检查代码片段,验证组件测试中常被忽略的文档级无障碍设置。
故障排除
如果标准无障碍查询失败,或evaluate_script代码片段返回意外结果:
- - 视觉检查:当自动化脚本无法确定对比度时(例如渐变图像或复杂背景上的文字),使用take_screenshot捕获元素。虽然模型无法从图像中精确测量对比度比率,但可以直观评估可读性并识别明显问题。