モバイル エミュレーション

Chrome DevTools でモバイル エミュレーション機能を有効にすることで、ユーザーがモバイル デバイス(「Nexus 7」タブレット、「iPhone 5」など)上で Chrome の PC 版から Chrome をエミュレートできます。この機能により、ウェブ開発が高速化され、デベロッパーは実際のデバイスがなくても、モバイル デバイスでウェブサイトがどのように表示されるかを簡単にテストできます。ChromeDriver では、「mobileEmulation」機能を使用してモバイル エミュレーションを有効にすることもできます。この機能を使用すると、辞書の値を指定できます。

DevTools のエミュレーション パネルと同様に、ChromeDriver でもモバイル エミュレーションを有効にするには、既知のデバイスを指定する方法と、個別のデバイス属性を指定する方法の 2 つの方法があります。辞書「mobileEmulation」の形式は、必要なメソッドによって異なります。

既知のモバイル デバイスの指定

特定のデバイス名でモバイル エミュレーションを有効にするには、「mobileEmulation」ディクショナリに「deviceName」が含まれている必要があります。「deviceName」の値として、DevTools のエミュレーション パネルの有効なデバイス名を使用します。

デバイスの設定画面のスクリーンショット

Java

Map<String, String> mobileEmulation = new HashMap<>();
mobileEmulation.put("deviceName", "Nexus 5");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setExperimentalOption("mobileEmulation", mobileEmulation);
WebDriver driver = new ChromeDriver(chromeOptions);

Ruby

mobile_emulation = { "deviceName" => "Nexus 5" }
caps = Selenium::WebDriver::Remote::Capabilities.chrome(
   "chromeOptions" => { "mobileEmulation" => mobile_emulation })
driver = Selenium::WebDriver.for :remote, url: 'http://localhost:4444/wd/hub',
desired_capabilities: caps

Python

from selenium import webdriver
mobile_emulation = { "deviceName": "Nexus 5" }
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)
driver = webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub',
desired_capabilities = chrome_options.to_capabilities())

個々のデバイス属性の指定

個々の属性を指定して、モバイル エミュレーションを有効にすることもできます。この方法でモバイル エミュレーションを有効にするには、「mobileEmulation」辞書に「deviceMetrics」と「clientHints」辞書、さらに「userAgent」文字列を含めます。次のデバイス指標を「deviceMetrics」ディクショナリで指定してください。

  • 「width」- デバイスの画面の幅(ピクセル単位)
  • "height" - デバイスの画面の高さ(ピクセル単位)
  • 「pixelRatio」 - デバイスのピクセル比
  • 「touch」 - タッチイベントをエミュレートするかどうか。値のデフォルトは true で、通常は省略できます。
  • 「mobile」- ブラウザをモバイル ユーザー エージェントとして動作させる必要があるかどうか(スクロールバーのオーバーレイ、向きイベントの出力、ビューポートに合わせたコンテンツの縮小など)。値のデフォルトは true で、通常は省略できます。

「clientHints」辞書には、次のエントリを含めることができます。

  • 「platform」はオペレーティング システムです。該当のプラットフォームで実行されている Chrome から返される値と完全に一致する既知の値(「Android」、「Chrome OS」、「Chromium OS」、「Fuchsia」、「Linux」、「macOS」、「Windows」)のいずれかを指定できます。または、ユーザー定義の値を指定することもできます。この値は必須です。
  • 「mobile」- ブラウザがモバイル リソース バージョンとデスクトップ リソース バージョンのどちらをリクエストするのか。通常、Android 搭載のスマートフォンで動作している Chrome では、この値は true に設定します。タブレットの Android デバイスの Chrome では、この値が false に設定されます。デスクトップ デバイスの Chrome でもこの値は false に設定されます。この情報を使用して、現実的なエミュレーションを指定できます。この値は必須です。
  • 残りのエントリは省略可能です。テストに関連する場合を除き、省略できます。
    • 「brands」- ブランドとメジャー バージョンのペアのリスト。省略した場合、ブラウザは独自の値を使用します。
    • "fullVersionList" - ブランドとバージョンのペアのリスト。これにより、ブラウザは独自の値を使用する必要がなくなりました。
    • "platformVersion" - OS のバージョン。デフォルトは空の文字列です。
    • "model" - デバイスモデル。デフォルトは空の文字列です。
    • 「architecture」- CPU アーキテクチャ。既知の値は「x86」と「arm」です。ユーザーは任意の文字列値を自由に指定できます。デフォルトは空の文字列です。
    • "bitness" - プラットフォームのビット数。既知の値は「32」と「64」です。ユーザーは任意の文字列値を自由に指定できます。デフォルトは空の文字列です。
    • 「wow64」- Windows 64 で Windows 32 をエミュレートします。デフォルトで false に設定されるブール値。

