Technical
How to Set Up Automated Testing for an Android TWA
October 12, 2026 · 7 min read
A Trusted Web Activity (TWA) operates as a hybrid architecture. From the perspective of the Android operating system, it is a native application package (APK or AAB) containing a lightweight wrapper. From the perspective of the user, it is a high-performance, full-screen instance of Google Chrome or another compatible custom tab provider. This hybrid nature introduces unique challenges for testing. You cannot rely solely on standard web browser testing frameworks, nor can you rely purely on native Android automation tools. To verify that your converted progressive web app functions flawlessly when launched from the home screen, you must construct a test harness capable of crossing the bridge between native Android container controls and the underlying Chromium engine.
The Dual-Context Architecture of TWA Testing
When automated testing tools interact with a native Android application, they communicate via the Android Debug Bridge (ADB) using system-level testing drivers like UI Automator. These drivers are designed to locate native buttons, text inputs, and system dialogues. However, once a TWA launches, the application interface renders inside a web viewport powered by Chrome. Native drivers view this viewport as a single, opaque graphical element. They cannot inspect the inner HTML elements, input fields, or buttons inside your web page.
To solve this, testing frameworks must employ a dual-context strategy. The test run begins in the native context to verify app launch, splash screens, and native permission dialogues. It then switches to the web context, which establishes a debugging connection to Chrome via ChromeDriver. Once this context transition occurs, you can locate and interact with elements using standard CSS selectors or XPath queries, just as you would in a desktop web testing suite.
Testing Tool Compatibility
Choosing the correct testing framework is critical to establishing a reliable automated pipeline. Not all mobile testing frameworks support the web context switching necessary to inspect the interior of a TWA. The following table highlights the primary tools used for automating TWA tests and their respective capabilities.
| Testing Framework | Context Switching Support | Platform Focus | Best Suited For |
|---|---|---|---|
| Appium | Full Support | Cross-Platform Mobile | Comprehensive native and web automation pipelines |
| WebdriverIO | Full Support | Web and Mobile JS | JavaScript-centric projects targeting native apps |
| Playwright (Android) | Experimental Support | Web-First Automation | Quick verification of browser engine interactions |
| Espresso with Web Extension | Limited Support | Native Android (Java/Kotlin) | Low-level native developers with minor web elements |
Configuring Your Local Environment
To begin automating your TWA tests, you must establish an environment that allows your test runner to communicate with both the Android device and the browser engine. This requires three distinct components: a running Android emulator or physical device with USB debugging enabled, the Android SDK platform tools, and a matching version of ChromeDriver.
First, ensure that your test device has Google Chrome installed. The version of Google Chrome running on the Android device must match the major version of ChromeDriver installed on your test host machine. If you are using a standard emulator, you may need to configure an emulator image that includes Google APIs to ensure Chrome is present and up to date.
Verify that your device is connected and discoverable by executing the following command in your terminal:
adb devices
If the device is listed correctly, you must next ensure that your application under test has debugging enabled. In your Android project configuration, the WebappActivity or custom TWA launcher activity must allow debugging. While Google Chrome allows debugging of local pages by default on emulators, enabling native debugging helper tools ensures Appium can intercept the Chromium webview socket connection.
Setting Up Appium Capabilities for TWAs
Appium uses a JSON payload called Desired Capabilities to understand how to configure and launch your application under test. When targeting a TWA, you must provide the package name of your compiled Android application and the specific launcher activity that initiates the Trusted Web Activity lifecycle.
Below is a standard capability configuration for testing a TWA app:
{
"platformName": "Android",
"automationName": "UiAutomator2",
"deviceName": "Android_Emulator",
"appPackage": "com.example.twa.app",
"appActivity": "com.google.androidhotspot.twa.LauncherActivity",
"chromedriverExecutable": "/path/to/your/matching/chromedriver",
"noReset": true
}
In this configuration, the appPackage represents the unique identifier assigned during the conversion process, and appActivity points to the TWA launcher class. Specifying the exact path to the chromedriver executable prevents version mismatch issues during execution, which is the most common cause of test initialization failures.
Implementing the Context Switch in Code
Once your testing framework launches the application, it starts inside the native context (typically named NATIVE_APP). In this initial state, your test runner can only see native system components, such as the Android status bar, system permission prompts, or custom splash screens. To interact with your PWA content, you must explicitly program the test script to search for and transition to the web context.
This transition process involves querying the driver for all available contexts, identifying the context associated with Chrome or the TWA package, and instructing the driver to target that context. Here is an example implementation using WebdriverIO and Appium:
async function runTwaTest() {
let contexts = await driver.getContexts();
let webContext = contexts.find(context => context.includes("WEBVIEW") || context.includes("CHROMIUM"));
if (webContext) {
await driver.switchContext(webContext);
let loginButton = await driver.$("#login-submit");
await loginButton.click();
} else {
throw new Error("Web context not found inside the TWA container.");
}
}
Once the context switch is executed successfully, all subsequent driver commands interact directly with the DOM of your progressive web app. You can extract page titles, inspect storage states, set cookies, and invoke standard web interactions.
Verifying Digital Asset Links Integration
One of the most critical automated checks for a TWA is validating the correct deployment of Digital Asset Links. If the Digital Asset Links verification fails, the native wrapper displays the Chrome address bar at the top of the viewport. This compromises the native app presentation and can lead to rejection during Google Play store reviews.
You can use automated UI assertions to detect if the URL bar is visible. After launching the TWA and before switching contexts, check for the presence of native Android browser elements. If the native driver detects elements belonging to the package com.android.chrome with a resource ID matching the URL address bar, your asset validation is failing. Including this assertion in your test suite prevents regression issues when updating DNS or web servers.
Running Tests in CI/CD Pipelines
Integrating TWA testing into a Continuous Integration (CI) environment requires virtualisation of the Android environment. Because emulators require hardware acceleration (nested virtualisation), you must select a CI runner that supports GPU acceleration or use a cloud-based mobile device testing grid.
When running automated tests in cloud grids, ensure the grid provider supports testing hybrid applications. The pipeline should first build the signed APK or AAB, deploy it to the target device, execute the tests to verify launch, run your complete functional test suite within the web context, and then tear down the container. This ensures that changes to both the web application code and the native Android wrapper configuration are continually verified before shipping updates to the Google Play Store.
Ready to ship your Android app?
Paste your PWA URL, get a signed APK and a Google Play ready AAB in minutes.
Build my app