简体   繁体   English

量角器 browser.wait 不等待

[英]Protractor browser.wait doesn't wait

I am assuming that browser.wait should be a blocking call, but it is not working as I expected.我假设 browser.wait 应该是一个阻塞调用,但它没有按我预期的那样工作。 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);      
});

Now, because I assumed browser.wait was actually blocking, I thought that my console.log calls would be run in order;现在,因为我认为 browser.wait 实际上是阻塞的,所以我认为我的 console.log 调用会按顺序运行; 1,2,3,4,5; 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?我怎样才能让 browser.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() is not a blocking call, it schedules a command to 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.如果条件返回 webdriver.promise.Promise,轮询循环将等待它解决并使用已解决的值来评估条件是否已满足。 The resolution time for a promise is factored into whether a wait has timed out.承诺的解决时间会影响等待是否超时。

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:在此处查看用例:

You need to instruct protractor to pause until browser.wait is completed by using await您需要使用await指示量角器暂停,直到browser.wait完成

it('test case', async () => {
  await browser.wait(function() {
    console.log('1 - BeforeEach WAIT');
    return true;
  });
  console.log('2 - BeforeEach after wait');
});

Wait function will hold execution for that particular function, But JavaScript work in async way.等待函数将保持该特定函数的执行,但 JavaScript 以异步方式工作。 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.为了更好地理解它,您需要阅读角度/量角器中的承诺。

To get your code working you need to .then(function(){});为了让你的代码工作,你需要 .then(function(){}); (asking function 2 to wait until 1st complete. (要求功能 2 等到第一个完成。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM