0Pricing
Jetpack Compose Academy · Pelajaran

Assertion & Menjalankan Tindakan

Verifikasi state dan simulasikan input pengguna.

Assertion & Menjalankan Tindakan adalah pelajaran Jetpack Compose Academy gratis di CoddyKit. Ini adalah pelajaran 2 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Jetpack Compose Academy, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Jetpack Compose Academy mencakup 4 pelajaran total.

Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.

Find, Then Verify

Finding a node is only half the job. To prove your UI behaves, you add an assertion that states what should be true about that node. ✅

assertIsDisplayed

The most common assertion is assertIsDisplayed. It confirms the node is actually visible to the user, not just present in the tree.

composeTestRule.onNodeWithText("Welcome")
    .assertIsDisplayed()

assertExists vs assertIsDisplayed

Watch the difference: assertExists checks the node is in the tree, while assertIsDisplayed also checks it is on screen and not clipped away.

composeTestRule.onNodeWithText("Footer")
    .assertExists()

Chaining Assertions

You can chain several checks on one node. Each returns the node again, so the test reads like a clear sentence about your UI state.

rule.onNodeWithText("Save")
    .assertIsDisplayed()
    .assertIsEnabled()

Performing a Click

To simulate a tap, call performClick on a found node. Compose dispatches the same click your real users would trigger.

composeTestRule.onNodeWithText("Add")
    .performClick()

Typing Text

For input fields, performTextInput types characters into the focused field, letting you test forms exactly as a user would fill them.

rule.onNodeWithText("Email")
    .performTextInput("a@b.com")

Act, Then Assert

The classic flow is act then assert: perform a click, then verify the new state appeared. This mirrors how a person uses your app.

rule.onNodeWithText("+").performClick()
rule.onNodeWithText("Count: 1")
    .assertIsDisplayed()

Asserting on State Flags

Beyond visibility, you can check toggles. assertIsOn and assertIsOff verify switches and checkboxes hold the value you expect.

rule.onNodeWithText("Notifications")
    .assertIsOff()

Quick Check

You want to confirm a button is genuinely on screen, not merely present in the tree. Which assertion do you use?

Assert on Count

When you grab a group with onAllNodes, verify how many matched using assertCountEquals. It is perfect for checking list sizes.

rule.onAllNodesWithText("Item")
    .assertCountEquals(3)

Clear Before You Type

Replacing text? Call performTextClearance first, then performTextInput, so old characters do not stick around and break your check.

rule.onNodeWithText("Name")
    .performTextClearance()

Readable, Trustworthy Tests

Good assertions describe behavior, not pixels. Aim for tests that read like requirements, so a failure instantly tells you what behavior broke.

Recap: Actions & Assertions

You drive the UI with actions like performClick and performTextInput, then prove the result with assertions like assertIsDisplayed. That is the heart of UI testing. 🎯

Pertanyaan yang Sering Diajukan

Apakah pelajaran “Assertion & Menjalankan Tindakan” gratis?

Ya — teks lengkap “Assertion & Menjalankan Tindakan” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Jetpack Compose Academy, upgrade ke CoddyKit PRO. Kursus Jetpack Compose Academy mencakup 4 pelajaran total.

Apa yang akan aku pelajari di “Assertion & Menjalankan Tindakan”?

Verifikasi state dan simulasikan input pengguna. Kamu berlatih Jetpack Compose Academy dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.

Apakah aku perlu pengalaman untuk memulai Jetpack Compose Academy?

Tidak diperlukan pengalaman sebelumnya. Jetpack Compose Academy di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 2 dari 4.

Berapa lama pelajaran “Assertion & Menjalankan Tindakan” memakan waktu?

Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.

Bisakah aku menulis dan menjalankan kode dalam pelajaran Jetpack Compose Academy ini?

Ya. Setiap pelajaran Jetpack Compose Academy menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.

Semua pelajaran dalam kursus ini

  1. createComposeRule & Pencari
  2. Assertion & Menjalankan Tindakan
  3. Semantik & Tag Test
  4. Menguji ViewModels & Flow
← Kembali ke Jetpack Compose Academy