无障碍功能检查
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
通过测试无障碍功能,您可以从以下角度体验应用:
整个用户群,包括具有无障碍功能需求的用户。这种形式的
可以为您发掘改进应用功能的机会。
本页介绍了如何向现有的 Espresso 添加无障碍功能检查
测试。如需详细了解无障碍功能,请参阅无障碍功能
指南。
启用检查
您可以使用 AccessibilityChecks
类启用和配置无障碍功能测试:
Kotlin
import androidx.test.espresso.accessibility.AccessibilityChecks
@RunWith(AndroidJUnit4::class)
@LargeTest
class MyWelcomeWorkflowIntegrationTest {
init {
AccessibilityChecks.enable()
}
}
Java
import androidx.test.espresso.accessibility.AccessibilityChecks;
@RunWith(AndroidJUnit4.class)
@LargeTest
public class MyWelcomeWorkflowIntegrationTest {
@BeforeClass
public void enableAccessibilityChecks() {
AccessibilityChecks.enable();
}
}
默认情况下,当您执行 ViewActions
中定义的任何视图操作时,系统都会运行检查。每次检查都包括执行操作所在的视图以及所有后代视图。您可以在每次检查期间评估屏幕的整个视图层次结构,方法是将 true
传入 setRunChecksFromRootView()
,如以下代码段所示:
Kotlin
AccessibilityChecks.enable().setRunChecksFromRootView(true)
Java
AccessibilityChecks.enable().setRunChecksFromRootView(true);
抑制结果的子集
Espresso 针对应用运行无障碍功能检查后,您可能会发现一些有助于改进应用的无障碍功能,但无法马上进行处理的结果。为了防止 Espresso 测试因这些结果而不断失败,您可以暂时忽略它们。无障碍功能测试框架 (ATF) 使用 setSuppressingResultMatcher()
方法提供此功能,借此指示 Espresso 抑制满足给定匹配器表达式的所有结果。
如果您对应用所做的更改可以改进无障碍功能的一个方面,让 Espresso 尽可能多地显示无障碍功能其他方面的结果会有好处。因此,最好仅抑制已知的具体改进机会。
如果您暂时抑制无障碍功能测试的某些结果,打算以后再处理,切勿意外抑制类似的结果。因此,请使用作用域较小的匹配器。为此,选择的匹配器应确保只有在给定的结果满足以下每项无障碍功能检查的条件时,Espresso 才会抑制该结果:
- 某种类型的无障碍功能检查,如用于检查触摸目标大小的无障碍功能检查。
- 用于评估特定界面元素(如按钮)的无障碍功能检查。
ATF 定义了多个匹配器,以帮助您定义要在 Espresso 测试中显示的结果。以下示例会抑制与单个 TextView
元素的色彩对比度相关的检查结果。元素的 ID 为 countTV
。
Kotlin
AccessibilityChecks.enable().apply {
setSuppressingResultMatcher(
allOf(
matchesCheck(TextContrastCheck::class.java),
matchesViews(withId(R.id.countTV))
)
)
}
Java
AccessibilityValidator myChecksValidator =
AccessibilityChecks.enable()
.setSuppressingResultMatcher(
allOf(
matchesCheck(TextContrastCheck.class),
matchesViews(withId(R.id.countTV))));
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Accessibility checking\n\nTesting for accessibility lets you experience your app from the perspective of\nyour entire user base, including users with accessibility needs. This form of\ntesting can reveal opportunities to make your app more powerful and versatile.\n\nThis page describes how to add accessibility checks to your existing Espresso\ntests. For more information about accessibility, see the [Accessibility\nguides](/guide/topics/ui/accessibility).\n\nEnable checks\n-------------\n\nYou can enable and configure accessibility testing using the\n[`AccessibilityChecks`](/reference/androidx/test/espresso/accessibility/AccessibilityChecks)\nclass: \n\n### Kotlin\n\n```kotlin\nimport androidx.test.espresso.accessibility.AccessibilityChecks\n\n@RunWith(AndroidJUnit4::class)\n@LargeTest\nclass MyWelcomeWorkflowIntegrationTest {\n init {\n AccessibilityChecks.enable()\n }\n}\n```\n\n### Java\n\n```java\nimport androidx.test.espresso.accessibility.AccessibilityChecks;\n\n@RunWith(AndroidJUnit4.class)\n@LargeTest\npublic class MyWelcomeWorkflowIntegrationTest {\n @BeforeClass\n public void enableAccessibilityChecks() {\n AccessibilityChecks.enable();\n }\n}\n```\n\nBy default, the checks run when you perform any view action defined in\n[`ViewActions`](/reference/androidx/test/espresso/action/ViewActions). Each\ncheck includes the view on which the action is performed as well as all\ndescendant views. You can evaluate the entire view hierarchy of a screen during\neach check by passing `true` into\n[`setRunChecksFromRootView()`](https://github.com/google/Accessibility-Test-Framework-for-Android/blob/a6117fe0059c82dd764fa628d3817d724570f69e/src/main/java/com/google/android/apps/common/testing/accessibility/framework/integrations/espresso/AccessibilityValidator.java#L82),\nas shown in the following code snippet: \n\n### Kotlin\n\n```kotlin\nAccessibilityChecks.enable().setRunChecksFromRootView(true)\n```\n\n### Java\n\n```java\nAccessibilityChecks.enable().setRunChecksFromRootView(true);\n```\n\nSuppress subsets of results\n---------------------------\n\nAfter Espresso runs accessibility checks on your app, you might find several\nopportunities to improve your app's accessibility that you cannot address\nimmediately. In order to stop Espresso tests from continually failing because\nof these results, you can ignore them temporarily. The Accessibility Test\nFramework (ATF) provides this functionality using the\n[`setSuppressingResultMatcher()`](https://github.com/google/Accessibility-Test-Framework-for-Android/blob/a6117fe0059c82dd764fa628d3817d724570f69e/src/main/java/com/google/android/apps/common/testing/accessibility/framework/integrations/espresso/AccessibilityValidator.java#L95)\nmethod, which instructs Espresso to suppress all results that satisfy the given\nmatcher expression.\n\nWhen you make changes to your app that address one aspect of accessibility, it's\nbeneficial for Espresso to show results for as many other aspects of\naccessibility as possible. For this reason, it's best to suppress only specific\nknown opportunities for improvement.\n\nWhen you temporarily suppress accessibility test findings that you plan to\naddress later, it's important to not accidentally suppress similar findings. For\nthis reason, use matchers that are narrowly scoped. To do so, choose a\n[matcher](http://hamcrest.org/JavaHamcrest/tutorial#a-tour-of-common-matchers)\nso that Espresso suppresses a given result only if it satisfies **each** of the\nfollowing accessibility checks:\n\n1. Accessibility checks of a certain type, such as those that check for touch target size.\n2. Accessibility checks that evaluate a particular UI element, such as a button.\n\nThe [ATF defines several matchers](https://github.com/google/Accessibility-Test-Framework-for-Android/blob/a6117fe0059c82dd764fa628d3817d724570f69e/src/main/java/com/google/android/apps/common/testing/accessibility/framework/AccessibilityCheckResultUtils.java)\nto help you define which results to show in your Espresso tests. The following\nexample suppresses the results of checks that relate to a single `TextView`\nelement's color contrast. The element's ID is `countTV`. \n\n### Kotlin\n\n```kotlin\nAccessibilityChecks.enable().apply {\n setSuppressingResultMatcher(\n allOf(\n matchesCheck(TextContrastCheck::class.java),\n matchesViews(withId(R.id.countTV))\n )\n )\n}\n```\n\n### Java\n\n```java\nAccessibilityValidator myChecksValidator =\n AccessibilityChecks.enable()\n .setSuppressingResultMatcher(\n allOf(\n matchesCheck(TextContrastCheck.class),\n matchesViews(withId(R.id.countTV))));\n```"]]