ChromeDriver的npm之路


声明:本文转载自https://my.oschina.net/qixiaobo025/blog/1621246,转载目的在于传递更多信息,仅供学习交流之用。如有侵权行为,请联系我,我会及时删除。

背景

系统目前前端项目有点小爆发的感觉啊~

老项目中逐渐越来越多的使用心得技术 这不一个项目使用了chromeDriver

maven集成gulp压缩js,css方案 系统中使用了frontend-maven-plugin插件十分好用~

问题

某天小伙伴使用了chromeDriver出现了问题

[INFO] --- frontend-maven-plugin:1.3:npm (npm install) @ appclient --- [INFO] Running 'npm install --registry=https://registry.npm.taobao.org' in /data/jenkins/workspace/f6-local-test-mobile2.0/appclient [INFO] [INFO] > chromedriver@2.35.0 install /data/jenkins/workspace/f6-local-test-mobile2.0/appclient/node_modules/chromedriver [INFO] > node install.js [INFO] [INFO] Downloading https://chromedriver.storage.googleapis.com/2.35/chromedriver_linux64.zip [INFO] Saving to /data/jenkins/workspace/f6-local-test-mobile2.0/appclient/node_modules/chromedriver/chromedriver/chromedriver_linux64.zip [ERROR] ChromeDriver installation failed Error with http(s) request: Error: read ECONNRESET

报错的十分诡异 为啥还是走的chromedriver.storage.googleapis.com/???

检索

果然不止一个小伙伴碰到了

npm 安装 chromedriver 时,偶尔会出错,错误提示类似于:

> chromedriver@2.27.2 install /Users/Mario/Work/Lab/waylens-all-in-one-site/node_modules/chromedriver > node install.js  Downloading https://chromedriver.storage.googleapis.com/2.27/chromedriver_mac64.zip Saving to /var/folders/7l/mhhqzhps0y59by7pf04nyx5r0000gn/T/chromedriver/chromedriver_mac64.zip events.js:161       throw er; // Unhandled 'error' event       ^  Error: connect ETIMEDOUT 74.125.23.128:443     at Object.exports._errnoException (util.js:1023:11)     at exports._exceptionWithHostPort (util.js:1046:20)     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1090:14)

经分析发现,某些版本下,chromedriver 的 zip 文件 url 的响应是 302 跳转,而在 install.js 里使用的是 Node.js 内置的 http 对象的 get 方法无法处理 302 跳转的情况;而在另外一些情况下,则是因为 googleapis.com 被墙了,此时即使采用科学上网的方法也仍然无法获取文件。

无论是上述哪种情况,可以使用下面的命令安装:

npm install chromedriver --chromedriver_cdnurl=http://cdn.npm.taobao.org/dist/chromedriver

使用 cnpm 安装亦可。

可是我们用的插件不支持cnpm啊

final class DefaultNpmRunner extends NodeTaskExecutor implements NpmRunner {       static final String TASK_NAME = "npm";             public DefaultNpmRunner(NodeExecutorConfig config, ProxyConfig proxyConfig, String npmRegistryURL) {           super(config, TASK_NAME, config.getNpmPath().getAbsolutePath(), buildArguments(proxyConfig, npmRegistryURL));       }             private static List<String> buildArguments(ProxyConfig proxyConfig, String npmRegistryURL) {           List<String> arguments = new ArrayList<String>();                              if(npmRegistryURL != null && !npmRegistryURL.isEmpty()){               arguments.add ("--registry=" + npmRegistryURL);           }                 if(!proxyConfig.isEmpty()){               Proxy proxy = null;               if(npmRegistryURL != null && !npmRegistryURL.isEmpty()){                   proxy = proxyConfig.getProxyForUrl(npmRegistryURL);               }                     if(proxy == null){                   proxy = proxyConfig.getSecureProxy();               }                     if(proxy == null){                   proxy = proxyConfig.getInsecureProxy();               }                     arguments.add("--https-proxy=" + proxy.getUri().toString());               arguments.add("--proxy=" + proxy.getUri().toString());           }                       return arguments;       }   }

也不支持传递proxy和registy之外的参数!!!莫非这就到死路了么?

方案

  1. 使用alias 安装完cnpm然后alias为npm应该可行
  2. 在npmrc环境中添加对应cdn_url 

解决

为了最小化影响其他应用 开率采用方案2

如下~/.npmrc

chromedriver_cdnurl=https://npm.taobao.org/mirrors/chromedriver 
[INFO] --- frontend-maven-plugin:1.3:npm (npm install) @ appclient --- [INFO] Running 'npm install --registry=https://registry.npm.taobao.org' in /data/jenkins/workspace/f6-local-test-mobile2.0/appclient [INFO] [INFO] > chromedriver@2.35.0 install /data/jenkins/workspace/f6-local-test-mobile2.0/appclient/node_modules/chromedriver [INFO] > node install.js [INFO] [INFO] Downloading https://npm.taobao.org/mirrors/chromedriver/2.35/chromedriver_linux64.zip [INFO] Saving to /data/jenkins/workspace/f6-local-test-mobile2.0/appclient/node_modules/chromedriver/chromedriver/chromedriver_linux64.zip [INFO] Received 782K... [INFO] Received 1564K... [INFO] Received 2346K... [INFO] Received 3128K... [INFO] Received 3634K total. [INFO] Extracting zip contents

 

本文发表于2018年02月11日 18:36
(c)注:本文转载自https://my.oschina.net/qixiaobo025/blog/1621246,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如有侵权行为,请联系我们,我们会及时删除.

阅读 3420 讨论 0 喜欢 0

抢先体验

扫码体验
趣味小程序
文字表情生成器

闪念胶囊

你要过得好哇,这样我才能恨你啊,你要是过得不好,我都不知道该恨你还是拥抱你啊。

直抵黄龙府,与诸君痛饮尔。

那时陪伴我的人啊,你们如今在何方。

不出意外的话,我们再也不会见了,祝你前程似锦。

这世界真好,吃野东西也要留出这条命来看看

快捷链接
网站地图
提交友链
Copyright © 2016 - 2021 Cion.
All Rights Reserved.
京ICP备2021004668号-1