跳至主要内容

安卓

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

要求

  • Android设备或AVD模拟器。
  • ADB守护进程正在运行并与您的设备完成认证。通常只需运行adb devices命令即可。
  • Chrome 87 或更新版本已安装在设备上
  • chrome://flags中启用"在非root设备上启用命令行"选项。

已知限制

  • 目前还不支持原始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();
})();

方法

连接

Added in: v1.28 android.connect

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

用法

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

参数

  • wsEndpoint string#

    用于连接的浏览器websocket端点。

  • options Object (可选)

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

      用于WebSocket连接请求的额外HTTP头信息。可选。

    • slowMo number (可选)#

      将Playwright操作速度减慢指定的毫秒数。这有助于您观察操作过程。默认为0

    • timeout number (可选)#

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

返回


设备

Added in: v1.9 android.devices

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

用法

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

参数

  • options Object (optional)
    • host string (可选) 添加于: v1.22#

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

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

      防止在附加时自动安装playwright驱动程序。假设驱动程序已安装。

    • port number (可选) 添加于: v1.20#

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

返回


launchServer

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

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

参数

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

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

    • adbPort number (可选)#

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

    • deviceSerialNumber string (可选)#

      可选设备序列号,用于指定在哪个设备上启动浏览器。如果未指定,当连接多个设备时会抛出错误。

    • host string (可选) 添加于: v1.45#

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

    • omitDriverInstall boolean (optional)#

      防止在附加时自动安装playwright驱动程序。假设驱动程序已经安装完成。

    • port number (可选)#

      用于WebSocket的端口。默认为0,表示自动选择任何可用端口。

    • wsPath string (可选)#

      Android Server的服务路径。出于安全考虑,默认使用一个不可预测的字符串。

      警告

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

返回


setDefaultTimeout

Added in: v1.9 android.setDefaultTimeout

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

用法

android.setDefaultTimeout(timeout);

参数

  • timeout number#

    最大时间(毫秒)