我一直在与这个问题作斗争一段时间,似乎无法找到解决方案。在服务器上使用 NightmareJS + mocha 运行测试时,它们会失败。我尝试运行:
DEBUG=nightmare:* env ENV=staging mocha --harmony tests.js
但看不到任何日志/操作错误等。只是测试失败(超时)。
Error: timeout of 30000ms exceeded. Ensure the done() callback is being called in this test.
我只能在没有 NightmareJS 的情况下运行 Mocha 测试,而且它们运行良好。此外,在我的本地机器上运行 nighmareJS 测试时,它们可以正常工作。
NightmareJS 安装在服务器上,package.json 内容:
"dependencies": {
"chai": "^3.5.0",
"mocha": "^2.4.5",
"mocha-generators": "^1.2.0",
"mocha-bamboo-reporter": "*",
"nightmare": "^2.2.0",
"nodeunit" : "~0.8"
}
这是测试的代码:
require('mocha-generators').install();
var Nightmare = require('nightmare');
var expect = require('chai').expect;
var url = null;
var options = {
show: false,
'webPreferences': {
partition: 'something_xyz'
}
};
if (process.env.ENV === 'staging') {
console.log("Running tests against staging environment");
url = 'https://staging.something.com/app/';
} else {
console.log("Running tests against local environment");
url = 'http://localhost:8080/app/';
}
describe('Nightmare JS tests', function() {
this.timeout(30000);
describe('base functionality', function() {
var nightmare;
before(function*() {
nightmare = Nightmare(options);
});
after(function*() {
var endTest = yield nightmare
.end()
});
it('Should be able to login', function*() {
var result = yield nightmare
.goto(url)
.wait('img.google-login-btn')
.click('img.google-login-btn')
.wait('div.main-content.row h4')
.wait(1000)
.evaluate(function () {
return document.querySelector('div.main-content.row h4').innerText;
})
expect(result).to.equal("Logged in!");
});
// here are the rest of the tests
});
});
我该如何调试呢?当我在服务器上运行时,我不能使用 show: true 选项。