2021-02-11 10:31:57 -08:00
# class: Android
* langs: js
2021-08-06 14:02:41 -07:00
Playwright has **experimental** support for Android automation. This includes Chrome for Android and Android WebView.
2021-02-11 10:31:57 -08:00
2021-08-05 21:12:34 -07:00
*Requirements*
* Android device or AVD Emulator.
* [ADB daemon ](https://developer.android.com/studio/command-line/adb ) running and authenticated with your device. Typically running `adb devices` is all you need to do.
* [`Chrome 87` ](https://play.google.com/store/apps/details?id=com.android.chrome ) or 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*
2021-02-11 10:31:57 -08:00
An example of the Android automation script would be:
```js
2021-03-24 04:20:41 +08:00
const { _android: android } = require('playwright');
2021-02-11 10:31:57 -08:00
(async () => {
// Connect to the device.
2021-03-24 04:20:41 +08:00
const [device] = await android.devices();
2021-02-11 10:31:57 -08:00
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();
2021-03-24 04:20:41 +08:00
await page.waitForNavigation({ url: /.*microsoft\/playwright.*/ });
2021-02-11 10:31:57 -08:00
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();
})();
```
Note that since you don't need Playwright to install web browsers when testing Android, you can omit browser download via setting the following environment variable when installing Playwright:
2021-06-02 09:23:06 -07:00
```bash js
2021-05-11 20:47:48 +02:00
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1 npm i -D playwright
2021-02-11 10:31:57 -08:00
```
## async method: Android.devices
- returns: < [Array]< [AndroidDevice]>>
Returns the list of detected Android devices.
## method: Android.setDefaultTimeout
This setting will change the default maximum time for all the methods accepting [`param: timeout` ] option.
### param: Android.setDefaultTimeout.timeout
- `timeout` < [float]>
Maximum time in milliseconds