Background: The first to use mobile phone simulation H5 page run automation, found often because of the app connection or network reasons, such as a series of circumstances, resulting in M version (H5 page) Use cases run, want to run through the browser's own mobile phone simulator, to ensure stability
The browser comes with an emulator such as:
Code implementation Logic
Public classruntest {webdriver driver; @BeforeClass Public voidBeforeclass () {System.setproperty ("Webdriver.chrome.driver", "Resources/chromedriver.exe"); Map<string, string> mobileemulation =NewHashmap<string, string>(); //set up your device, for example: Google Nexus 7/apple iPhone 6//mobileemulation.put ("DeviceName", "Google Nexus 7");Mobileemulation.put ("DeviceName", "Apple IPhone 6 Plus"); Here is the name of the emulator to use, which is the top Model MAP in the emulator in the browser<string, object> chromeoptions =NewHashmap<string, object>(); Chromeoptions.put ("Mobileemulation", mobileemulation); Desiredcapabilities Capabilities=Desiredcapabilities.chrome (); Capabilities.setcapability (chromeoptions.capability, chromeoptions); Try{System.out.println ("Start driver~~~"); Driver=NewChromedriver (capabilities); System.out.println ("Start Driver Success ~ ~ ~"); } Catch(Exception e) {System.out.println ("Startup driver failed ~ ~ ~"); System.out.println (E.getmessage ()); }} @Test Public voidrun () {Driver.get ("http://m.baidu.com/"); System.out.println ("Use the browser, go to the Baidu page"); }
Effect
Selenium run H5 Web page using Google Chrome's own phone simulator