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