我目前正在尝试在 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 服务器时遇到问题(但从未在我的本地机器上)有任何想法吗?