done() 的用途和使用方法(量角器、茉莉花) [英] what done() is for and how to use it ( protractor, jasmine)
问题描述
it('should for something', function check(done) {
browser.sleep(2000);
$('.csTx').isPresent().then(function(result) {
if(result) {
done();
} else {
xPage.clickBack();
check(done);
}
})
}, 30000);
有人可以解释 done() 是如何工作的,以及它是做什么用的.我用谷歌搜索了它,但找不到任何容易让我理解的信息.我正在使用量角器和茉莉花进行自动化.请考虑上面的代码.
Can someone explain how done() works and what this is for. I googled it but cannot find any information that would be easy enough for me to understand. I am automating with protractor and jasmine. please consider the above code.
推荐答案
如果您的测试在您的测试的控制流中创建了一个并行任务队列,您需要使用 done
(阅读更多关于 承诺和控制流程).
You need to use done
if your test creates a parallel TaskQueue in your test's Control Flow (read more about promises and control flow).
例如:
describe('Control Flow', function() {
function logFromPromise(text) {
var deferred = protractor.promise.defer();
deferred.then(function() {
console.log(text);
});
deferred.fulfill();
return deferred;
}
it('multiple control flows', function() {
setTimeout(function() {
logFromPromise('1');
});
logFromPromise('0');
});
}
调用setTime
在控件中创建一个并行任务队列:
Calling setTime
creates a parallel Task Queue in the control:
ControlFlow
| TaskQueue
| | Task<Run fit("multiple control flows") in control flow>
| | | TaskQueue
| | | | Task <logFromPromise('0');>
| TaskQueue
| | Task <setTimeout>
在打印 0
后,Protractor 认为测试完成".在这个例子中,1
可能会在测试完成后被打印出来.要让量角器等待Task <setTimeout>
,需要调用done函数:
Protractor thinks the test is "done" after 0
is printed. In this example, 1
will probably be printed after the test is completed. To make protractor wait for Task <setTimeout>
, you need to call the done function:
it('multiple control flows', function(done) {
setTimeout(function() {
logFromPromise('1').then(function() {
done();
});
});
logFromPromise('0');
});
如果可以,请在完成"测试时让量角器处理.拥有并行任务队列可能会导致测试中出现意外的竞争条件.
If you can, let protractor handle when the test is "done". Having parallel TaskQueues can lead to unexpected race conditions in your test.
这篇关于done() 的用途和使用方法(量角器、茉莉花)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!