2021-02-11 10:31:57 -08:00
# class: Android
2022-07-05 16:24:50 -08:00
* since: v1.9
2021-02-11 10:31:57 -08:00
* 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.
2023-08-02 11:23:47 +02:00
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');
2021-02-11 10:31:57 -08:00
// 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();
})();
```
2022-10-24 17:23:11 -07:00
## async method: Android.connect
* since: v1.28
- returns: < [AndroidDevice]>
This methods attaches Playwright to an existing Android device.
Use [`method: Android.launchServer` ] to launch a new Android server instance.
### param: Android.connect.wsEndpoint
* since: v1.28
- `wsEndpoint` < [string]>
A browser websocket endpoint to connect to.
### option: Android.connect.headers
* since: v1.28
- `headers` < [Object]< [string], [string]>>
Additional HTTP headers to be sent with web socket connect request. Optional.
### option: Android.connect.slowMo
* since: v1.28
- `slowMo` < [float]>
Slows down Playwright operations by the specified amount of milliseconds. Useful so that you
can see what is going on. Defaults to `0` .
### option: Android.connect.timeout
* since: v1.28
- `timeout` < [float]>
Maximum time in milliseconds to wait for the connection to be established. Defaults to
`30000` (30 seconds). Pass `0` to disable timeout.
2021-02-11 10:31:57 -08:00
## async method: Android.devices
2022-07-05 16:24:50 -08:00
* since: v1.9
2021-02-11 10:31:57 -08:00
- returns: < [Array]< [AndroidDevice]>>
Returns the list of detected Android devices.
2022-04-21 22:16:42 +02:00
### option: Android.devices.host
2022-07-05 16:24:50 -08:00
* since: v1.22
2022-04-21 22:16:42 +02:00
- `host` < [string]>
Optional host to establish ADB server connection. Default to `127.0.0.1` .
2022-03-05 00:57:25 +05:30
### option: Android.devices.port
2022-07-05 16:24:50 -08:00
* since: v1.20
2022-03-05 00:57:25 +05:30
- `port` < [int]>
2022-04-21 22:16:42 +02:00
Optional port to establish ADB server connection. Default to `5037` .
2022-03-05 00:57:25 +05:30
2022-04-03 07:00:38 +08:00
### option: Android.devices.omitDriverInstall
2022-07-05 16:24:50 -08:00
* since: v1.21
2022-04-03 07:00:38 +08:00
- `omitDriverInstall` < [boolean]>
Prevents automatic playwright driver installation on attach. Assumes that the drivers have been installed already.
2022-10-24 17:23:11 -07:00
## async method: Android.launchServer
* since: v1.28
* langs: js
- returns: < [BrowserServer]>
Launches Playwright Android server that clients can connect to. See the following example:
2022-11-21 10:40:21 -08:00
**Usage**
2022-10-24 17:23:11 -07:00
Server Side:
```js
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();
console.log(wsEndpoint);
})();
```
Client Side:
```js
const { _android } = require('playwright');
(async () => {
const device = await _android.connect('< wsEndpoint > ');
console.log(device.model());
console.log(device.serial());
await device.shell('am force-stop com.android.chrome');
const context = await device.launchBrowser();
const page = await context.newPage();
await page.goto('https://webkit.org/');
console.log(await page.evaluate(() => window.location.href));
await page.screenshot({ path: 'page-chrome-1.png' });
await context.close();
})();
```
### option: Android.launchServer.adbHost
* since: v1.28
- `adbHost` < [string]>
Optional host to establish ADB server connection. Default to `127.0.0.1` .
### option: Android.launchServer.adbPort
* since: v1.28
- `adbPort` < [int]>
Optional port to establish ADB server connection. Default to `5037` .
### option: Android.launchServer.omitDriverInstall
* since: v1.28
- `omitDriverInstall` < [boolean]>
Prevents automatic playwright driver installation on attach. Assumes that the drivers have been installed already.
### option: Android.launchServer.deviceSerialNumber
* since: v1.28
- `deviceSerialNumber` < [string]>
Optional device serial number to launch the browser on. If not specified, it will
throw if multiple devices are connected.
2024-05-27 17:24:23 +08:00
### option: Android.launchServer.host
* since: v1.45
- `host` < [string]>
Host to use for the web socket. It is optional and if it is omitted, the server will accept connections on the unspecified IPv6 address (::) when IPv6 is available, or the unspecified IPv4 address (0.0.0.0) otherwise. Consider hardening it with picking a specific interface.
2022-10-24 17:23:11 -07:00
### option: Android.launchServer.port
* since: v1.28
- `port` < [int]>
Port to use for the web socket. Defaults to 0 that picks any available port.
### option: Android.launchServer.wsPath
* since: v1.28
- `wsPath` < [string]>
Path at which to serve the Android Server. For security, this defaults to an
unguessable string.
:::warning
Any process or web page (including those running in Playwright) with knowledge
of the `wsPath` can take control of the OS user. For this reason, you should
use an unguessable token when using this option.
:::
2021-02-11 10:31:57 -08:00
## method: Android.setDefaultTimeout
2022-07-05 16:24:50 -08:00
* since: v1.9
2021-02-11 10:31:57 -08:00
This setting will change the default maximum time for all the methods accepting [`param: timeout` ] option.
### param: Android.setDefaultTimeout.timeout
2022-07-05 16:24:50 -08:00
* since: v1.9
2021-02-11 10:31:57 -08:00
- `timeout` < [float]>
Maximum time in milliseconds