说明量角器中的异步/等待 [英] Explain about async/ await in Protractor

查看:134
本文介绍了说明量角器中的异步/等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是量角器的新手.这个功能中的async/await如何运作?有人可以向我解释吗?

I'm new to protractor. How does async/await in this function works? Can anybody explain it to me?

it('TC_01 - Verify Home page title', async () => {
    await headerPage.waitForTitleContain('Homepage', 30000);
    await expect(headerPage.getTitle()).toEqual('Homepage');
});

推荐答案

这全部与JavaScript的异步特性有关.

This is all about asynchronous nature of JavaScript.

目前量角器提出了几种处理异步操作的方法(我没有在此处描述直接的诺言链和生成器):

Currently protractor proposes several ways to handle asynchronous actions, (I didn't describe direct promise chaining, and generators here):

1)承诺管理器/控制流

https://github.com/SeleniumHQ/selenium/wiki/WebDriverJs#控制流

这是一种抽象概念,使您的所有动作都像队列一样被一个接一个地调用.每个动作都会返回一个特殊对象-一个Promise.它代表异步操作的结果,将来会收到.

This is abstraction that makes all your actions to be called one by one, like a queue. Each action returns a special object - a Promise. It represents result of async operation, that will be received in future.

2)第二种方式-异步/等待

https://ponyfoo.com/articles/understanding- javascript-async-await#using-async-await

它是关于Promise对象的新抽象,可以轻松地将动作一个接一个地链接.优势在于,这是本机语言构造,而不是Promise Manager,它使您的代码看起来像已同步,并具有try/catch和其他熟悉的构造.

It is new abstraction around promises objects and allows to easily chain actions one by one. Advantage is this is native language construction, instead of Promise Manager, and makes your code look like synchronized, with try/catch and other familiar constructions.

您可以将等待视为暂停执行代码,直到解决从操作返回的承诺为止"

You can think of await like "suspend code execution until promise that returned from action is resolved"

但是async/await仍然可以在内部使用Promise.

But async/await still works with promises inside.

在与protractorJS一起使用异步/等待时的建议对:

1)确保您禁用了控制流/承诺管理器: https://github.com/angular/protractor/blob/master/lib/config.ts#L714 启用控制流的混合等待可能导致不可预测的结果.

1) Make sure you disabled control flow / promise manager: https://github.com/angular/protractor/blob/master/lib/config.ts#L714 Mixing awaits with enabled control flow may lead to unpredictable results.

2)不要忘记在所有异步操作之前加上await(通常是所有量角器api方法).如果您忘记这样做-无需等待的操作将不会与其他操作一起排队,因此操作顺序将被破坏

2) Do not forget to prepend ALL your async actions with await (usually this is all protractor api methods). If you will forgot to do this - action without await won't be queued with other actions, so order of actions will be broken

3)确保使用支持此功能的nodejs-至少是nodejs 7.8.x(或更高版本).如果使用TypeScript,请将编译目标设置为目标":"es2017"

3) Make sure you are using nodejs that supports this - at least nodejs 7.8.x (or newer). If TypeScript is used, set compile target as "target": "es2017"

更多阅读内容: https://github.com/angular/protractor/blob/master/docs/control-flow.md

https://github.com/SeleniumHQ/selenium/wiki/WebDriverJs#option-3-migrate-to-asyncawait

这篇关于说明量角器中的异步/等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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