Android
Added in: v1.9Playwright 对 Android 自动化提供实验性支持。这包括 Chrome for Android 和 Android WebView。
要求
- Android 设备或 AVD 模拟器。
- ADB 守护进程正在运行并已在你的设备上认证。通常运行
adb devices就足够了。 - 设备上安装了
Chrome 105或更新版本 - 在
chrome://flags中启用了"Enable command line on non-rooted devices"。
已知限制
- 尚不支持原始 USB 操作,所以你需要 ADB。
- 设备需要保持唤醒状态才能生成截图。启用"保持唤醒"开发者模式会有所帮助。
- 我们没有在设备上运行所有测试,所以并非所有功能都能正常工作。
如何运行
Android 自动化脚本的示例如下:
const { _android: android } = require('playwright');
(async () => {
// 连接到设备。
const [device] = await android.devices();
console.log(`型号: ${device.model()}`);
console.log(`序列号: ${device.serial()}`);
// 截取整个设备的屏幕截图。
await device.screenshot({ path: 'device.png' });
{
// --------------------- WebView -----------------------
// 启动一个带有 WebView 的应用程序。
await device.shell('am force-stop org.chromium.webview_shell');
await device.shell('am start org.chromium.webview_shell/.WebViewBrowserActivity');
// 获取 WebView。
const webview = await device.webView({ pkg: 'org.chromium.webview_shell' });
// 填写输入框。
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');
// 像往常一样操作 WebView 的页面。
const page = await webview.page();
await page.waitForURL(/.*microsoft\/playwright.*/);
console.log(await page.title());
}
{
// --------------------- 浏览器 -----------------------
// 启动 Chrome 浏览器。
await device.shell('am force-stop com.android.chrome');
const context = await device.launchBrowser();
// 像往常一样使用 BrowserContext。
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();
}
// 关闭设备。
await device.close();
})();方法
android.connect()
Added in: v1.28 async此方法将 Playwright 附加到现有的 Android 设备。 使用 android.launchServer() 启动新的 Android 服务器实例。
参数
要连接的浏览器 websocket 端点。
返回值
Promise<AndroidDevice>
android.devices()
Added in: v1.9 async返回检测到的 Android 设备列表。
参数
返回值
Promise<Array<AndroidDevice>>
android.launchServer()
Added in: v1.28 async启动客户端可以连接的 Playwright Android 服务器。请参见以下示例:
用法
服务器端:
const { _android } = require('playwright');
(async () => {
const browserServer = await _android.launchServer({
// 如果你连接了多个设备并想使用特定的一个。
// 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.org/');
console.log(await page.evaluate(() => window.location.href));
await page.screenshot({ path: 'page-chrome-1.png' });
await context.close();
})();参数
建立 ADB 服务器连接的可选主机。默认为
127.0.0.1。建立 ADB 服务器连接的可选端口。默认为
5037。deviceSerialNumberstring(可选)用于启动浏览器的可选设备序列号。如果未指定,当连接多个设备时将抛出错误。
hoststring(可选) Added in: v1.45用于 web socket 的主机。这是可选的,默认为
localhost,仅接受来自回环接口的连接。传递显式地址(例如0.0.0.0)以接受来自网络的连接 - 请注意,这会将设备 RPC 暴露给任何可以到达监听端口的对象。omitDriverInstallboolean(可选)阻止在附加时自动安装 playwright 驱动程序。假设驱动程序已经安装。
用于 web socket 的端口。默认为 0,即选择任何可用端口。
提供 Android 服务器的路径。出于安全原因,默认为不可猜测的字符串。
WARNING
任何知道
wsPath的进程或网页(包括在 Playwright 中运行的)都可以控制操作系统用户。因此,使用此选项时应使用不可猜测的令牌。
返回值
Promise<BrowserServer>
android.setDefaultTimeout()
Added in: v1.9此设置将更改所有接受 timeout 选项的方法的默认最大时间。
参数