Android
Playwright has experimental support for Android automation. This includes Chrome for Android and Android WebView.
Requirements
- Android device or AVD Emulator.
- ADB daemon running and authenticated with your device. Typically running
adb devicesis all you need to do. Chrome 87or newer installed on the device- "Enable command line on non-rooted devices" enabled in
chrome://flags.
Known limitations
- Raw USB operation is not yet supported, so you need ADB.
- Device needs to be awake to produce screenshots. Enabling "Stay awake" developer mode will help.
- We didn't run all the tests against the device, so not everything works.
How to run
An example of the Android automation script would be:
const { _android: android } = require('playwright');
(async () => {
// Connect to the device.
const [device] = await android.devices();
console.log(`Model: ${device.model()}`);
console.log(`Serial: ${device.serial()}`);
// Take screenshot of the whole device.
await device.screenshot({ path: 'device.png' });
{
// --------------------- WebView -----------------------
// Launch an application with WebView.
await device.shell('am force-stop org.chromium.webview_shell');
await device.shell('am start org.chromium.webview_shell/.WebViewBrowserActivity');
// Get the WebView.
const webview = await device.webView({ pkg: 'org.chromium.webview_shell' });
// Fill the input box.
await device.fill({
res: 'org.chromium.webview_shell:id/url_field',
}, 'github.com/microsoft/playwright');
await device.press({
res: 'org.chromium.webview_shell:id/url_field',
}, 'Enter');
// Work with WebView's page as usual.
const page = await webview.page();
await page.waitForNavigation({ url: /.*microsoft\/playwright.*/ });
console.log(await page.title());
}
{
// --------------------- Browser -----------------------
// Launch Chrome browser.
await device.shell('am force-stop com.android.chrome');
const context = await device.launchBrowser();
// Use BrowserContext as usual.
const page = await context.newPage();
await page.goto('https://webkit.org/');
console.log(await page.evaluate(() => window.location.href));
await page.screenshot({ path: 'page.png' });
await context.close();
}
// Close the device.
await device.close();
})();
Methods
connect
Added in: v1.28This methods attaches Playwright to an existing Android device. Use android.launchServer() to launch a new Android server instance.
Usage
await android.connect(endpoint);
await android.connect(endpoint, options);
Arguments
-
A browser websocket endpoint to connect to.
-
optionsObject (optional)-
headersObject<string, string> (optional)#Additional HTTP headers to be sent with web socket connect request. Optional.
-
Slows down Playwright operations by the specified amount of milliseconds. Useful so that you can see what is going on. Defaults to
0. -
Maximum time in milliseconds to wait for the connection to be established. Defaults to
30000(30 seconds). Pass0to disable timeout.
-
Returns
devices
Added in: v1.9Returns the list of detected Android devices.
Usage
await android.devices();
await android.devices(options);
Arguments
optionsObject (optional)-
hoststring (optional) Added in: v1.22#Optional host to establish ADB server connection. Default to
127.0.0.1. -
omitDriverInstallboolean (optional) Added in: v1.21#Prevents automatic playwright driver installation on attach. Assumes that the drivers have been installed already.
-
portnumber (optional) Added in: v1.20#Optional port to establish ADB server connection. Default to
5037.
-
Returns
launchServer
Added in: v1.28Launches Playwright Android server that clients can connect to. See the following example:
Usage
Server Side:
const { _android } = require('playwright');
(async () => {
const browserServer = await _android.launchServer({
// If you have multiple devices connected and want to use a specific one.
// deviceSerialNumber: '<deviceSerialNumber>',
});
const wsEndpoint = browserServer.wsEndpoint(