行動裝置模擬

Chrome 使用者只要啟用 Chrome 開發人員工具中的行動裝置模擬功能,就可以在 Chrome 電腦版的行動裝置 (例如「Nexus 7」平板電腦或 iPhone 5) 上模擬 Chrome。這項功能可加快網頁開發速度,讓開發人員不需使用實體裝置,就能快速測試網站在行動裝置上的呈現效果。ChromeDriver 也可以透過「mobileEmulation」啟用行動裝置模擬功能功能,並將結果值指定為字典值。

和「開發人員工具模擬」面板中一樣,ChromeDriver 提供兩種啟用行動裝置模擬功能的方式:指定已知裝置,或是指定個別裝置屬性。「mobileEmulation」的格式則取決於您需要的方法。

指定已知的行動裝置

如要啟用具有特定裝置名稱的「行動裝置模擬」功能,請使用「mobileEmulation」字典必須包含「deviceName」。請使用開發人員工具模擬面板中的有效裝置名稱做為「deviceName」的值。

裝置設定的螢幕截圖

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」中指定下列裝置指標字典:

  • 「寬度」- 裝置螢幕的寬度 (以像素為單位)
  • &quot;height&quot;- 裝置螢幕的高度 (以像素為單位)
  • 「pixelRatio」- 裝置的像素比例
  • 「觸控」- 是否模擬觸控事件。這個值預設為 true,且通常可以省略。
  • 「行動裝置」- 瀏覽器是否必須像行動使用者代理程式一樣運作 (重疊捲軸、發出方向事件、縮小內容以符合可視區域等)。這個值預設為 true,且通常可以省略。

「clientHints」字典中可能包含下列項目:

  • 「月台」- 作業系統。這可能是已知值 (「Android」、「ChromeOS」、「Chromium OS」、「Fuchsia」、「Linux」、「macOS」、「Windows」) 等,且該值完全符合 Chrome 在指定平台上所傳回的值,或是使用者定義的值。必須提供這個值。
  • 「行動裝置」- 瀏覽器是否應該要求行動版或電腦版資源版本。一般來說,在搭載 Android 的手機上執行 Chrome 時,這個值會設為 True。平板電腦 Android 裝置上的 Chrome 將這個值設為 false。電腦裝置上的 Chrome 也會將這個值設為 false。您可以使用這項資訊指定實際模擬。必須提供這個值。
  • 其餘項目是選用項目,可省略,除非這些項目與測試相關:
    • 「brands」:品牌 / 主要版本組合的清單。如果省略這個引數,瀏覽器會採用自己的值。
    • &quot;fullVersionList&quot;- 品牌和版本組合的清單。省略時則讓瀏覽器使用自己的值。
    • 「platformVersion」- 作業系統版本。預設為空字串。
    • 「模型」- 裝置型號。預設為空字串。
    • 「架構」- CPU 架構。已知值為「x86」和「arm」。使用者可自行提供任何字串值。預設為空字串。
    • 「bitness」- 平台位元性。已知值為「32」以及「64」使用者可自行提供任何字串值。預設為空字串。
    • 「wow64」- 在 Windows 64 上模擬視窗 32。預設為 false 的布林值。

ChromeDriver 可以推論「userAgent」「clientHints」的值在下列平台上使用「Android」、「ChromeOS」、「Chromium OS」、「Fuchsia」、「Linux」、「macOS」、「Windows」。因此可以省略這個值。

如果「clientHints」省略字典 (舊版模式) ChromeDriver 會以最佳方式推論「clientHints」來自「userAgent」由於「userAgent」存在內部混淆,這項功能無法正確運作值的格式。

如要瞭解可在「Mobile Emulation」面板中找到的手機和平板電腦,請前往開發人員工具原始碼

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,效能可能會因此大幅改變
  • 行動使用者介面未模擬 (尤其是隱藏網址列會影響網頁高度);
  • 系統不支援消歧彈出式視窗 (當中請選取幾個觸控目標之一);
  • 許多硬體 API (例如螢幕方向變更事件) 無法使用