Next-Gen App & Browser Testing Cloud
Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

On This Page
Learn Espresso for Android testing, including basics, API components, setup, running tests on real devices, advanced techniques, and best practices.

Yogendra Porwal
February 11, 2026
If you’re building Android apps and want your user interface to work exactly as expected every time, Espresso for Android automation testing is the go-to framework for developers. With Espresso, you can simulate user actions like tapping buttons or entering text and then verify that the app shows the expected UI state.
Overview
What Is Espresso Framework?
Espresso is a UI testing framework developed by Google for Android applications. It is used to write automated tests that interact with app views and verify UI behavior directly within the app process.
What Are the Espresso API Components?
Espresso provides a structured set of APIs to interact with UI elements, perform actions, and validate results during test execution.
How to Use Espresso for Android UI Testing?
Setting up Espresso involves configuring your Android project with the required tools, dependencies, and test runner before writing and executing UI tests.
Espresso is a UI testing framework, developed by Google, used in Android development to write automated tests that interact with views and verify screen behavior.
It is part of the AndroidX testing libraries and is typically used within Android Studio alongside Java or Kotlin test script.
Core Features:
For more details, check out this Espresso tutorial.
Component APIs of Espresso include onView() and onData() for starting UI interactions and global actions. ViewMatchers find UI elements, ViewActions perform user actions, and ViewAssertions verify view states after interactions.
Note: Run Espresso tests on real Android devices. Try TestMu AI Now!
You can set up Espresso by installing Android Studio, JDK, and an emulator or device. Add Espresso and JUnit dependencies, configure AndroidJUnitRunner, sync Gradle, then write and run UI tests.
Before starting with Espresso for Android automation testing, make sure the following prerequisites are in place:
In this Espresso Android tutorial, we are using the TestMu AI Android Proverbial app.
Step 1: Add Dependencies
Before writing your first Espresso test, you need to add the required dependencies to your project.
In your app’s build.gradle file, add the following:
dependencies {
androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
}Step 2: Configure Test Runner
Let’s add the test runner for instrumented tests in app/build.gradle file.
In defaultConfig, add the following:
android {
defaultConfig {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
}Step 3: Sync Project
Sync the Gradle project to ensure that all the configuration and new dependencies are downloaded. This can be done with a one click on the sync button at the top right of Android Studio. You can also use the Ctrl+Shift+O shortcut.
Now that we are done with all the configuration and prerequisites, let’s go ahead and write our first test with Espresso.
Test Scenario:
Implementation:
Create a MainActivityTest class at src/androidTest/java/<package-path>/.
@RunWith(AndroidJUnit4.class)
public class MainActivityTest {
@Rule
public ActivityScenarioRule<MainActivity> activityScenarioRule =
new ActivityScenarioRule<>(MainActivity.class);
@Test
public void checkTitleText() {
// validate title is displayed and has expected content
onView(withId(R.id.Textbox)).check(matches(isDisplayed()));
onView(withId(R.id.Textbox))
.check(matches(withText("Hello! Welcome to lambdatest Sample App called Proverbial")));
// click on Text button
onView(withId(R.id.Text)).perform(click());
onView(withId(R.id.Textbox)).check(matches(withText("Proverbial")));
}
}
Code Walkthrough:
Test Execution:
Ensure that an emulator or Android device is running and connected. You can check that by running the command below.
adb devicesIt should show the connected device or emulator.
Now, run the test via Android Studio or with the following command:
./gradlew connectedAndroidTestThis will launch the test on the default emulator connected to your project.
For Android UI testing, emulators are good for quick feedback during development, but real devices are needed to uncover issues tied to hardware, OS versions, and device-specific behavior.
Since Android runs across a wide range of devices with different screen sizes, system configurations, and manufacturers, testing on real devices helps catch issues that emulators may not expose.
Mobile app testing platforms such as TestMu AI provide access to a real device cloud of Android and iOS devices across multiple OS versions and device configurations. It supports automation testing frameworks such as Appium, Espresso, and XCUITest. You can perform Espresso automation testing online across a wide device range without maintaining local Android device labs.
To test Android apps online with Espresso on TestMu AI, start by signing up for a TestMu AI account or logging in to an existing one. Then follow the steps below:
TestMu AI APIs are used to upload and execute Espresso test suites, so an authentication token is required. You can generate the BASIC_AUTH_TOKEN using the TestMu AI Basic Authentication Header Generator. This token will be used in API requests for uploading APKs and triggering test execution.
Use Gradle to generate the required APK files:
./gradlew assembleDebug assembleAndroidTest
The above command will generate two APK files:
These files are typically located in the app/build/outputs/apk/ directory.
To run the test, we need to upload APKs to the real devices available on TestMu AI. This can be done using the upload framework API.
curl -u "YOUR_LAMBDATEST_USERNAME:YOUR_LAMBDATEST_ACCESS_KEY" --location --request POST "https://manual-api.lambdatest.com/app/uploadFramework" --form "appFile=@<PATH_OF_YOUR_ANDROID_APP>" --form "type=espresso-android"A successful upload returns the following response:
{
"app_id": "lt://APP123456789123456789",
"name": "apk_name.apk",
"type": "espresso-android",
"url": "url_to_uploaded_apk",
"custom_id": null,
"ios_keychain_enabled": "false"
}Use the same command to upload both the APKs generated in the previous step. Note down the app_id for both the Proverbial APK and the test APK. These app IDs will be used in the next step.
You can refer to all the available APIs at the TestMu AI App Automation API documentation.
With both APKs uploaded, trigger the test execution using the following API request:
curl --location --request POST "https://mobile-api.lambdatest.com/framework/v1/espresso/build" \
--header "Authorization: Basic BASIC_AUTH_TOKEN" \
--header "Content-Type: application/json" \
--data-raw '{
"app": "APP_ID",
"testSuite": "TEST_SUITE_ID",
"device": ["Pixel 6-12"],
"queueTimeout": 360,
"IdleTimeout": 150,
"deviceLog": true,
"network": false,
"build": "Proverbial-Espresso",
"geoLocation": "FR"
}'Ensure the correct app IDs are used:
Once the request is submitted, a successful response includes a buildId, which can be used to track execution.
To view the execution, go to the TestMu AI App Automation Dashboard. You will find the execution details, such as logs, screenshots, and video recordings, in the App Automation Dashboard.
Other than the core API components, Espresso also supports various advanced features to make your Android testing more reliable and maintainable.
Let’s have a look at a few of them.
For asynchronous work such as network requests or background data loading, Espresso needs to be informed when the app is busy and when it can safely continue executing test steps. Idling Resources act as a signaling mechanism that tells Espresso when an asynchronous operation starts and finishes.
Example:
IdlingResource idlingResource = new CountingIdlingResource("resource_name");
// register the idling resource before the test
@Before
public void registerIdlingResource() {
IdlingRegistry.getInstance().register(idlingResource);
}
@After
public void unregisterIdlingResource() {
if (idlingResource != null) {
IdlingRegistry.getInstance().unregister(idlingResource);
}
}This approach is useful when the app interacts with other apps or software services, allowing tests to focus on internal app behavior.
Example:
Intents.init();
intended(hasComponent(NewActivity.class.getName()));
Intents.release();With multiprocess support enabled, Espresso can coordinate interactions across processes while maintaining synchronization and test stability.
Example:
Log.d(TAG, "Checking main process name...");
onView(withId(R.id.textNamedProcess)).check(matches(withText("com.example.android.testing.espresso.multiprocesssample")));
Log.d(TAG, "Starting activity in a secondary process...");
onView(withId(R.id.startActivityBtn)).perform(click());It allows actions such as locating elements and performing user interactions. Espresso-Web relies on WebDriver Atoms to interact with WebView content.
Espresso-Web enables basic interaction and validation but is not a replacement for full browser automation. For testing web applications in depth, WebDriver-based frameworks are more suitable.
Example:
onWebView()
// find element with id specified
.withElement(findElement(Locator.ID, "text_input"))
// Clear previous input
.perform(clearElement())When working with Espresso, you may run into setup, sync, or test execution issues, so understanding common issues and their solutions helps keep Android UI tests stable and reliable.
WebView Not Detected by Espresso
Espresso Does Not Wait for the WebView to Load
Cannot Interact With WebView Elements Due to JavaScript Restrictions
No Way to Assert That a Web Element Does Not Exist
Dependency or Version Conflicts Break Espresso-Web
When working with Espresso, test failures or inconsistent behavior can occur for several reasons. The following tips help isolate and resolve common issues:
Espresso is built for Android apps and runs inside the app for fast, reliable UI tests. Appium supports Android and iOS, works externally, and is better for cross-platform and end-to-end testing.
| Aspect | Espresso | Appium |
|---|---|---|
| Platform support | Android only | Android, iOS, and mobile web |
| Execution model | Runs inside the app process | Runs outside the app using platform automation frameworks |
| Language support | Java, Kotlin | Java, Python, JavaScript, C#, and others |
| Setup complexity | Lower for Android projects | Higher due to cross-platform configuration |
| Test speed | Generally faster due to in-app execution | Slower compared to Espresso |
| Synchronization | Built-in synchronization with the UI thread | Requires explicit waits in many cases |
| Access to app internals | Direct access to Android UI components | No direct access to app internals |
| Best suited for | Android-only UI testing | Cross-platform and multi-language testing |
| WebView testing | Limited, basic WebView support | Strong support for mobile web and WebViews |
Espresso is a commonly used framework for automating UI tests in Android applications. Its tight integration with the Android toolchain and built-in synchronization make it suitable for validating UI behavior during development.
While emulators are useful for faster feedback, running tests on real devices is important for covering variations in hardware, OS versions, and system behavior. Real device platforms, such as TestMu AI, help run the same tests across multiple devices without maintaining physical infrastructure.
By following best practices and using Espresso’s advanced features where appropriate, teams can build UI tests that are easier to maintain and more consistent over time.
Did you find this page helpful?
TestMu AI forEnterprise
Get access to solutions built on Enterprise
grade security, privacy, & compliance