跳转到主要内容

Android

Playwright 目前提供对 Android 自动化的实验性支持。这包括 Android 版 Chrome 和 Android WebView。

要求

  • Android 设备或 AVD 模拟器。
  • ADB 守护进程 正在运行并已通过设备授权。通常只需运行 adb devices 即可。
  • 设备上安装了 Chrome 87 或更新版本
  • chrome://flags 中启用了“Enable command line on non-rooted devices”(在非 root 设备上启用命令行)。

已知限制

  • 尚不支持原始 USB 操作,因此您需要 ADB。
  • 设备必须处于唤醒状态才能截屏。启用开发者模式中的“保持唤醒”(Stay awake)功能会有所帮助。
  • 我们并未在设备上运行所有测试,因此并非所有功能都能完美运行。

如何运行

一个 Android 自动化脚本示例如下

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.ac.cn/');
console.log(await page.evaluate(() => window.location.href));
await page.screenshot({ path: 'page.png' });

await context.close();
}

// Close the device.
await device.close();
})();

方法

connect

新增于: v1.28 android.connect

此方法将 Playwright 连接到现有的 Android 设备。请使用 android.launchServer() 来启动一个新的 Android 服务器实例。

用法

await android.connect(endpoint);
await android.connect(endpoint, options);

参数

  • endpoint string#

    要连接的浏览器 websocket 端点。

  • options Object (可选)

    • headers Object<string, string> (可选)#

      要随 WebSocket 连接请求发送的额外 HTTP 头。可选。

    • slowMo number (可选)#

      将 Playwright 操作减慢指定的毫秒数。有助于观察正在发生的事情。默认为 0

    • timeout number (可选)#

      等待连接建立的最大时间(毫秒)。默认为 30000(30 秒)。传递 0 禁用超时。

返回


devices

添加于:v1.9 android.devices

返回检测到的 Android 设备列表。

用法

await android.devices();
await android.devices(options);

参数

  • options Object (可选)
    • host string (可选)新增于: v1.22#

      建立 ADB 服务器连接的可选主机。默认为 127.0.0.1

    • omitDriverInstall boolean (可选)添加于:v1.21#

      防止在连接时自动安装 Playwright 驱动程序。假设驱动程序已预先安装。

    • port number (可选)新增于: v1.20#

      建立 ADB 服务器连接的可选端口。默认为 5037

返回


launchServer

新增于: v1.28 android.launchServer

启动 Playwright Android 服务器,供客户端连接。参见以下示例

用法

服务端

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);
})();

客户端

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.ac.cn/');
console.log(await page.evaluate(() => window.location.href));
await page.screenshot({ path: 'page-chrome-1.png' });

await context.close();
})();

参数

  • options Object (可选)
    • adbHost string (可选)#

      建立 ADB 服务器连接的可选主机。默认为 127.0.0.1

    • adbPort number (可选)#

      建立 ADB 服务器连接的可选端口。默认为 5037

    • deviceSerialNumber string (可选)#

      用于启动浏览器的可选设备序列号。如果未指定,当连接了多个设备时会报错。

    • host string (可选)新增于: v1.45#

      用于 web socket 的主机地址。它是可选的,如果省略,服务器将在可用时接受非指定的 IPv6 地址 (::) 上的连接,否则在非指定的 IPv4 地址 (0.0.0.0) 上接受连接。建议通过选择特定接口来加强安全性。

    • omitDriverInstall boolean (可选)#

      防止在连接时自动安装 Playwright 驱动程序。假设驱动程序已预先安装。

    • port number (可选)#

      用于 web socket 的端口。默认为 0,即自动选择任何可用端口。

    • wsPath string (可选)#

      提供 Android 服务器服务的路径。出于安全考虑,默认值为一个不可猜测的字符串。

      警告

      任何知晓 wsPath 的进程或网页(包括在 Playwright 中运行的进程或网页)都可以控制操作系统用户。因此,使用此选项时,应使用不可猜测的令牌。

返回


setDefaultTimeout

添加于:v1.9 android.setDefaultTimeout

此设置将更改所有接受 timeout 选项的方法的默认最大超时时间。

用法

android.setDefaultTimeout(timeout);

参数

  • timeout number#

    最大超时时间(以毫秒为单位)