How to reflect chaining (ControlFlow) of Protractor (WebDriver) actions in TypeScript

柔情痞子 提交于 2020-01-03 18:57:50

问题


In a protractor test it is possible to chain actions like "clear" and "sendKeys" on an Element like this:

element(by.id('myId')).clear().sendKeys('123456789')

I like the compact style of it. But why does it work?

According to the API Docs of webdriver.Element.clear() the return type of clear() is webdriver.promise.Promise.<void>

When i compile it with TypeScript (1.8.x), the compiler complains that there is no property called sendKeys() on Promise. And I think that's actually the case.

I believe this works at runtime due to the WebDriver ControlFlow Magic.

How can i extend the TypeScript Declaration File of Protractor, to reflect this ControlFlow-Magic and make my TypeScript compiler happy?


回答1:


You can cast it to the type <any> like so:

(<any> someInput.clear()).sendKeys()

Ugly, but works without the TS complaint.




回答2:


we ended up adding the function to the namespace like this

declare namespace webdriver.promise {
    interface Promise<T> {
        sendKeys(s: String);
    }
}


来源:https://stackoverflow.com/questions/37924866/how-to-reflect-chaining-controlflow-of-protractor-webdriver-actions-in-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!