How to test Toast Message using Espresso?

On This Page How to Test Toast Message in Espresso

March 16, 2026 · 5 min read · Testing Guide

How to test Toast Message using Espresso?

Android Toast is a small message displayed on the screen, similar to a tooltip or similar popup notification. It is displayed on top of the main content of an activity and only continue visible for a little clip period.

Overview

What is testing Toast messages?

Testing Toast messages involves verifying that the correct message look when a user triggers an action. Since Toasts are fugacious and not tied to a specific UI factor, you need to use Espresso ’ s UIAutomator or early workarounds to capture and assert their presence.

Why test Toast messages?

Toast messages provide necessary feedback, such as success confirmation or error alerts. Toast messages must be displayed correctly to maintain a good user experience and prevent likely UI bugs in production.

This clause explain how to toast messages in Espresso and run the goner message exam in Espresso.

How to Test Toast Message in Espresso

Before you can start testing toast message in Espresso, firstly you need to create adecorView variable and assign it before trial. The DecorView is the view that actually holds the window ’ s background drawable.

Here ’ s how you can createdecorView:

private View decorView; @ Before public nihility setUp () {activityRule.getScenario () .onActivity (new ActivityScenario.ActivityAction & lt; YourActivity & gt; () {@ Override public nothingness perform (YourActivity activity) {decorView = activity.getWindow () .getDecorView ();}});}

Below is the Test causa with assertion that would test Toast Message.

onView (withText (`` R.id.ToastText '')) .inRoot (withDecorView (Matchers.not (decorView))) // Here you use decorView .check (lucifer (isDisplayed ()));

Combining decorView with the Test case with assertion written to test Toast Message.

individual View decorView; @ Before public nothingness frame-up () {activityRule.getScenario () .onActivity (new ActivityScenario.ActivityAction & lt; GardenActivity & gt; () {@ Override public emptiness perform (GardenActivity activity) {decorView = activity.getWindow () .getDecorView ();}});} //Test if the Toast Message is Displayed @ Test public void testValidToast () {// Given that no Plants are added to the user 's garden // When the `` Add Plant '' push is clicked (Yellow Button) onView (withId (R.id.add_plant)) .perform (click ()); //Verify Toast is Displayed After clicking the Plant List Fragment onView (withText (`` Welcome For The Visit '')) .inRoot (withDecorView (Matchers.not (decorView))) // Here we use decorView .check (matches (isDisplayed ()));} //Test if the Toast Message is not Displayed @ Test public void testToastNotDisplayed () {// Given that no Plants are added to the user 's garden // When the `` Add Plant '' button is clicked (Yellow Button) onView (withId (R.id.add_plant)) .perform (click ()); //Verify Toast is Displayed After clicking the Plant List Fragment onView (withText (`` Your Toast '')) .inRoot (withDecorView (Matchers.not (decorView))) // Here we use decorView .check (lucifer (not (isDisplayed ())));}

For official documentation of BrowserStack, visit.

Run Toast Message Test in Espresso

Create a test contour In Android Studio:

  • Unfastened Run carte & gt; & gt; Edit Configurations
  • Add a new Android Tests configuration
  • Choose a module
  • Add a specific instrumentation runner:
    android.support.test.runner.AndroidJUnitRunner
  • Add your category name

Once the configuration is setup connect your device or choose the emulator and immediately run the constellation.

Read More:

Executing Toast Message Test in Espresso with BrowserStack

For autonomous testing across multiple user personas, check out SUSATest — it explores your app like 10 different real users.

For more accurate results, it is recommended to test on real device, take into account.

With BrowserStack ’ s you can run Espresso Toast Message on real devices by follow the stairs below:

Step 1 To Run your examination cases on BrowserStack you need two apk files.

First one is yourmain appApk and another is yourandroidTest Apk.

To generate both the apk run below command

  • To create a.apkfile for the app run the following command in the terminal:
./gradlew assembleDebug
  • To make a.apkfile for the trial classes in the terminal run the next command:
./gradlew assembleAndroidTest

Find both .apkfile under your project folder:& lt; project-folder & gt; \build\outputs\apk. In the apk brochure you will find two files:

  • app-debug.apk
  • app-debug-androidTest.apk

These are the file that you will upload to the cloud when you create your Execution Plan.

Step 2Get your accession key from the BrowserStack using the below command

lock -u `` & lt; username & gt;: & lt; access-key & gt; '' -X POST '' https: //api-cloud.browserstack.com/app-automate/upload '' -F '' file= @ /path/to/app/file/Application-debug.apk ''

Output

Please mention the App URL (bs: // & lt; hashed appid & gt;) returned in the response of this call:

{`` app_url '': '' bs: // & lt; hashed appid & gt; ''}

Step 3Upload the app by running below command on your terminal

curl -u `` & lt; username & gt;: & lt; access-key & gt; '' -X POST '' https: //api-cloud.browserstack.com/app-automate/espresso/test-suite '' -F '' file= @ /path/to/test/file/Application-debug-test.apk ''

Output

Please observe the Test Suite URL (bs: // & lt; hashed testID & gt;) return in the response of this outcry:

{`` test_url '': '' bs: // & lt; hashed testID & gt; ''}

The Test ID returned in the response will be used to execute your test.

Note:Go through the for detailed information on Espresso

Execute Toast Message Espresso Test

To execute the test in Espresso, run the following command:

scroll -u `` & lt; username & gt;: & lt; access-key & gt; '' \ -X POST `` https: //api-cloud.browserstack.com/app-automate/espresso/v2/build '' \ -d ' {`` class '': [`` Test-Class-Name ''], `` devices '': [`` Samsung Galaxy S8-7.0 ''], `` app '': `` bs: // & lt; hashed appid from Step 1 & gt; '', `` testSuite '': `` bs: // & lt; hashed testID from Step 2 & gt; ''} ' \ -H `` Content-Type: application/json '' //Test-Class-Name for the above github project com.google.samples.apps.sunflower.ToastTest

The tryout results are available on the command-line interface, as well as the dashboard. You have now run your 1st.

Talk to an Expert

Conclusion

Testing Toast messages guarantee they appear with the right text and timing to improve app dependableness. Espresso helps verify that these message display as expect. Adding Toast message trial to your UI try scheme helps get issues early and enhance the user experience.

With BrowserStack ’ s existent gimmick cloud, you can run Espresso tests for Toast messages on real Android devices. This ensures accurate results across different OS versions, screen sizes, and device contour without managing physical devices.

Tags
98,000+ Views

# Ask-and-Contributeabout this topic with our Discord community.

Related Guides

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free