(原創)如何使用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

相關文章
相關標籤/搜索