admin管理员组文章数量:1434893
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:
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
}
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:
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
}
Share
Improve this question
edited Apr 18, 2017 at 17:48
Felix Kling
818k181 gold badges1.1k silver badges1.2k bronze badges
asked Apr 18, 2017 at 11:42
petrppepetrppe
1553 silver badges15 bronze badges
1
-
async/await
is part of ES2017, not ES2016 (ES7). – Felix Kling Commented Apr 18, 2017 at 17:48
1 Answer
Reset to default 4The 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 await
s 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()
本文标签: javascriptSelenium with asyncawait in JSfind and click on elementStack Overflow
版权声明:本文标题:javascript - Selenium with asyncawait in JS, find and click on element - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745621815a2666728.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论