问题描述
希望获得有关这种情况下最佳实践的一些反馈(使用带有异步/等待功能而不是SELENIUM_PROMISE_MANAGER的页面对象模型的量角器测试框架).
Hoping to get some feedback on what is the best practice in this situation (Protractor testing framework using page object model with async/await instead of SELENIUM_PROMISE_MANAGER).
假设我有一个名为setUsername
的函数,该函数只是在字段中设置用户名.我想知道是否更好的做法是使用async/await来等待函数本身中的操作或返回操作.无论哪种方式,无论何时调用该函数,都需要等待.
Let say I have a function called setUsername
which simply sets the username in a field. I'm wondering is it better practice to use async/await to await the action in the function itself or to return the action. Either way whenever the function is called it will need to be awaited.
选项1
this.setUsername = async function (username) {
await usernameInput.sendKeys(username);
}
选项2
this.setUsername = function (username) {
return usernameInput.sendKeys(username);
}
调用任一选项的语法
await loginPO.setUsername('admin');
理由::如果我使用option1,那么我声明两次等待(在func中以及在被调用时),这似乎没有必要,但是该函数的行为更加符合我的期望.如果我选择选项2,则等待仅使用一次,但是从仅需要设置值而不返回任何内容的函数中返回任何内容似乎是错误的.
Reasoning: If I go with option1 then I am declaring await twice (in func and when called), which seems unnecessary, but the function behaves more inline with what I expect. If I go with option 2 then await is only used once but it seems wrong to return anything from a function where I only need to set a value and not get anything back.
推荐答案
在我看来,最好使用选项1 ,在这里您将明确表明函数是async
,因为它具有一些需要等待的动作.
In my opinion it is better to use option 1, where you will explicit show that your function is async
because has some actions that need to be awaited.
因此,每个人都会理解,使用它需要功能来兑现承诺.另外,如果您的方法将有两个或多个需要等待的动作,则必须将函数设为async
.
So, everyone will understand that for using it function will be needed to resolve a promise.Also, if your method will have two or more actions that are needed to be awaited, so, you will have to make your function async
.
这篇关于使用页面对象模型是一种更好的做法,即当函数不返回值时,返回一个promise或在该函数中使用async/await的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!