ChromeDriver では、「Android」、「Chrome OS」、「Chromium OS」、「Fuchsia」、「Linux」、「macOS」、「Windows」のプラットフォームの「clientHints」から「userAgent」値を推測できます。そのため、この値は省略できます。

「clientHints」辞書が省略されている場合(レガシー モード)、ChromeDriver では「userAgent」から「clientHints」を可能な限り推測します。ただし、「userAgent」値の形式が内部であいまいであるため、この機能は確実に動作しません。

モバイル エミュレーション パネルで入手できるスマートフォンやタブレットは、DevTools のソースコードにあります。

Java

Map<String, Object> deviceMetrics = new HashMap<>();
deviceMetrics.put("width", 360);
deviceMetrics.put("height", 640);
deviceMetrics.put("pixelRatio", 3.0);
Map<String, Object> mobileEmulation = new HashMap<>();
mobileEmulation.put("deviceMetrics", deviceMetrics);
mobileEmulation.put("userAgent", "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19");
Map<String, Object> clientHints = new HashMap<>();
clientHints.put("platform", "Android");
clientHints.put("mobile", true);
mobileEmulation.put("clientHints", clientHints);
ChromeOptions chromeOptions = new ChromeOptions(); chromeOptions.setExperimentalOption("mobileEmulation", mobileEmulation); WebDriver driver = new ChromeDriver(chromeOptions);

Ruby

mobile_emulation = {
   "deviceMetrics" => { "width" => 360, "height" => 640, "pixelRatio" => 3.0 },
   "userAgent" => "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19",
   "clientHints" => { "platform" => "Android", "mobile" => true}
}
caps = Selenium::WebDriver::Remote::Capabilities.chrome("chromeOptions" => mobile_emulation)
driver = Selenium::WebDriver.for :remote, url: 'http://localhost:4444/wd/hub', desired_capabilities: caps

Python

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
mobile_emulation = {
   "deviceMetrics": { "width": 360, "height": 640, "pixelRatio": 3.0 },
   "userAgent": "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19",
   "clientHints": {"platform": "Android", "mobile": True} }
chrome_options = Options()
chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)
driver = webdriver.Chrome(chrome_options = chrome_options)

完全なモバイル エミュレーション構成の例:

JSON

"mobileEmulation": {
  "userAgent": "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/111.0.0.0 Mobile Safari/537.36",
  "deviceMetrics": {
     "mobile": true,
     "touch": true,
     "width": 412,
     "height": 823,
     "pixelRatio": 1.75
  },
  "clientHints": {
     "brands": [
        {"brand": "Google Chrome", "version": "111"},
        {"brand": "Chromium", "version": "111"}
     ],
     "fullVersionList": [
        {"brand": "Google Chrome", "version": "111.0.5563.64"},
        {"brand": "Chromium", "version": "111.0.5563.64"}
     ],
     "platform": "Android",
     "platformVersion": "11",
     "architecture": "arm",
     "model": "lorem ipsum (2022)"
     "mobile": true,
     "bitness": "32",
     "wow64": false
  }
}

モバイル エミュレーションと実際のデバイスの違い

モバイル エミュレーションを使用してパソコンでモバイルサイトをテストすると便利ですが、テスターは、次のような微妙な違いが多数あることに留意する必要があります。

  • まったく異なる GPU であるため、パフォーマンスに大きな変化が生じる可能性があります。
  • モバイル UI がエミュレートされない(具体的には、非表示の URL バーがページの高さに影響します)。
  • 確認ポップアップ(いくつかのタップ ターゲットのいずれかを選択する)はサポートされていません。
  • 多くのハードウェア API(画面の向きの変更イベントなど)が利用できない。