This guide walks you through setting up and running Playwright tests on real Android devices, enabling you to automate UI tests for your mobile web apps or hybrid apps directly on physical hardware. You’ll learn how to connect devices, configure Playwright, and execute tests, gaining confidence that your app works in real-world conditions.
What You’ll Need
- A computer with Node.js (v16 or later) and npm installed.
- A real Android device (phone or tablet) with USB debugging enabled.
- USB cable to connect the device to your computer.
- Basic knowledge of JavaScript/TypeScript and Playwright.
- Android SDK Platform Tools (adb) installed on your computer.
Step-by-Step
- Install Playwright and required packages: In your project directory, run
npm init -ythennpm install @playwright/test. Also install the Playwright Android support:npm install playwright-core(or use the fullplaywrightpackage). - Enable USB debugging on your Android device: Go to Settings > About phone and tap Build number seven times to unlock developer options. Then go to Settings > System > Developer options and enable USB debugging.
- Connect your device via USB: Plug your Android device into your computer. On the device, accept the “Allow USB debugging?” prompt. Verify the connection by running
adb devicesin your terminal. Your device should appear as “device” (not “unauthorized”). - Create a Playwright test file: Create a file named
android.spec.js(or .ts) in your project. Start with a basic test that launches the browser on your device. - Write a test that connects to the device: Use Playwright’s Android API. Here’s a minimal example:
const { _android } = require('playwright'); (async () => { const device = await _android.launch(); console.log('Device connected:', device.model()); await device.shell('am start -a android.intent.action.VIEW -d https://example.com'); await device.close(); })();This launches Playwright’s Android driver, connects to your device, and opens a URL in the default browser.
- Run the test: Execute the script with
node android.spec.js. If successful, you’ll see the device model printed, and the browser should open on your phone. - Use Playwright’s browser automation on Android: For full Playwright automation (like clicking elements, filling forms), you need to use the Chrome or WebView on the device. Playwright can launch a Chromium instance on the device. Here’s an example:
const { _android } = require('playwright'); (async () => { const device = await _android.launch(); const context = await device.launchBrowser({ browserName: 'chromium' }); const page = await context.newPage(); await page.goto('https://example.com'); await page.screenshot({ path: 'android.png' }); await context.close(); await device.close(); })();This launches a full Chromium browser on your device, allowing you to use all Playwright APIs.
- Integrate with Playwright Test Runner: To use the test runner, create a
playwright.config.jsfile with a custom project that uses the Android device. You can set a global setup to connect to the device and pass it to tests via fixtures. - Run your test suite: Use
npx playwright testto execute your tests. Ensure your device remains connected and unlocked during the run.
Common Mistakes
- Device not detected: Often due to missing USB drivers or not accepting the debugging prompt. Check
adb devicesand ensure the device shows as “device”. - Using the wrong browser: Playwright’s Android support currently works best with Chromium. Trying to use WebKit or Firefox may fail.
- Forgetting to unlock the device: Tests may fail if the screen is locked. Set your device to stay awake and disable lock screen during testing.
- Not closing the context/device: Leaving the browser open can cause resource leaks and flaky tests. Always close the context and device in a
finallyblock or useafterAll.
Tips
- Use a dedicated device for testing to avoid interruptions from calls or notifications.
- Keep your device plugged in and set to never sleep during tests.
- If you need to test on multiple devices, you can run tests in parallel by launching multiple devices.
- Use the
device.shell()method to interact with the device’s OS, like setting permissions or clearing app data. - Take screenshots at key steps to debug failures visually.
Troubleshooting
- “Error: No devices found”: Ensure USB debugging is enabled and your device is authorized. Run
adb kill-serverthenadb start-serverand reconnect. - “Error: Device is offline”: Disconnect and reconnect the USB cable, or restart adb. Sometimes switching USB ports helps.
- Playwright fails to launch Chromium on device: Make sure the device has enough free storage and that Chrome is installed (or Playwright will download it). Try clearing the app cache for Chrome.
- Tests are slow: Real devices are slower than emulators. Increase timeouts in your Playwright config, and avoid unnecessary waits.
- WebView vs. Chrome: If you’re testing a hybrid app, you may need to target the WebView. Use
device.launchBrowser({ browserName: 'chromium', channel: 'webview' })to automate the WebView instead of Chrome.
Frequently Asked Questions
Can I run Playwright tests on iOS devices?
No, Playwright’s Android support is specific to Android. For iOS, you’d need a different tool like Appium or XCUITest.
Do I need to root my Android device?
No, rooting is not required. USB debugging is sufficient.
Can I use Playwright to test native Android apps?
No, Playwright is designed for web content. For native apps, consider Appium or Espresso.
How do I run tests on multiple devices simultaneously?
You can launch multiple devices in your script and run tests in parallel, but each device needs to be connected via USB or over Wi-Fi (using adb over TCP/IP).
What if my device is not recognized by adb?
Install the correct USB driver for your device manufacturer, or try using a different USB cable/port. Also, ensure you’ve accepted the RSA key prompt on the device.
Can I use Playwright on Android without a USB connection?
Yes, you can connect via adb over Wi-Fi. Run adb tcpip 5555 on the device, then adb connect . Then Playwright will find the device.
Related
General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.
More in this bed
- Custom Alphanumeric Passcode iPhone: How to Set
- HomePod Software Update: How to Install
- How to Shuffle Photos in Slideshow on iPhone
- Quick Look Not Working on Mac? Fixes That Work
- How to See Deleted WhatsApp Messages on iPhone
- cat, head, or tail: Which Linux Command to Use