How to reflect chaining (ControlFlow) of Protracto

2019-06-23 20:02发布

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?

2条回答
兄弟一词,经得起流年.
2楼-- · 2019-06-23 20:15

we ended up adding the function to the namespace like this

declare namespace webdriver.promise {
    interface Promise<T> {
        sendKeys(s: String);
    }
}
查看更多
一夜七次
3楼-- · 2019-06-23 20:34

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

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

Ugly, but works without the TS complaint.

查看更多
登录 后发表回答