/** * 使用谷歌瀏覽器模擬手機瀏覽器 * @param devicesName * @author xxx * 建立時間:2017-06-15,更新時間:2017-06-15 * 備註 */ public void initdriver(String devicesName){ System.setProperty("webdriver.chrome.driver", "resources/chromedriver.exe"); Map<String, String> mobileEmulation = new HashMap<String, String>(); //設置設備,例如:Google Nexus 7/Apple iPhone 6 //mobileEmulation.put("deviceName", "Google Nexus 7"); Log.logInfo("使用谷歌瀏覽器模擬手機設備爲:"+devicesName); mobileEmulation.put("deviceName", devicesName); Map<String, Object> chromeOptions = new HashMap<String, Object>(); chromeOptions.put("mobileEmulation", mobileEmulation); DesiredCapabilities capabilities = DesiredCapabilities.chrome(); capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions); try { Log.logInfo("開始啓動driver~~~"); driver = new ChromeDriver(capabilities); Log.logInfo("啓動driver成功~~~"); } catch (Exception e) { Log.logInfo("啓動driver失敗~~~"); Log.logInfo(e.getMessage()); } }
devicesName名稱規範,可安以下圖填寫:web
運行的效果:chrome