expect(await greeting.getText()).toEqual('Hello Julie!');
is the correct way to go.
await
stops execution queue and waits until first command is done before moving on to the second one, IN ORDER TO RESOLVE PROMISE. So if your function is synchronous and doesn't return a promise, why await
it?
From protractor page, .getText() returns !webdriver.promise.Promise.<string>
- long story short, it returns you a promise, thus it needs to be resolved
expect(await greeting.getText()).toEqual('Hello Julie!');
on the other hand returns undefined
(try to console.log it). And since it doesn't return promise, and there is nothing to resolve, await
is not needed
Additional example
Lets assume you want a synchronous function to take parameters:
someFunction("2" === "1", 1, await $welcomeMessage.getText());
First thing js does, is it runs each argument of the function as a separarte statement. i.e.
"2" === "1" // false
1 // 1
await $welcomeMessage.getText() // 'welcome user'
So by the time when function will be ready to be executed the overall statement will look like so
someFunction(false, 1, 'welcome user');
In other words, even though it looks like the function takes asynchronous promise which you need to wait for, in fact, this promise is resolved before it's executed. So if someFunction
itself is synchronous, it doesn't need to be awaited
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…