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このメソッドは、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 (オプション)#ブラウザを起動するオプションのデバイスシリアル番号。指定しない場合、複数のデバイスが接続されているとエラーが発生します。
-
host
string (オプション)追加バージョン: v1.45#Webソケットに使用するホスト。これはオプションであり、省略された場合、サーバーはIPv6が利用可能な場合は指定されていないIPv6アドレス(::)で、そうでない場合は指定されていないIPv4アドレス(0.0.0.0)で接続を受け入れます。特定のインターフェースを選択して強化することを検討してください。
-
omitDriverInstall
boolean (オプション)#アタッチ時のPlaywrightドライバの自動インストールを防ぎます。ドライバが既にインストールされていることを前提とします。
-
Webソケットに使用するポート。デフォルトは、利用可能な任意のポートを選択する
0
です。 -
Androidサーバーを提供するパス。セキュリティ上の理由から、デフォルトでは推測不能な文字列になります。
警告wsPath
を知っているプロセスやウェブページ(Playwright内で実行されているものを含む)は、OSユーザーの制御を奪う可能性があります。このため、このオプションを使用する際は推測できないトークンを使用すべきです。
-
戻り値
setDefaultTimeout
追加バージョン: v1.9この設定は、timeoutオプションを受け入れるすべてのメソッドのデフォルトの最大時間を変更します。
使用方法
android.setDefaultTimeout(timeout);
引数