問題描述
我假設 browser.wait 應該是一個阻塞調用,但它沒有按我預期的那樣工作.這是我的示例:
I am assuming that browser.wait should be a blocking call, but it is not working as I expected. Here is my sample:
describe("browser.wait", function() {
beforeEach(function() {
browser.wait(function() {
console.log('1 - BeforeEach WAIT');
return true;
});
console.log('2 - BeforeEach after wait');
});
afterEach(function() {
browser.wait(function() {
console.log('4 - afterEach WAIT');
return true;
});
console.log('5 - afterEach after wait');
});
it('should probably actually wait.', function() {
console.log('3 - IT statement');
expect(1).toBe(1);
});
現在,因為我假設 browser.wait 實際上是阻塞的,所以我認為我的 console.log 調用會按順序運行;1,2,3,4,5;
Now, because I assumed browser.wait was actually blocking, I thought that my console.log calls would be run in order; 1,2,3,4,5;
我得到的實際輸出是:
2 - BeforeEach after wait
1 - BeforeEach WAIT
3 - IT statement
5 - afterEach after wait
4 - afterEach WAIT
如何讓 browser.wait 等待?還是我完全使用了錯誤的功能?我需要阻止一些事情,直到我的瀏覽器到達下一次調用所需的位置.
How can I get browser.wait to wait? Or am I using the wrong function completely? I need things to block until my browser gets to where it needs to be for the next call.
推薦答案
都是關于承諾的(實際上每個量角器問題都是關于承諾的).
It is all about promises (actually every protractor question is about promises).
browser.wait()
不是阻塞調用,它調度一個命令 等待一個條件:
安排一個命令等待一個條件成立,如定義一些用戶提供的功能.如果在評估過程中出現任何錯誤等等,他們將被允許傳播.如果有條件返回一個 webdriver.promise.Promise,輪詢循環將等待它被解析并使用解析的值來評估是否條件已經滿足.承諾的解決時間是考慮等待是否超時.
Schedules a command to wait for a condition to hold, as defined by some user supplied function. If any errors occur while evaluating the wait, they will be allowed to propagate. In the event a condition returns a webdriver.promise.Promise, the polling loop will wait for it to be resolved and use the resolved value for evaluating whether the condition has been satisfied. The resolution time for a promise is factored into whether a wait has timed out.
它不會立即調用你傳入的函數,它會安排一個命令并等待 promise 被解析(如果里面的函數返回一個 promise).
It would not call the function you are passing in immediately, it would schedule a command and wait for promise to be resolved (if the function inside returns a promise).
在這種情況下,您可以使用 then()
來獲得正確的順序:
You can use then()
to have a correct order in this case:
beforeEach(function() {
browser.wait(function() {
console.log('1 - BeforeEach WAIT');
return true;
}).then(function () {
console.log('2 - BeforeEach after wait');
});
});
在此處查看用例:
- 如何等待條件?
這篇關于量角器 browser.wait 不等待的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!