![](/img/trans.png)
[英]Mobile app emulation able to connect to online application service but not working on actual device
[英]Unable to run mobile emulation in custome device
我必須在不同的移動視圖中運行一些測試用例。 我正在嘗試使用 Google Chrome 在移動仿真模式下測試運行。 如果選擇了現有列表中的設備,但在嘗試從此處創建自定義設備時,測試用例工作正常:
並使用以下代碼運行測試:
@Test
public void responsive() {
String driverPath = "/Users/resources/drivers/chromedriver";
System.setProperty("webdriver.chrome.driver", driverPath);
Map<String, String> mobileEmulation = new HashMap<>();
mobileEmulation.put("deviceName", "Pixel 3");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setExperimentalOption("mobileEmulation", mobileEmulation);
WebDriver driver = new ChromeDriver(chromeOptions);
driver.get("https://www.google.com");
}
導致以下異常:
org.openqa.selenium.InvalidArgumentException:無效參數:'firstMatch'的條目0因無效參數而無效:無法解析功能:goog:chromeOptions來自無效參數:無法解析來自無效參數的mobileEmulation:'Pixel 3'必須是有效設備來自未知錯誤:必須是有效的設備構建信息:版本:'3.13.0',修訂:'2f0d292',時間:'2018-06-25T15:24:21.231Z'
是否有任何額外的東西需要在自定義設備中運行測試?
從google-chrome版本 83.0.4103.116上的默認設備列表中,我沒有將Pixel 3
視為列出的設備。
快照 A:
快照 B:
快照 C:
要添加自定義(新)設備,您必須根據以下內容添加設備詳細信息:
Device Name
width
height
Device pixel ratio
User agent string
User agent type
device type
快照:
不確定chrome中的自定義設備有什么問題。 即使我使用以下代碼打印所有設備,它也不會打印創建的自定義設備。
try {
ObjectMapper mapper = new ObjectMapper();
Map map = mapper.readValue(
new File("/Users/<username>/Library/Application Support/Google/Chrome/Default/Preferences"),
Map.class);
Map devTools = (Map) map.get("devtools");
Map preferences = (Map) devTools.get("preferences");
String standardEmulatedDeviceList = (String) preferences.get("standardEmulatedDeviceList");
List emulatorMap = mapper.readValue(standardEmulatedDeviceList, List.class);
System.out.println(emulatorMap.size());
for (Object object : emulatorMap) {
Map device = (Map) object;
System.out.println(device.get("title"));
}
} catch (IOException ex) {
System.out.println("Look there is an error..");
}
}
但是有一種替代方法可以做到這一點。 可以創建具有所需設備高度、寬度和像素比的設備矩陣。 在 Chrome 選項中傳遞該設備矩陣而不是設備名稱。 最重要的是,必須通過設備用戶代理才能使其表現得像設備屏幕。 這是一個很好的獲取設備規格的網站。
我的工作代碼在這里 -
Map<String, Object> deviceMetrics = new HashMap<>();
deviceMetrics.put("width", 412);
deviceMetrics.put("height", 797);
deviceMetrics.put("pixelRatio", 1.0);
Map<String, Object> mobileEmulation = new HashMap<>();
mobileEmulation.put("deviceMetrics", deviceMetrics);
mobileEmulation.put("userAgent", "Mozilla/5.0 (Linux; Android 9; Pixel 3 XL Build/PD1A.180621.003) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Mobile Safari/537.36");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setExperimentalOption("mobileEmulation", mobileEmulation);
WebDriver driver = new ChromeDriver(chromeOptions);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.