所以这里是我试图用casperjs和摩卡解决的问题。我试图在页面上测试一个元素的文本值,以查看它是否在...... 5-10秒的时间内更新。想法是我抓住价值,推入一个数组,等待500毫秒,并重复,直到数组有20个项目。那大约10秒。然后在阵列上运行下划线/ lodash的_.uniq
函数,并测试数组长度为> 1
。摩卡不尊重超时或完成回调时运行CasperJS测试
我正在运行的问题是,摩卡没有等待完成,因为说明测试是成功/失败。我想我可以增加摩卡的超时时间,但这没有任何区别。请参阅下面的代码。我已经评论它的可读性。
it('has elements whose values update', function() {
// 20 seconds, which should be plenty of time
this.timeout(20000);
casper.then(function() {
// The test array
var values = [],
// So we can stop the intervals
intervalId;
function getValue() {
// Grab element's text value
var value = casper.evaluate(function() { return $('#element').text(); });
// Push in to our test array
values.push(value);
// 20 * 500ms == 10 seconds or 10000ms
// When set to 500ms, this block never runs. The test passes before it has a chance to
if (values.length === 20) {
// Stop it from checking the value any further
clearInterval(intervalId);
// Test to see we've had more than one unique value in the 10 seconds
expect(_.uniq(values).length).to.be.gt(1);
}
}
// Wait for the value on the page to populate
// It defaults to '-' when the page loads
casper.waitFor(function() {
return this.evaluate(function() {
return $('#element').text() !== '-';
});
// Start the check with a delay of 500ms between each check
}, function then() {
intervalId = setInterval(getValue, 500);
});
});
});
对于设定为500毫秒的时间间隔值I获得到下一个测试之前摩卡移动在values
2-3元素值。甚至更奇怪的是我console.log(values)
他们正在确定测试通过摩卡后,屏幕上打印。原因是values.length
永远不会达到10,所以expect
调用永远不会被调用。测试被认为是通过。这是在500毫秒间隔测试输出:
Dashboard
✓ has elements whose values update (202ms)
Values: ["20,832,022"]
Values: ["20,832,022","20,832,372"]
Values: ["20,832,022","20,832,372","20,832,722"]
✓ has the page title of leads (41ms)
2 passing (11s)
它通过即使没有20个项目。它永远不会检查它由于某处的超时。这里是50ms的间隔输出:
Dashboard
✓ has elements whose values update (341ms)
Values: ["20,400,667"]
Values: ["20,400,667","20,400,718"]
Values: ["20,400,667","20,400,718","20,400,718"]
Values: ["20,400,667","20,400,718","20,400,718","20,400,769"]
Values: ["20,400,667","20,400,718","20,400,718","20,400,769","20,400,769"]
Values: ["20,400,667","20,400,718","20,400,718","20,400,769","20,400,769","20,400,820"]
Values: ["20,400,667","20,400,718","20,400,718","20,400,769","20,400,769","20,400,820","20,400,820"]
Values: ["20,400,667","20,400,718","20,400,718","20,400,769","20,400,769","20,400,820","20,400,820","20,400,871"]
Values: ["20,400,667","20,400,718","20,400,718","20,400,769","20,400,769","20,400,820","20,400,820","20,400,871","20,400,871"]
Values: ["20,400,667","20,400,718","20,400,718","20,400,769","20,400,769","20,400,820","20,400,820","20,400,871","20,400,871","20,400,922"]
Final Values: ["20,400,667","20,400,718","20,400,718","20,400,769","20,400,769","20,400,820","20,400,820","20,400,871","20,400,871","20,400,922"]
✓ has the page title of leads (41ms)
2 passing (8s)
我得到更多的50ms,但这只是一个半秒的测试。页面上的其他一些值需要更长时间才能更新为此值不可行。
我试过通过done
回调it
声明,但摩卡忽略它,并不等待它被调用。
这是一个工具的限制还是我使用它们是错误的?
我已经尝试使用下面的方法完成回调。
it('has elements whose values update', function (done) {
和
expect(_.uniq(values).length).to.be.gt(1);
done();
它仍然忽略了我标志着试验,异步。在500ms时,它仍然通过,但没有进入if语句或完成的呼叫。在50毫秒它抛出这个错误:
done() called multiple times
我使用mocha-casperjs。这会影响它吗?