量角器 - 在执行下一步之前等待异步承诺
Protractor - Wait for async promise before doing next
首先,我已经检查了关于这一点的各种 post 和博客,但我仍然无法弄清楚如何正确地做到这一点。
我尝试了很多不同的组合:
- 浏览器等待
- protractor.controlFlow().执行
- protractor.controlFlow().await(
...仍然没有成功..
我的问题
在我的 beforeEach 函数中,我想调用量角器承诺并等待它解决,然后再执行我的其余代码。
我的代码
我已经为任何愿意帮助我的人准备了这个简单的测试
describe('testAsync', function() {
beforeEach(function() {
console.log('beforeEach - step 1 ')
browser.get("https://angularjs.org/");
console.log('beforeEach - step 2 ')
testFunc()
console.log('beforeEach - after testFunc - step 3')
});
var testFunc = function(){
console.log("testFunc - step 1")
browser.wait(function() {
var deferred = protractor.promise.defer();
element(by.id('twitter-widget-1')).isPresent()
.then(function (isPresent) {
console.log("testFunc - step 2")
deferred.fulfill(isPresent);
});
return deferred.promise;
});
console.log("testFunc - step 3")
}
it('test after BeforeEach', function() {
console.log("Last trace")
});
});
当前输出
[launcher] Running 1 instances of WebDriver
beforeEach - step 1
beforeEach - step 2
testFunc - step 1
testFunc - step 3
beforeEach - after testFunc - step 3
testFunc - step 2
Last trace
预期输出
[launcher] Running 1 instances of WebDriver
beforeEach - step 1
beforeEach - step 2
testFunc - step 1
testFunc - step 2 // <------ This is within the promise resolve
testFunc - step 3
beforeEach - after testFunc - step 3
Last trace
testFunc
中的两个 console.log
(第 1 步和第 3 步)未包装在 promise 中,将在您调用该函数时立即触发。您的输出证明了这一点。然后你的承诺(看起来工作得很好!),returns 当承诺解决时记录第 2 步(但在日志已经触发之后)。
因此,看起来这就是您想要的? IE。您的 beforeEach
确实看起来像是在达到您的第一个规范之前触发了异步函数。
我想这会得到你想要的输出:
describe('testAsync', function() {
beforeEach(function() {
console.log('beforeEach - step 1 ');
// `get` implicitly registers a promise with the control flow
browser.get("https://angularjs.org/");
console.log('beforeEach - step 2 '); // runs "before" get above returns!
testFunc().then(function() {
// use a then to explicitly chain a dependency off a promise
console.log('beforeEach - after testFunc - step 3');
})
protractor.promise.controlFlow().execute(function() {
console.log('beforeEach - after testFunc, via controlFlow - step 4');
});
console.log('beforeEach - end of beforeEach - everything registered, nothing done');
});
var testFunc = function(){
console.log("testFunc - step 1")
// return browser wait promise to caller
// `wait` also implicitly registers with the control flow
return browser.wait(function() {
return element(by.id('twitter-widget-1')).isPresent()
.then(function (isPresent) {
console.log("testFunc - step 2")
return true; // tell wait its done by resolving then promise->element promise->wait
});
});
}
it('test after BeforeEach', function() {
console.log("Last trace")
});
});
首先,我已经检查了关于这一点的各种 post 和博客,但我仍然无法弄清楚如何正确地做到这一点。
我尝试了很多不同的组合:
- 浏览器等待
- protractor.controlFlow().执行
- protractor.controlFlow().await(
...仍然没有成功..
我的问题
在我的 beforeEach 函数中,我想调用量角器承诺并等待它解决,然后再执行我的其余代码。
我的代码
我已经为任何愿意帮助我的人准备了这个简单的测试
describe('testAsync', function() {
beforeEach(function() {
console.log('beforeEach - step 1 ')
browser.get("https://angularjs.org/");
console.log('beforeEach - step 2 ')
testFunc()
console.log('beforeEach - after testFunc - step 3')
});
var testFunc = function(){
console.log("testFunc - step 1")
browser.wait(function() {
var deferred = protractor.promise.defer();
element(by.id('twitter-widget-1')).isPresent()
.then(function (isPresent) {
console.log("testFunc - step 2")
deferred.fulfill(isPresent);
});
return deferred.promise;
});
console.log("testFunc - step 3")
}
it('test after BeforeEach', function() {
console.log("Last trace")
});
});
当前输出
[launcher] Running 1 instances of WebDriver
beforeEach - step 1
beforeEach - step 2
testFunc - step 1
testFunc - step 3
beforeEach - after testFunc - step 3
testFunc - step 2
Last trace
预期输出
[launcher] Running 1 instances of WebDriver
beforeEach - step 1
beforeEach - step 2
testFunc - step 1
testFunc - step 2 // <------ This is within the promise resolve
testFunc - step 3
beforeEach - after testFunc - step 3
Last trace
testFunc
中的两个 console.log
(第 1 步和第 3 步)未包装在 promise 中,将在您调用该函数时立即触发。您的输出证明了这一点。然后你的承诺(看起来工作得很好!),returns 当承诺解决时记录第 2 步(但在日志已经触发之后)。
因此,看起来这就是您想要的? IE。您的 beforeEach
确实看起来像是在达到您的第一个规范之前触发了异步函数。
我想这会得到你想要的输出:
describe('testAsync', function() {
beforeEach(function() {
console.log('beforeEach - step 1 ');
// `get` implicitly registers a promise with the control flow
browser.get("https://angularjs.org/");
console.log('beforeEach - step 2 '); // runs "before" get above returns!
testFunc().then(function() {
// use a then to explicitly chain a dependency off a promise
console.log('beforeEach - after testFunc - step 3');
})
protractor.promise.controlFlow().execute(function() {
console.log('beforeEach - after testFunc, via controlFlow - step 4');
});
console.log('beforeEach - end of beforeEach - everything registered, nothing done');
});
var testFunc = function(){
console.log("testFunc - step 1")
// return browser wait promise to caller
// `wait` also implicitly registers with the control flow
return browser.wait(function() {
return element(by.id('twitter-widget-1')).isPresent()
.then(function (isPresent) {
console.log("testFunc - step 2")
return true; // tell wait its done by resolving then promise->element promise->wait
});
});
}
it('test after BeforeEach', function() {
console.log("Last trace")
});
});