Karma 无法捕获 PhantomJS

Posted

技术标签:

【中文标题】Karma 无法捕获 PhantomJS【英文标题】:Karma can't capture PhantomJS 【发布时间】:2013-07-01 08:34:15 【问题描述】:

我们已经设置了一个运行 Karma 的 Jenkins CI 服务器,目标是 PhantomJS。我们正在通过 Grunt 运行我们的测试。 Jenkins、Grunt 和 Phantom 都运行正常,而且 Karma 似乎启动良好,但 Karma 无法捕获 Phantom。我们的脚本在本地(OSX)运行得很好。通过 bash 或 Jenkins 运行时存在相同的错误:

Running "karma:jenkins-unit" (karma) task
[2013-07-03 11:03:12.168] [WARN] config - urlRoot normalized to "/__karma/"
DEBUG [reporter]: Using reporter "dots".
DEBUG [reporter]: Using reporter "junit".
DEBUG [reporter]: Using reporter "coverage".
INFO [karma]: Karma server started at http://localhost:8084/__karma/
INFO [launcher]: Starting browser PhantomJS
DEBUG [launcher]: Creating temp dir at /tmp/testacular-7720703
DEBUG [launcher]: phantomjs /tmp/testacular-7720703/capture.js
INFO [karma]: To run via this server, use "karma run --runner-port 9104"
...
WARN [launcher]: PhantomJS have not captured in 60000 ms, killing.
DEBUG [launcher]: Process PhantomJS exitted with code 0
DEBUG [launcher]: Cleaning temp dir /tmp/testacular-7720703
INFO [launcher]: Trying to start PhantomJS again.
DEBUG [launcher]: Creating temp dir at /tmp/testacular-7720703
DEBUG [launcher]: phantomjs /tmp/testacular-7720703/capture.js
WARN [launcher]: PhantomJS have not captured in 60000 ms, killing.
DEBUG [launcher]: Process PhantomJS exitted with code 0
DEBUG [launcher]: Cleaning temp dir /tmp/testacular-7720703
INFO [launcher]: Trying to start PhantomJS again.
DEBUG [launcher]: Creating temp dir at /tmp/testacular-7720703
DEBUG [launcher]: phantomjs /tmp/testacular-7720703/capture.js
WARN [launcher]: PhantomJS have not captured in 60000 ms, killing.
DEBUG [launcher]: Process PhantomJS exitted with code 0
DEBUG [karma]: PhantomJS failed to capture, aborting the run.
DEBUG [launcher]: Disconnecting all browsers
DEBUG [launcher]: Killing PhantomJS
DEBUG [launcher]: Cleaning temp dir /tmp/testacular-7720703
Warning: Task "karma:jenkins-unit" failed. Use --force to continue.

我们的服务器是 CentOS 6.4。

以下是我们运行的版本: 咕噜声-cli v0.1.9 咕噜声 v0.4.1 节点 0.10.12 和 0.8.25。 幻影1.9.1 业力0.8.6

任何帮助将不胜感激!

【问题讨论】:

我的设置和你完全不同,但是当我收到PhantomJS failed to capture 错误时,原来是 localhost 没有映射到 127.0.0.1。这就是可能触发此错误的原因。 【参考方案1】:

karma.conf.js配置文件中使用轮询代替套接字,使用绝对路径代替相对路径,以确保正确遍历目录结构并且客户端/服务器连接没有外部依赖:

module.exports = function(config) 
  
  var absolute_root = process.cwd() + '/';
  config.set
  (
    
    // https://npmjs.org/browse/keyword/karma-adapter
    frameworks: ['jasmine'],

    // list of files

    files: 
       [
       absolute_root + 'test/Spec/**/*.js',
       absolute_root + 'js/*.js',
       absolute_root + '../libs/jquery.js'
       ],

     usePolling: true,

     transports: ['xhr-polling', 'jsonp-polling'],

     browsers: ['PhantomJS']
    
  );
  ;

参考文献

Karma is not able to run test cases on phantomJS

Karma doesn't exit properly when using public api with the finish callback

AngularJS + Socket.IO + karma not working in karma 0.8.5

Karma Runner Hangs Indefinitely

Preprocessors not running in Jenkins(Linux) "basePath" is relative to "C:\" instead of config file Karma config.js Istanbul/coverage reporter generate LCOV file with relative path for SF parameter instead of absolute path

【讨论】:

【参考方案2】:

在我的情况下添加

transports: ['xhr-polling', 'jsonp-polling']

karma.conf.js 就足够了。真正的问题是业力的一个非常旧的版本(0.12)。现在有了 1.4。我不需要消耗 CPU 的轮询。

【讨论】:

以上是关于Karma 无法捕获 PhantomJS的主要内容,如果未能解决你的问题,请参考以下文章

未捕获的类型错误:无法读取未定义抛出的属性“coSearchCriteria” - Angular Karma/Jasmine

karma.conf.js 未捕获的参考错误:谷歌未定义

Karma/Jasmine 测试失败并出现错误:'未捕获的错误:尚未为上下文加载模块名称“simple_test.js”:_。使用要求([])'

未捕获的ReferenceError:未定义必需

未捕获的类型错误:无法读取未定义的属性“团队”抛出 - 业力

Karma 无法匹配任何 js 文件