CalmPerson
CalmPerson

Reputation: 87

Android JetPack Compose Testing. How do I check if the Toast message appears?

I tried to do this but it doesn't work.

onNodeWithTag(TestTag.OPEN_DRAWER_BUTTON).performClick()
onNodeWithTag(TestTag.SYNCHRONIZE_BUTTON).performClick()
onNodeWithText(activity.getString(R.string.success_update)).assertExists()

I get it.

java.lang.AssertionError: Failed: assertExists.
Reason: Expected exactly '1' node but could not find any node that satisfies: (Text + EditableText contains 'Exchange rates have been updated!' (ignoreCase: false))

Drawer:

enter image description here

Upvotes: 4

Views: 1150

Answers (1)

joaquinVidal
joaquinVidal

Reputation: 11

Toasts don't appear in the tree of views, so there isn't a way to detect its existence from Compose UI Tests.

However, the recommended way for showing small messages to the user is Snackbar, which is rendered inside the view hierarchy. Therefore it can be detected from Compose UI Tests.

From Android Docs: "Note that Snackbars are preferred for brief messages while the app is in the foreground." "If your app is in the foreground, consider using a snackbar instead of using a toast. Snackbars include user-actionable options, which can provide a better app experience."

Upvotes: 1

Related Questions