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此方法将 Playwright 连接到现有的 Android 设备。请使用 android.launchServer() 来启动一个新的 Android 服务器实例。
用法
await android.connect(endpoint);
await android.connect(endpoint, options);
参数
-
要连接的浏览器 websocket 端点。
-
optionsObject (可选)
返回
devices
添加于:v1.9返回检测到的 Android 设备列表。
用法
await android.devices();
await android.devices(options);
参数
optionsObject (可选)
返回
launchServer
新增于: v1.28启动 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();
})();
参数
optionsObject (可选)-
建立 ADB 服务器连接的可选主机。默认为
127.0.0.1。 -
建立 ADB 服务器连接的可选端口。默认为
5037。 -
deviceSerialNumberstring (可选)#用于启动浏览器的可选设备序列号。如果未指定,当连接了多个设备时会报错。
-
用于 web socket 的主机地址。它是可选的,如果省略,服务器将在可用时接受非指定的 IPv6 地址 (::) 上的连接,否则在非指定的 IPv4 地址 (0.0.0.0) 上接受连接。建议通过选择特定接口来加强安全性。
-
omitDriverInstallboolean (可选)#防止在连接时自动安装 Playwright 驱动程序。假设驱动程序已预先安装。
-
用于 web socket 的端口。默认为 0,即自动选择任何可用端口。
-
提供 Android 服务器服务的路径。出于安全考虑,默认值为一个不可猜测的字符串。
警告任何知晓
wsPath的进程或网页(包括在 Playwright 中运行的进程或网页)都可以控制操作系统用户。因此,使用此选项时,应使用不可猜测的令牌。
-
返回
setDefaultTimeout
添加于:v1.9此设置将更改所有接受 timeout 选项的方法的默认最大超时时间。
用法
android.setDefaultTimeout(timeout);
参数