我已经在这里成功安装了节点模块“mocha-phantomjs” 。我写了以下测试用例:
test1.js
describe("DOM Tests", function () {
var el = document.createElement("div");
el.id = "myDiv";
el.innerHTML = "Hi there!";
el.style.background = "#ccc";
document.body.appendChild(el);
var myEl = document.getElementById('myDiv');
it("is in the DOM", function () {
expect(myEl).to.equal(null);
});
it("is a child of the body", function () {
expect(myEl.parentElement).to.equal(document.body);
});
it("has the right text", function () {
expect(myEl.innerHTML).to.equal("Hi there!");
});
it("has the right background", function () {
expect(myEl.style.background).to.equal("rgb(204, 204, 204)");
});
});
测试用例.html
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="mocha.css" />
</head>
<body>
<div id="mocha"></div>
<script src="mocha.js"></script>
<script src="chai.js"></script>
<script>
mocha.ui('bdd');
mocha.reporter('html');
expect = chai.expect;
</script>
<script src="test1.js"></script>
<script>
if (window.mochaPhantomJS) { mochaPhantomJS.run(); }
else { mocha.run(); }
</script>
</body>
</html>
我得到的是绝对没有输出,即使我在我的 js 文件中做了错字。
这里的重点是我必须在无头服务器中使用一些自动化的 dom 和 js 测试并将其与 Jenkins 集成,我认为使用 TAP 报告是可以的。欢迎任何其他建议。
编辑
我更新了我的 phantomjs 版本,现在我得到以下信息:
TypeError: 'undefined' is not a function (evaluating 'mocha.ui('bdd')')
file:///home/seventeenlive/site/tests/TestRunner2.html:11
ReferenceError: Can't find variable: describe
file:///home/seventeenlive/site/tests/test1.js:20
ReferenceError: Can't find variable: process
mocha-phantomjs/core_extensions.js:21
mocha-phantomjs/core_extensions.js:82
ReferenceError: Can't find variable: process
phantomjs://webpage.evaluate():2
phantomjs://webpage.evaluate():8
phantomjs://webpage.evaluate():8
ReferenceError: Can't find variable: process
phantomjs://webpage.evaluate():3
phantomjs://webpage.evaluate():17
phantomjs://webpage.evaluate():17
Failed to start mocha.