How to reflect chaining (ControlFlow) of Protracto

2019-06-23 20:12发布

问题:

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);
    }
}