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).
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.
option1
this.setUsername = async function (username) {
await usernameInput.sendKeys(username);
}
option2
this.setUsername = function (username) {
return usernameInput.sendKeys(username);
}
Syntax for calling either option
await loginPO.setUsername('admin');
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.
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.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
.