メインコンテンツにスキップ

Android

Playwrightは、Androidの自動化に実験的なサポートを提供しています。これには、Android版ChromeとAndroid WebViewが含まれます。

要件

  • AndroidデバイスまたはAVDエミュレーター。
  • ADBデーモンが実行されており、デバイスで認証されていること。通常、adb devicesを実行するだけで十分です。
  • Chrome 87以降がデバイスにインストールされていること
  • 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();
})();

メソッド

接続

追加バージョン: v1.28 android.connect

このメソッドは、Playwrightを既存のAndroidデバイスにアタッチします。android.launchServer()を使用して、新しいAndroidサーバーインスタンスを起動します。

使用法

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

引数

  • wsEndpoint 文字列#

    接続するブラウザのWebSocketエンドポイント。

  • options Object (optional)

    • headers オブジェクト<文字列, 文字列> (オプション)#

      Webソケット接続リクエストと共に送信される追加のHTTPヘッダー。オプション。

    • slowMo 数値 (オプション)#

      指定されたミリ秒数だけPlaywrightの操作を遅くします。何が起こっているかを確認するのに便利です。デフォルトは0です。

    • timeout 数値 (オプション)#

      接続が確立されるまで待機する最大時間(ミリ秒単位)。デフォルトは30000(30秒)です。タイムアウトを無効にするには0を渡します。

戻り値


デバイス

追加バージョン: v1.9 android.devices

検出されたAndroidデバイスのリストを返します。

使用法

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

引数

  • options Object (optional)
    • host 文字列 (オプション)追加: v1.22#

      ADBサーバー接続を確立するためのオプションのホスト。デフォルトは127.0.0.1です。

    • omitDriverInstall 真偽値 (オプション)追加バージョン: v1.21#

      アタッチ時のPlaywrightドライバーの自動インストールを防ぎます。ドライバーがすでにインストールされていることを前提とします。

    • port 数値 (オプション)追加バージョン: 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.org/');
console.log(await page.evaluate(() => window.location.href));
await page.screenshot({ path: 'page-chrome-1.png' });

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

引数

  • options Object (optional)
    • adbHost 文字列 (オプション)#

      ADBサーバー接続を確立するためのオプションのホスト。デフォルトは127.0.0.1です。

    • adbPort 数値 (オプション)#

      ADBサーバー接続を確立するためのオプションのポート。デフォルトは5037です。

    • deviceSerialNumber 文字列 (オプション)#

      ブラウザを起動するためのオプションのデバイスシリアル番号。指定しない場合、複数のデバイスが接続されていると例外をスローします。

    • host 文字列 (オプション)追加されたバージョン: v1.45#

      WebSocketに使用するホスト。オプションであり、省略された場合、IPv6が利用可能な場合は指定されていないIPv6アドレス(::)で、そうでない場合は指定されていないIPv4アドレス(0.0.0.0)で接続を受け入れます。特定のアドレスを選択してセキュリティを強化することを検討してください。

    • omitDriverInstall 真偽値 (オプション)#

      アタッチ時のPlaywrightドライバーの自動インストールを防ぎます。ドライバーがすでにインストールされていることを前提とします。

    • port 数値 (オプション)#

      WebSocketに使用するポート。デフォルトは0で、利用可能な任意のポートを選択します。

    • wsPath 文字列 (オプション)#

      Androidサーバーを提供するパス。セキュリティのため、これは推測不可能な文字列にデフォルト設定されます。

      警告

      wsPathを知っているプロセスまたはウェブページ(Playwrightで実行されているものを含む)は、OSユーザーを制御できます。このため、このオプションを使用する場合は、推測不可能なトークンを使用する必要があります。

戻り値


setDefaultTimeout

追加バージョン: v1.9 android.setDefaultTimeout

この設定は、timeoutオプションを受け入れるすべてのメソッドのデフォルトの最大時間を変更します。

使用法

android.setDefaultTimeout(timeout);

引数

  • timeout 数値#

    ミリ秒単位の最大時間