Due to the need to test H5, you need to set the screen size of the page to simulate the phone browser. The Chrome browser comes with an emulator, which is the icon for the small mobile phone number on the F12 development toolbar. Manually Click to select the phone model to simulate. But if you need to set the browser to automate the script to automatically turn into a mobile browser screen size is, how to do it. Hate research debugging finally found a most complete answer. Find a lot of pits on the internet, will lose the most critical step. The setup code is posted here. Pro-Test available
Browser Setup code (for example in Chrome)
//设置浏览器driver System.setProperty("webdriver.chrome.driver", "tools/chromedriver.exe"); //浏览器选项设置 ChromeOptions options = new ChromeOptions(); options.addArguments("lang_zh_CN.UTF-8"); //这步是关键哦,指定的浏览器size,对应手机型号的size options.addArguments("window-size=375,667"); options.addArguments("user-agent=\"Mozilla/5.0 (iPhone; CPU iPhone OS 11_2 like Mac OS X) AppleWebKit/604.4.7 (KHTML, like Gecko) Version/11.0 Mobile/15C114 Safari/604.1\""); DesiredCapabilities cap = DesiredCapabilities.chrome(); cap.setCapability(ChromeOptions.CAPABILITY, options); WebDriver driver = new ChromeDriver(options);// driver.manage().window().maximize(); driver.get("https://www.baidu.com");
The effect of opening after operation see:
Automated test settings Browser properties to simulate H5 testing on mobile devices