1.安装对应浏览器版本的
chromedriver.exe
在此下载 http://chromedriver.storage.googleapis.com/index.html
notes.txt文件可查看对应的版本信息
2.代码如下:
/**
* 使用谷歌浏览器模拟wap测试
*/
@Test
public void initdriver(){
String devicesName="Galaxy S5"; //填写谷歌浏览器模拟器具有的设备名称
System.setProperty("webdriver.chrome.driver","D:\\yhtWord\\IdeaProject\\chromedriver.exe");//chromedriver.exe的安装路径
Map<String, String> mobileEmulation=new HashMap<String, String>();
//设置设备
mobileEmulation.put("deviceName",devicesName);
System.out.println("使用谷歌浏览器模拟手机设备为:"+ devicesName);
Map<String, Object> chromeOptions=new HashMap<String, Object>();
chromeOptions.put("mobileEmulation",mobileEmulation);
DesiredCapabilities capabilities=DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY,chromeOptions);
try {
WebDriver driver=new ChromeDriver(capabilities);
driver.get("http://www.baidu.com");
}catch (Exception e){
e.printStackTrace();
}
}
3.浏览器设置
最好关闭下浏览器,不然可能会报错:不能解析
capabilities 无效设备等。
4.运行@Test代码,浏览器打开如图