【发布时间】:2019-07-23 17:39:28
【问题描述】:
我不确定我在 TypeScript 和 Protractor 代码中是否以正确的方式使用 async/await。如果你看到下面的代码,规范说 await 并调用页面对象,而页面对象又是 async/await。页面对象调用另一个方法,该方法又是 async/await。这是正确的实施吗?有没有最佳实践?我觉得我到处都在使用 async/await。
规格:
await login.loginTomySiteTools();
页面对象:
async loginTomySiteTools(): Promise<void> {
await this.helper.enterText(this.email, this.mySuperApp.userID);
await this.helper.click(this.btnNext);
}
助手:
async enterText(element: WebElement, textToEnter: string): Promise<void> {
await browser.sleep(1000);
await element.sendKeys(textToEnter);
}
【问题讨论】:
-
看起来不错。我不知道 Protractor 是如何工作的以及您在哪里编写
await login.loginTomySiteTools();调用,但它需要保留在async function中。不仅如此,调用async函数的函数必须知道被调用者是async并返回Promise。否则在控制权返回给调用者后,被调用者的代码将完成,对被调用者行为的期望将失败。 -
感谢@axiac 的回答
标签: typescript async-await protractor