随着移动设备使用率的不断增加,移动页面的测试也变得越来越重要。

对于互联网公司M站的测试,如果不通过专用的appium等移动端测试工具是否还有方便快捷的办法呢?答案当然是有啊。

使用chrome driver和chrome浏览器并进入chrome的 toggle device mode 模式,就可以很好的模拟手机端,下面直接上代码。

public class Test{
	public static void main(String args[])
	{
		System.setProperty("webdriver.chrome.driver", "E:/Software/自动化软件/chromedriver.exe");
		Map<String, String> mobileEmulation = new HashMap<String, String>();
		mobileEmulation.put("deviceName", "Google Nexus 5");		
		Map<String, Object> chromeOptions = new HashMap<String, Object>();		
		chromeOptions.put("mobileEmulation", mobileEmulation);		
		DesiredCapabilities capabilities = DesiredCapabilities.chrome();		
		capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);		
		WebDriver driver = new ChromeDriver(capabilities);
		Navigation navigation=driver.navigate();
		navigation.to("https://m.baidu.com/");
		String title=driver.getTitle();
		System.out.println("title:"+title);
		
	}

}

  说一下我使用的chrome版本和chromedriver版本,这两个如果配合不好会有坑。

Chrome version:

Chrome driver:

去这个地方 http://chromedriver.storage.googleapis.com/index.html 打不开自己想办法吧,哈哈,下载2.15版本 我是windows平台,下载的32那个,这样selenium每次打开浏览器就是手机模式了,可以操作对手机方面的一些测试。

                                                                                                                                                                                                                                                                                        -----------------------from 鑫测试

 

 posted on 2016-10-31 16:04  金色伏特加  阅读(8644)  评论(0编辑  收藏  举报