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

Android

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

要件

  • AndroidデバイスまたはAVDエミュレーター。
  • ADBデーモンが実行中で、デバイスと認証されていること。通常、adb devicesを実行するだけで十分です。
  • デバイスにChrome 87以降がインストールされていること
  • chrome://flagsで「Enable command line on non-rooted devices」が有効になっていること。

既知の制限事項

  • 生の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 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を渡すとタイムアウトが無効になります。

戻り値


devices

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

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

使用方法

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

引数

  • options Object (オプション)
    • host string (オプション)追加バージョン: v1.22#

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

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

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

    • port number (オプション)追加バージョン: 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 (オプション)
    • adbHost string (オプション)#

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

    • adbPort number (オプション)#

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

    • deviceSerialNumber string (オプション)#

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

    • host string (オプション)追加バージョン: v1.45#

      Webソケットに使用するホスト。これはオプションであり、省略された場合、サーバーはIPv6が利用可能な場合は指定されていないIPv6アドレス(::)で、そうでない場合は指定されていないIPv4アドレス(0.0.0.0)で接続を受け入れます。特定のインターフェースを選択して強化することを検討してください。

    • omitDriverInstall boolean (オプション)#

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

    • port number (オプション)#

      Webソケットに使用するポート。デフォルトは、利用可能な任意のポートを選択する0です。

    • wsPath string (オプション)#

      Androidサーバーを提供するパス。セキュリティ上の理由から、デフォルトでは推測不能な文字列になります。

      警告

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

戻り値


setDefaultTimeout

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

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

使用方法

android.setDefaultTimeout(timeout);

引数

  • timeout number#

    最大時間(ミリ秒単位)