由于它是在Protractor ControlFlow文档中编写的,因此WebDriver异步调用会自动存储在控制流中,并将按定义的顺序执行。实际上,似乎这种方法只是一种语法糖,以避免明确写出“然后”链。但是当我需要将我的异步函数显式地放入Control Flow队列时?想象一下,我有一堆代码:
myAsync(xxx).then(function() {
doSomething();
return;
});
并且此代码处于Protractor / Jasmine测试的中间,因此在它上面和下面有断言;我应该做点什么:
flow.execute(myAsync);
如果是,我必须在这种情况下放置“then”部分?
答案 0 :(得分:1)
it('blah', function() {
browser.get('something');
expect(element('foo').getText()).toBe('bar');
var myAsync = function() {
// if your async function doesn't return a promise, make it one
var deferred = protractor.promise.defer()
// do some async stuff in here and then reject or fulfill with...
if (error) {
deferred.reject(error)
else {
deferred.fulfill(value);
}
return deferred.promise;
};
// hook into the controlFlow and execute the async thing so you can check after
browser.controlFlow().execute(myAsync);
expect(element('foo').getText()).toBe('baz');
// or check the return value of the promise
browser.controlFlow().execute(myAsync).then(function(result) {
expect(result).toBe('something');
});