Android
Playwright 对 Android 自动化提供实验性支持。这包括 Chrome for Android 和 Android WebView。
要求
- Android 设备或 AVD 模拟器。
- ADB daemon 正在运行并已通过您的设备进行身份验证。通常只需运行
adb devices
即可。 - 设备上安装了
Chrome 87
或更高版本 - "Enable command line on non-rooted devices" 在
chrome://flags
中启用。
已知限制
- 原始 USB 操作尚未支持,因此您需要 ADB。
- 设备需要保持唤醒状态才能生成截图。启用“保持唤醒”开发者模式会有帮助。
- 我们尚未在设备上运行所有测试,因此并非所有功能都有效。
如何运行
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.org/');
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(wsEndpoint);
await android.connect(wsEndpoint, options);
参数
-
要连接的浏览器 websocket 端点。
-
options
Object (可选)
返回值
devices
新增于: v1.9返回检测到的 Android 设备列表。
用法
await android.devices();
await android.devices(options);
参数
options
Object (可选)
返回值
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.org/');
console.log(await page.evaluate(() => window.location.href));
await page.screenshot({ path: 'page-chrome-1.png' });
await context.close();
})();
参数
options
Object (可选)-
建立 ADB 服务器连接的可选主机。默认为
127.0.0.1
。 -
建立 ADB 服务器连接的可选端口。默认为
5037
。 -
deviceSerialNumber
string (可选)#用于在其上启动浏览器的可选设备序列号。如果未指定,当连接多个设备时将抛出错误。
-
用于 web socket 的主机。它是可选的,如果省略,服务器将在 IPv6 可用时接受来自未指定 IPv6 地址 (::) 的连接,否则接受来自未指定 IPv4 地址 (0.0.0.0) 的连接。考虑通过选择特定接口来增强其安全性。
-
omitDriverInstall
boolean (可选)#阻止在附加时自动安装 playwright 驱动程序。假设驱动程序已安装。
-
用于 web socket 的端口。默认为 0,即选取任何可用端口。
-
提供 Android Server 的路径。出于安全考虑,默认为一个难以猜测的字符串。
警告任何知道
wsPath
的进程或网页(包括在 Playwright 中运行的)都可以控制操作系统用户。因此,使用此选项时应使用难以猜测的令牌。
-
返回值
setDefaultTimeout
新增于: v1.9此设置将更改接受 timeout 选项的所有方法的默认最大时间。
用法
android.setDefaultTimeout(timeout);
参数