2

I'm trying to refactor my tests using Selenium webdriver and Mocha to ES7 with async/await functionality. I have got following piece of code:

await loginPage.loginAsAdmin()

/* THIS DOES NOT WORK */
//await layout.Elements.routePageButton().click()

/* THIS DOES WORK */
let a = await layout.Elements.routePageButton()
await a.click()

I don't understand why the particular does not work - I get:

TypeError: layout.Elements.routePageButton(...).click is not a function

Function before click method returns webElement, as you can see: Returned element

Layout:

routePageButton:  async () => await findVisibleElement('#route_info a')
const findVisibleElement = utils.Methods.Element.findVisible

Method:

findVisible: async (cssSelector) => {
  let elm = await driver().findElement(by.css(cssSelector))
  return elm
}
1
  • async/await is part of ES2017, not ES2016 (ES7). Commented Apr 18, 2017 at 17:48

1 Answer 1

4

The problem here is misunderstanding that await is a language keyword in ES2017 that allows you to block execution of the calling async function until a Promise returned by an invoked function resolves.

routePageButton() returns a Promise, and this is why the second syntax above works, as execution is blocked until the Promise resolves to a WebElement object.

However in the syntax you are using in the first example, the function that it is attempting to await on (click()) is never called, because a Promise does not have a click() function. Note that you have two awaits in your second syntax, but only one in your first.

To do what you are attempting to do in one line, you would have to do something like:

await (await layout.Elements.routePageButton()).click()
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the clarification, apologies that I got this wrong, I'll edit soon.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.