0

我目前正在尝试在 Web 服务器模式下运行 JSCover 以确定在 PhantomJS 无头浏览器中执行的 Jasmine 测试的覆盖范围。我也在使用 grunt+nodejs 来启动测试。

我在 gruntfile 中用于启动 JSCover 服务器并执行 phantomJS 的代码是:

// Start JSCover Server
var childProcess = require('child_process'),
var JSCOVER_PORT = "43287";
var JAVA_HOME = process.env.JAVA_HOME;

var jsCoverChildArgs = [
        "-jar", "src/js/test/tools/JSCover-all.jar",
        "-ws",
        "--branch",
        "--port="+JSCOVER_PORT,
        "--document-root=./",
        "--report-dir=target/",
        "--no-instrument=src/js/lib/",
        "--no-instrument=src/js/test/",
        "--no-instrument=src/js/test/lib/"
    ];

var jsCoverProc = childProcess.spawn(JAVA_HOME + "/bin/java", jsCoverChildArgs);

// Start PhantomJS
var phantomjs = require('phantomjs'),
var binPath = phantomjs.path,

var childArgs = [
        'src/js/test/lib/phantomjs_jasminexml_runner.js',
        'http://localhost:'+JSCOVER_PORT+'/src/js/test/SpecRunner.html',
        'target/surefire-reports'
    ];
runner = childProcess.execFile(binPath, childArgs);

runner.on('exit', function (code) {
    // Tests have finished, so clean up the process
    var success = (code === 0) ? true : false;
    jsCoverProc.kill(); // kill the JSCover server now that we are done with it

    done(success);
}); 

但是,当我在 cloudbees 中的 Jenkins 节点上运行 Web 服务器,然后针对它运行 phantomjs 时,我收到以下错误之一:

  • 一些测试开始运行,但随后该过程失败:

    A spec : should be able to have a mock lo-dash ... 
    Warning: Task "test" failed. Use --force to continue.
    
    Aborted due to warnings.
    Build step 'Execute shell' marked build as failure
    Recording test results
    Finished: FAILURE
    
  • PhantomJS 无法访问 JSCover 服务器:

    Running "test" task
    phantomjs> Could not load 'http://127.0.0.1:43287/src/js/test/SpecRunner.html'.
    Warning: Task "test" failed. Use --force to continue.
    

对于第二个错误,我尝试使用我设置的不同端口和主机名(例如,主机名使用 127.0.0.1 或 localhost,端口使用 4327、43287 等)。端口不是在构建时动态设置的——我将它们硬编码在我的 grunt 脚本中。

关于为什么会发生上述错误或为什么我在 Cloudbees Jenkins 节点上运行和访问 JSCover 服务器时遇到问题(但从未在我的本地机器上)有任何想法吗?

4

2 回答 2

3

因此,当您使用任何进程执行 JSCover 时,都需要时间才能启动。如果我们期望它早点出现,那么错误肯定会出现。

引用伟大的文章:http ://blog.johnryding.com/post/46757192364/javascript-code-coverage-with-phantomjs-jasmine-and

现在我有了一个满足我所有要求的代码覆盖工具,最后一部分是让这个代码作为我们 Jenkins 构建的一部分运行(它使用 grunt 脚本)。这很容易运行,但我遇到了两个一直破坏我的构建的错误:

  1. 有时 phantomJS 会无法连接到 JSCover 服务器
  2. 有时 phantomJS 会连接到服务器,但随后会在运行期间的某个随机点放弃执行我的测试。

These were really weird issues that only occurred on my team’s Jenkins nodes and were hard to diagnose - even though they turned out to be simple fixes.

For issue 1, that error was the result of my grunt script not waiting for JSCover to start before I executed phantomJS.

For the second issue, it turns out that my team was using a special jasmine test runner to help with producing XML files after tests completed. The problem with this file was that it had a function that waited for Jasmine to complete its execution, but utilized an extremely short timeout before it gave up running the tests. This was a problem with Jenkins + JSCover because it took a longer time for the tests to load and run now that they had to be loaded from a web server instead of straight from the file system. Fortunately, this fix was as easy as increasing the timeout.

于 2013-05-31T07:20:33.587 回答
1

我会说你需要在生成 JSCover 后等待一段时间 - 过去我在生成时使用 webdriver 完成了一些事情,然后等待它可用(理想情况下,你可以寻找响应并休眠,重复,直到生成的进程准备好)。

即在继续之前从 127.0.0.1:43287 查找有效的 http 响应(无论“有效”表示服务器已启动)。

于 2013-04-04T04:04:46.483 回答