0Pricing
Jetpack Compose Academy · 课时

语义与测试标签

让用户界面易于测试并支持辅助功能。

语义与测试标签 是 CoddyKit 上的免费 Jetpack Compose Academy 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Jetpack Compose Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Jetpack Compose Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

The Semantics Tree

Compose builds a semantics tree next to the visual one. It describes what each element means, and it is exactly what both tests and screen readers read.

Why Semantics Matter

If a node carries no meaning in the tree, finders cannot reach it and TalkBack cannot announce it. Good semantics make your app both testable and accessible. ♿

Content Descriptions Add Meaning

Icons and images carry no text, so give them a contentDescription. It feeds the semantics tree, letting tests find them and screen readers describe them.

Icon(Icons.Default.Add,
    contentDescription = "Add item")

When Text Is Not Unique

Sometimes many nodes share the same text and a text finder gets confused. You need a stable handle that does not depend on the visible label.

Introducing testTag

A testTag is an invisible identifier you attach with a Modifier. It gives a node a unique name for tests without affecting how it looks.

Box(Modifier.testTag("avatar"))

Finding by Tag

Once tagged, locate the node with onNodeWithTag. This is rock-solid even when text changes across languages or states.

composeTestRule.onNodeWithTag("avatar")
    .assertIsDisplayed()

Quick Check

Several buttons share the label Done, so a text finder is ambiguous. What gives one button a unique, invisible handle for tests?

Tags vs Descriptions

Keep them separate: contentDescription serves real users, while testTag exists only for tests. Do not overload one to do the other's job.

The semantics Modifier

For full control, the semantics modifier lets you set custom properties, like marking a node as a heading for accessibility tools.

Modifier.semantics { heading() }

Merging Descendants

Compose can merge a parent and its children into one node with mergeDescendants. That makes a whole row announce as a single, friendly unit.

Modifier.semantics(mergeDescendants = true) {}

Print the Tree to Debug

Stuck on a missing node? Call printToLog on the root to dump the full semantics tree, then read exactly what is available to match.

rule.onRoot().printToLog("TREE")

Design for Testability

Adding clear descriptions and tags as you build means your tests almost write themselves. Accessible apps and testable apps grow from the same habits.

Recap: Semantics & Tags

The semantics tree powers both testing and accessibility. Use contentDescription for users and testTag for tests to keep your UI easy to verify. 🌳

常见问题解答

「语义与测试标签」课时是免费的吗?

是的 — 「语义与测试标签」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Jetpack Compose Academy 课程的其余内容,请升级到 CoddyKit PRO。 Jetpack Compose Academy 课程共包含 4 节课。

「语义与测试标签」这节课中我会学到什么?

让用户界面易于测试并支持辅助功能。 你通过在浏览器中直接运行的动手代码来练习 Jetpack Compose Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Jetpack Compose Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Jetpack Compose Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「语义与测试标签」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Jetpack Compose Academy 课中编写并运行代码吗?

能。每节 Jetpack Compose Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. createComposeRule 与查找器
  2. 断言与执行操作
  3. 语义与测试标签
  4. 测试 ViewModel 与 Flow
← 返回 Jetpack Compose Academy