Implementation:Microsoft Playwright Client Android
| Knowledge Sources | |
|---|---|
| Domains | Browser Automation, Mobile Testing |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Concrete tool for client-side Android device automation provided by the Playwright library.
Description
The `Android` class and its companion `AndroidDevice` class provide the client-side API for automating Android devices through Playwright. The `Android` class extends `ChannelOwner` and implements the public `api.Android` interface, managing device discovery and timeout settings. `AndroidDevice` provides methods for interacting with Android devices including input gestures (tap, swipe, drag, fling, pinch), taking screenshots, launching browsers, and managing WebViews. Communication with the server is handled through Playwright's channel-based RPC protocol.
Usage
Use these classes when automating Android device interactions from a Playwright client, including device discovery via `android.devices()`, performing touch gestures, launching Chrome browser contexts on the device, and connecting to WebViews for web content automation.
Code Reference
Source Location
- Repository: Microsoft_Playwright
- File: packages/playwright-core/src/client/android.ts
Signature
export class Android extends ChannelOwner<channels.AndroidChannel> implements api.Android {
_playwright!: Playwright;
readonly _timeoutSettings: TimeoutSettings;
_serverLauncher?: AndroidServerLauncherImpl;
constructor(parent: ChannelOwner, type: string, guid: string, initializer: channels.AndroidInitializer);
setDefaultTimeout(timeout: number): void;
async devices(options?: { port?: number }): Promise<AndroidDevice[]>;
}
export class AndroidDevice extends ChannelOwner<channels.AndroidDeviceChannel> implements api.AndroidDevice {
constructor(parent: ChannelOwner, type: string, guid: string, initializer: channels.AndroidDeviceInitializer);
async tap(selector: api.AndroidSelector, options?: types.TimeoutOptions): Promise<void>;
async swipe(selector: api.AndroidSelector, direction: Direction, percent: number, options?: SpeedOptions & types.TimeoutOptions): Promise<void>;
async launchBrowser(options?: types.BrowserContextOptions): Promise<BrowserContext>;
async screenshot(options?: { path?: string }): Promise<Buffer>;
async close(): Promise<void>;
}
Import
import { Android, AndroidDevice } from './client/android';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| parent | ChannelOwner | Yes | Parent channel owner in the object hierarchy |
| type | string | Yes | The type identifier for this channel object |
| guid | string | Yes | Globally unique identifier for this object |
| initializer | channels.AndroidInitializer | Yes | Initialization data from the server |
| options.port | number | No | ADB server port for device discovery |
Outputs
| Name | Type | Description |
|---|---|---|
| devices | AndroidDevice[] | List of discovered Android devices |
| BrowserContext | BrowserContext | Browser context launched on the Android device |
| screenshot | Buffer | Screenshot image data from the device |
Usage Examples
const { _android: android } = require('playwright');
// Discover devices
const devices = await android.devices();
const device = devices[0];
// Perform gestures
await device.tap({ res: 'com.example:id/button' });
await device.swipe({ res: 'com.example:id/list' }, 'up', 50);
// Launch browser on device
const context = await device.launchBrowser();
const page = await context.newPage();
await page.goto('https://example.com');
// Take screenshot
const screenshot = await device.screenshot({ path: 'device.png' });
await device.close();