As mobile device usage continues to increase, the testing of mobile pages becomes increasingly important.
For the Internet company m Station test, if not through the dedicated Appium and other mobile testing tools, whether there is a convenient way to do? The answer, of course, is yes.
Using Chrome driver and chrome and going to Chrome's toggle device mode mode, you'll be able to simulate the phone side, directly below the code.
ImportJava.util.HashMap;ImportJava.util.Map;ImportOrg.openqa.selenium.WebDriver;Importorg.openqa.selenium.WebDriver.Navigation;ImportOrg.openqa.selenium.chrome.ChromeDriver;Importorg.openqa.selenium.chrome.ChromeOptions;Importorg.openqa.selenium.remote.DesiredCapabilities; Public classTestH5 { Public Static voidMain (String args[]) {System.setproperty ("Webdriver.chrome.driver", "E:/selenium/2016-07-14/chromedriver.exe"); Map<string, string> mobileemulation =NewHashmap<string, string>(); Mobileemulation.put ("DeviceName", "Google Nexus 5"); Map<string, object> chromeoptions =NewHashmap<string, object>(); Chromeoptions.put ("Mobileemulation", mobileemulation); Desiredcapabilities Capabilities=Desiredcapabilities.chrome (); Capabilities.setcapability (chromeoptions.capability, chromeoptions); Webdriver Driver=NewChromedriver (capabilities); Navigation Navigation=driver.navigate (); Navigation.to ("https://m.baidu.com/"); String title=Driver.gettitle (); System.out.println ("Title:" +title); }}
Using the chrome version and the Chromedriver version, these two will have pits if they don't fit well.
Chromedriver version is 2.53.1
Reprint: http://www.cnblogs.com/iamhp/p/6016194.html
How to use selenium to drive Chrome browser and open mode for phone mode