1.背景

因为现在项目是要做分布式,而以前使用谷歌浏览器模拟手机运行做的分布式,是指定在某台机器运行是通过Jenkins配置,来指定服务器,但是这样有一个问题,如果大家都同时配置到某台电脑,那台服务器压力就很大,所以需要根据每台服务器的情况,去分配任务,那我就需要解决第一个问题,如何让模拟器指定ip运行,我的项目的部署方式(分布式)使用的selenium grid 的方式,使用模拟器方式最开始一直使用的是ChromeDriver方式启动,但是这个ChromeDriver方式启动只能是启动本地浏览器,进行手机模拟,不能指定在某台IP上进行运行

最开始看了很多官网资料,https://sites.google.com/a/chromium.org/chromedriver/mobile-emulation ,针对ChromeDriver java没有办法指定IP,而python可以,最开始走了一大圈,想通过ChromeDriver的方法来进行改变,饶了一大圈的弯路,最后想通了一点只要更换启动方式就可以了

以前启动手机模拟代码是这样:

public void initdriver(){
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-infobars");
Map<String, Object> prefs = new HashMap<String, Object>();
// 是否加载图片
// prefs.put("profile.managed_default_content_settings.images", 2);
options.setExperimentalOption("prefs", prefs);
Map<String, Object> deviceMetrics = new HashMap<String, Object>();
deviceMetrics.put("width", 360);
deviceMetrics.put("height", 640);
deviceMetrics.put("pixelRatio", 3.0);
Map<String, Object> mobileEmulation = new HashMap<String, Object>();
mobileEmulation.put("deviceMetrics", deviceMetrics);
mobileEmulation.put("userAgent",
"Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19");
options.setExperimentalOption("mobileEmulation", mobileEmulation);
System.setProperty("webdriver.chrome.driver", "resources/chromedriver.exe");
driver = new ChromeDriver(options);
}

 而现在我只需要把启动方式做更改

ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-infobars");
Map<String, Object> prefs = new HashMap<String, Object>();
// 是否加载图片
// prefs.put("profile.managed_default_content_settings.images", 2);
options.setExperimentalOption("prefs", prefs);
Map<String, Object> deviceMetrics = new HashMap<String, Object>();
deviceMetrics.put("width", 360);
deviceMetrics.put("height", 640);
deviceMetrics.put("pixelRatio", 3.0);
Map<String, Object> mobileEmulation = new HashMap<String, Object>();
mobileEmulation.put("deviceMetrics", deviceMetrics);
mobileEmulation.put("userAgent",
"Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19");
options.setExperimentalOption("mobileEmulation", mobileEmulation);
System.setProperty("webdriver.chrome.driver", "resources/chromedriver.exe");
if(TestngListener2.getProjectCode().contains("platform")){//这里表示使用的平台,为了不影响以前框架使用
System.out.println("使用的平台进行启动的浏览器");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
try {
driver = new RemoteWebDriver(new URL("http://10.40.2.114:5555/wd/hub"), capabilities);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}else{
driver = new ChromeDriver(options);
} }

  

最新文章

  1. 学习笔记:delphi多线程知识
  2. 被碾压过得Samsung SCH-W319 的取证恢复
  3. WPF中Grid布局
  4. IOS开发——使用数据库
  5. 在Android上用AChartEngine轻松绘制图表
  6. 解决Ubuntu 下 vi编辑器不能使用方向键和退格键问题
  7. HDU 1254
  8. ExecutorService常用方法和newFixedThreadPool创建固定大小的线程池
  9. JavaScript中的prototype
  10. windows phone 新手引导
  11. puppet运维配置实列
  12. NHibernate执行原生带sum的sql
  13. 仿制的ActivityIndicatorView
  14. Emmet插件
  15. 命令行解决mysql中文乱码
  16. MySQL操作失误导致mysql数据库查看不了
  17. zip7压缩
  18. 201521123018 《Java程序设计》第1周学习总结
  19. es6的新特性--模板字符串
  20. windows下安装和配置nginx

热门文章

  1. Java 初始化和清理
  2. 问题:git add 遇到 warning: LF will be replaced by CRLF in 警告(已解决)
  3. TCP Nagle算法以及延迟确认(即延迟回复ACK)的学习
  4. 对象池2(方法功能)Pools
  5. [转]FireFox与IE 下js兼容触发click事件的代码
  6. Kudu的性能测试
  7. 控制input输入框中提示信息的显示和隐藏的方法
  8. Hadoop学习笔记(3) Hadoop I/O
  9. Spring Boot2中Spring Security导致Eureka注册失败
  10. mapreduce总结