(原创)如何使用selenium 驱动chrome浏览器而且打开方式为手机模式-转载请注明出处

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

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

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

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

Chrome version:windows

Chrome driver:api

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

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

相关文章
相关标签/搜索