Javascript 如何在节点上使用mocha和phantomjs确保每个测试都使用未触及的HTML
我编写了一个Web应用程序,其中我想使用mocha和phantomjs从命令行进行一些DOM测试,我正在尝试找出最好的方法 由于webapplication正在进行大量DOM操作,我希望能够为每个测试加载全新的DOM。我尝试使用,但我不知道如何在页面上运行mocha测试(或者这是否是正确的方法) 当然,我可以创建一个文件Javascript 如何在节点上使用mocha和phantomjs确保每个测试都使用未触及的HTML,javascript,node.js,testing,phantomjs,mocha.js,Javascript,Node.js,Testing,Phantomjs,Mocha.js,我编写了一个Web应用程序,其中我想使用mocha和phantomjs从命令行进行一些DOM测试,我正在尝试找出最好的方法 由于webapplication正在进行大量DOM操作,我希望能够为每个测试加载全新的DOM。我尝试使用,但我不知道如何在页面上运行mocha测试(或者这是否是正确的方法) 当然,我可以创建一个文件test.html,其中通过脚本标记包含mocha,然后运行phantomjs test.html。但是,我如何确保DOM在每次测试中都不受影响呢?为每个具有相同内容的测试添加一
test.html
,其中通过脚本标记包含mocha,然后运行phantomjs test.html
。但是,我如何确保DOM在每次测试中都不受影响呢?为每个具有相同内容的测试添加一个HTML文件将是一场噩梦
到目前为止,我所拥有的:
my testrunner.js:
var Mocha = require('mocha'),
expect = require('expect.js'),
fs = require('fs'),
path = require('path');
var mocha = new Mocha(
{
ui: 'bdd'
});
// I only add one file for now
mocha.addFile(
path.join(__dirname, 'tests_integration/test.js')
);
mocha.run(function(failures){
process.on('exit', function () {
process.exit(failures);
});
});
My test.js:
这就是我的挣扎之处,因为我不知道如何在页面内运行测试:
var phantom = require('phantom');
phantom.create(function (ph) {
ph.createPage(function (page) {
page.open("http://test.local", function (status) {
page.evaluate(function (tests) {
// here I need to run my tests, which obviously doesnt work that way
describe('DOM tests',function(){
it('should be able to acces the page',function(){
expect(document.body).not.to.be(undefined)
});
});
return document.body
}.bind(tests), function (html) {
ph.exit();
});
});
});
});
要运行这些测试,我需要execnode integration runner.js
。我想到的一个解决方案是将摩卡咖啡注入幻影页面,但这对我来说似乎有点骇客
我是不是完全错了 好的,经过一些研究并与其他开发人员交谈之后,创建不同的HTML文件似乎是最干净的解决方案。然而,由于我不想在每次更改webapp时手动更新所有这些文件,所以我决定使用injectJS解决方案 现在我可以动态地将mocha测试文件注入到实时站点并运行它们