How to use selenium to drive Chrome browser and open mode for phone mode

Source: Internet
Author: User

As mobile device usage continues to increase, the testing of mobile pages becomes increasingly important.

For the Internet company m Station test, if not through the dedicated Appium and other mobile testing tools, whether there is a convenient way to do? The answer, of course, is yes.

Using Chrome driver and chrome and going to Chrome's toggle device mode mode, you'll be able to simulate the phone side, directly below the code.

ImportJava.util.HashMap;ImportJava.util.Map;ImportOrg.openqa.selenium.WebDriver;Importorg.openqa.selenium.WebDriver.Navigation;ImportOrg.openqa.selenium.chrome.ChromeDriver;Importorg.openqa.selenium.chrome.ChromeOptions;Importorg.openqa.selenium.remote.DesiredCapabilities; Public classTestH5 { Public Static voidMain (String args[]) {System.setproperty ("Webdriver.chrome.driver", "E:/selenium/2016-07-14/chromedriver.exe"); Map<string, string> mobileemulation =NewHashmap<string, string>(); Mobileemulation.put ("DeviceName", "Google Nexus 5"); Map<string, object> chromeoptions =NewHashmap<string, object>(); Chromeoptions.put ("Mobileemulation", mobileemulation); Desiredcapabilities Capabilities=Desiredcapabilities.chrome ();        Capabilities.setcapability (chromeoptions.capability, chromeoptions); Webdriver Driver=NewChromedriver (capabilities); Navigation Navigation=driver.navigate (); Navigation.to ("https://m.baidu.com/"); String title=Driver.gettitle (); System.out.println ("Title:" +title); }}

Using the chrome version and the Chromedriver version, these two will have pits if they don't fit well.

Chromedriver version is 2.53.1

Reprint: http://www.cnblogs.com/iamhp/p/6016194.html

How to use selenium to drive Chrome browser and open mode for phone mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.