This guide covers testing Android WebView using Playwright connected via Chrome DevTools Protocol.
- App Launch: The CanIAndroidWebView app is launched on an Android device/emulator
- WebView Debugging: The app exposes a Chrome DevTools Protocol endpoint
- Port Forwarding: ADB forwards the WebView debugging port to localhost
- Playwright Connection: Playwright connects to the WebView as if it were Chrome
- Test Execution: Tests run against web content loaded in the WebView
- Result Collection: Results are saved and can be uploaded to GitHub
┌─────────────────┐
│ Playwright │
│ Test Suite │
└────────┬────────┘
│ Chrome DevTools Protocol
↓
┌─────────────────┐
│ localhost │
│ :9222 │
└────────┬────────┘
│ ADB Port Forward
↓
┌─────────────────┐
│ Android Device │
│ WebView App │
│ (debugging on) │
└─────────────────┘
The app needs WebView debugging enabled. Add this to MainActivity.kt:
import android.webkit.WebView
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Enable WebView debugging for automation
WebView.setWebContentsDebuggingEnabled(true)
// ... rest of the code
}
}-
Enable Developer Options on your Android device
- Go to Settings → About phone
- Tap "Build number" 7 times
-
Enable USB Debugging
- Go to Settings → Developer options
- Enable "USB debugging"
-
Connect Device
adb devices # Should show your device as "device" (not "unauthorized")
While not strictly necessary for Chrome DevTools Protocol connection, Appium can help with app control:
npx appium# Install APK if not already installed
adb install -r apps/CanIAndroidWebView/app/build/outputs/apk/debug/app-debug.apk
# Launch the app
adb shell am start -n com.example.caniandroidwebview/.MainActivitynpm run test:androidimport { chromium } from '@playwright/test';
import { connectToWebView } from '../../src/android/webview-helper';
const { browser, context, page } = await connectToWebView(deviceId, packageName);
// Now you can use Playwright as normal
await page.goto('https://collector.openwebdocs.org/');
const title = await page.title();
console.log('Page title:', title);
await browser.close();// Test JavaScript APIs
const features = await page.evaluate(() => {
return {
localStorage: typeof localStorage !== 'undefined',
serviceWorker: 'serviceWorker' in navigator,
// ... more features
};
});# Connect Chrome DevTools
chrome://inspect/#devices
# Or use adb logcat
adb logcat | grep -i "chromium\|webview"adb shell cat /proc/net/unix | grep webview_devtools_remote# Forward port
adb forward tcp:9222 localabstract:webview_devtools_remote_XXXX
# Open in browser
open http://localhost:9222Solution: Ensure the app has WebView debugging enabled and is running with a WebView loaded.
Solution:
- Check
adb devicesshows your device - Revoke and re-grant USB debugging authorization
- Try
adb kill-server && adb start-server
Solution:
- Check if port 9222 is already in use:
lsof -i :9222 - Use a different port in your test configuration
const devices = await getConnectedDevices();
for (const deviceId of devices) {
// Run tests on each device
}Modify the app to accept intents with settings:
// In MainActivity.kt
val url = intent.getStringExtra("url") ?: "https://collector.openwebdocs.org/"
webView.loadUrl(url)Then launch with custom URL:
adb shell am start -n com.example.caniandroidwebview/.MainActivity \
--es url "https://your-test-site.com"See .github/workflows/android-tests.yml for GitHub Actions example.
- Run the test suite:
npm run test:android - View results:
test-results/android-results.json - Upload results:
npm run upload-results test-results/android-results.json