selenium 使用谷歌浏览器模拟wap测试

    /**
     * 使用谷歌浏览器模拟手机浏览器
     * @param devicesName
     * @author xxx
     * 创建时间:2017-06-15,更新时间:2017-06-15
     * 备注
     */
    public void initdriver(String devicesName){
        System.setProperty("webdriver.chrome.driver", "resources/chromedriver.exe");
        Map<String, String> mobileEmulation = new HashMap<String, String>();
        //设置设备,例如:Google Nexus 7/Apple iPhone 6
        //mobileEmulation.put("deviceName", "Google Nexus 7"); 
        Log.logInfo("使用谷歌浏览器模拟手机设备为:"+devicesName);
        mobileEmulation.put("deviceName", devicesName); 
        Map<String, Object> chromeOptions = new HashMap<String, Object>();     
        chromeOptions.put("mobileEmulation", mobileEmulation);     
        DesiredCapabilities capabilities = DesiredCapabilities.chrome();       
        capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
        try {
            Log.logInfo("开始启动driver~~~");
            driver = new ChromeDriver(capabilities);
            Log.logInfo("启动driver成功~~~");
        } catch (Exception e) {
            Log.logInfo("启动driver失败~~~");
            Log.logInfo(e.getMessage());
        }        
    }

devicesName名称规范,可安如下图填写:

 

运行的效果:

 

posted @ 2017-06-23 09:32  星星故乡  阅读(1163)  评论(0编辑  收藏  举报