我应该等待expect(<async>)条件吗 [英] Should I await expect(<async>) conditions

查看:38
本文介绍了我应该等待expect(<async>)条件吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

https://www.protractortest.org/#/async-await显示了在量角器中使用 async/await 的示例,但包括以下行:

https://www.protractortest.org/#/async-await shows an example of using async/ await in protractor, but includes the line:

    expect(await greeting.getText()).toEqual('Hello Julie!');

但我不明白这怎么可能是正确的:如果 greeting.getText() 是异步的(由 await 指示),那么直到该行完成后才会知道文本.不是:

But I don't understand how that can be correct: if the greeting.getText() is async (indicated by the await), then the text won't be known until after that line has completed. Isn't:

    await expect(await greeting.getText()).toEqual('Hello Julie!');

所以我们在继续之前肯定会等待文本被获取(和检查).在这个例子中,它不是很重要(假设 expect 检查确实执行了),但总的来说,是否存在重新排序 getText() 与进一步操作的风险(例如,这可能会将其从 DOM 中删除).

So we definitely wait for the text to be obtained (and checked) before we move on. In this example, it's not too important (assuming the expect check does execute), but in general, is there any risk of re-ordering the getText() with further operations (which may remove it from the DOM, for example).

如果它只是基于浏览器的异步操作,实际上是否安全,因为它们无论如何都是串行执行的(因此 getText() 将在后续操作之前完成?

Is it in practice safe if it's only browser-based async operations, as they execute serially anyway (so the getText() will complete before a subsequent operation?

大概如果我们包含非基于浏览器的异步操作(例如,我们向服务器发出 http 请求),则无法保证排序,因此最佳实践是编写await expect(...)".toX(...)'(即使expect 表达式不是异步的也是安全的).

Presumably if we included non-browser based async operations (e.g. we did a http request to a server), then the ordering wouldn't be guaranteed, so best practice would be to write 'await expect(...).toX(...)' (which is safe even if the expect expression is not async).

当然,尽管如此,我们最终在几乎每一行的开头都加上了await",这看起来有点难看.

Of course, with all that, we end up with 'await' on the beginning of nearly every line, which looks a bit ugly.

推荐答案

引入await后Protractor真的很简单,不需要复杂化

Protractor is really simple after the introduction of await, don't need to complicate it

首先在配置文件中禁用 selenium 控制流,只需在配置文件中添加这一行:

First disable selenium control flow in config file, just add this line in config file:

SELENIUM_PROMISE_MANAGER: false

https://www.protractortest.org/#/control-flow

现在测试使用:

  it('should find an element by text input model', async function() {
    await browser.get('app/index.html#/form');

    var username =element(by.model('username'));
    await username.clear();
    await username.sendKeys('Jane Doe');

    var name = element(by.binding('username'));

    expect(await name.getText()).toEqual('Jane Doe');

  });

请注意,我没有使用 await

Note that i am not using await for

 var name = element(by.binding('username'));

这是因为在量角器中,除非您对其调用方法,否则不会触发查找元素操作

This is because in protractor the find element action is not triggered unless you call a method on it

所以你不必在那里使用 await

so you don't have to use await there

你不会得到 element not found 除非你调用它的任何方法.

you won't get element not found unless you call any method on it .

这篇关于我应该等待expect(<async>)条件吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