Search code examples
javascriptangularasync-awaitprotractorangular-e2e

Why can't I use: "await .getAttribute()" in Protractor, even though it returns a promise?


I am trying to change my Protractor tests to use async/await instead of the selenium control flow, but it won't let me use await for the .getAttribute() function. All i get is this error Message: "SyntaxError: await is only valid in async function". But shouldn't .getAttribute() be async since it returns a promise?

Here is one of many examples where i get this error:

this.navBarcreator = async () => {        
    var mapArray = {}

    await element.all(by.tagName('mat-list-item')).each((elem) => {
        var tmp = await elem.getAttribute('aria-describedby')
        if (tmp != null) {
            ...
        }
    })

Solution

  • (elem) => {
        var tmp = await elem.getAttribute('aria-describedby')
        if (tmp != null) {
            ...
        }
    

    That function is not async, and it has to be async for await to work. Make your callback async and it should work.

    async (elem) => { //... }