Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Set browser properties for automated testing to simulate H5 testing on mobile devices

Shulou Source: shulou.com Published: 2022-06-03 07:30:19 09月23日 Update

Due to the need to test H6, you need to set the page to simulate the screen size of the mobile browser. The chrome browser comes with a simulator, that is, the icon of the small mobile number on the development toolbar after F12. After manual click, you can choose the mobile phone model to simulate. But if you need to set the screen size of the browser to automatically change to the mobile browser when executing the automation script, how to do it. I hate to research and debug and finally find the most complete answer. Look for a lot of holes on the Internet, will lose the most crucial step. The setting code is posted here. Self-testing is available

Browser setting code (take chrome browser as an example)

/ / set browser driver System.setProperty ("webdriver.chrome.driver", "tools/chromedriver.exe"); / / set browser options ChromeOptions options = new ChromeOptions (); options.addArguments ("lang_zh_CN.UTF-8"); / / this step is critical. Specify browser size, corresponding to the size options.addArguments of the phone model ("window-size=375667"). Options.addArguments ("user-agent=\" Mozilla/5.0 (iPhone; CPU iPhone OS 1132 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 it after running is shown below:

Tags: Browsing browser mobile phone code key model size screen test automation most complete following picture that is icon tool toolbar manual mobile phone number effect simulator Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Apple macOS Shulou Tech Info MySQL