Javascript 量角器 browser.wait 不等待

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27803314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:42:02  来源:igfitidea点击:

Protractor browser.wait doesn't wait

javascriptangularjstestingprotractorend-to-end

提问by Justin

I am assuming that browser.wait should be a blocking call, but it is not working as I expected. Here is my sample:

我假设 browser.wait 应该是一个阻塞调用,但它没有按我预期的那样工作。这是我的示例:

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);      
});

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;

现在,因为我认为 browser.wait 实际上是阻塞的,所以我认为我的 console.log 调用会按顺序运行;1、2、3、4、5;

The actual output I get is:

我得到的实际输出是:

2 - BeforeEach after wait  
1 - BeforeEach WAIT  
3 - IT statement  
5 - afterEach after wait  
4 - afterEach 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.

我怎样才能让 browser.wait 等待?还是我完全使用了错误的功能?我需要阻止一些事情,直到我的浏览器到达下一次调用所需的位置。

回答by alecxe

It is all about promises (actually every protractor question is about promises).

这都是关于承诺的(实际上每个量角器问题都与承诺有关)。

browser.wait()is not a blocking call, it schedules a commandto wait for a condition:

browser.wait()不是阻塞调用,它调度命令以等待条件:

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.

调度命令以等待某个用户提供的函数定义的条件成立。如果在评估等待时发生任何错误,它们将被允许传播。如果条件返回 webdriver.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).

它不会立即调用您传入的函数,它会调度一个命令并等待 promise 被解析(如果内部函数返回一个 promise)。

You can use then()to have a correct order in this case:

then()在这种情况下,您可以使用正确的顺序:

beforeEach(function() {
    browser.wait(function() {
        console.log('1 - BeforeEach WAIT');
        return true;
    }).then(function () {
        console.log('2 - BeforeEach after wait');
    });
});

See the use cases here:

在此处查看用例:

回答by Yash Jagdale

Wait function will hold execution for that particular function, But JavaScript work in async way. So sometime there might be chance your function gets executed before wait function. To understand it better you need to read Promises in angular/protractor.

等待函数将保持该特定函数的执行,但 JavaScript 以异步方式工作。因此,有时您的函数可能会在等待函数之前执行。为了更好地理解它,您需要阅读角度/量角器中的承诺。

To get your code working you need to .then(function(){}); (asking function 2 to wait until 1st complete.

为了让你的代码工作,你需要 .then(function(){}); (要求功能 2 等到第一个完成。

browser.wait(function() {
    console.log('1 - BeforeEach WAIT');
    return true;
}).then(function () {
    console.log('2 - BeforeEach after wait');
});