2017-04-18 106 views
1

我想重構我的測試使用Selenium webdriver和摩卡到異步/等待功能的ES7。我有下面的代碼:硒與異步/等待在JS中,找到並點擊元素

await loginPage.loginAsAdmin() 

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

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

我不明白爲什麼特別不能正常工作 - 我得到:

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

功能之前點擊方法返回webElement,你可以看到: Returned element

佈局:

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

方法:

findVisible: async (cssSelector) => { 
    let elm = await driver().findElement(by.css(cssSelector)) 
    return elm 
} 
+0

'異步/ await'是ES2017的一部分,*不* ES2016(ES7)。 –

回答

2

這裏的問題是誤認爲await是ES2017語言的關鍵字,使您可以阻止調用async功能的執行,直到返回由調用函數解析一個Promise

routePageButton()返回一個Promise,這就是爲什麼第二語法上述作品,執行被阻塞,直到Promise解析爲一個WebElement對象。

但是,在您使用在第一個例子中的語法,它試圖await上(click())不會被調用的功能,因爲Promise沒有click()功能。請注意,在第二種語法中有兩個await,但第一個中只有一個。

要你正試圖在一行做什麼,你就必須這樣做:

await (await layout.Elements.routePageButton()).click() 
+0

現在我明白了,謝謝! – petrppe

+1

'await'不是ES7的一部分,它不會阻止執行。 –

+0

感謝您的澄清,道歉,我得到這個錯誤,我很快會編輯。 – Benjamin