Use your own promise in the protractor

Is there a way to use a protractor filterthat accepts a function that returns a native Promractor type with my own function that returns a native Promise?

const myFilter = async(e: ElementFinder): Promise<boolean> => {
...
}
myElements.filter(myFilter)

This causes a TypeScript compilation error

TS2345: An argument of type '(e: ElementFinder) = → Promise' is not assigned to type '(element:> ElementFinder, index ?: number) => boolean | Promise ". The type" Promise "is not assigned to the type" boolean | Promise. Promise type is not assigned to Promise type. " Skip ". The" cancel "property is missing from the" Promise "type.

By the way, I can’t create a protractor, I promise myself, because of deprecaion ( https://github.com/SeleniumHQ/selenium/issues/2969 )

+4
source share
1 answer

Well, if this is still relevant, I started working with tests again, and this time I immediately found a solution.

These assistants open the bridge between protractor.promise.Promiseand the native Promise.

const toNativePromise = <T = void>(p: protractor.promise.Promise<T>): Promise<T> => { 
    return new Promise((res, rej) => {
        p.then(res).catch(rej)
    })
}
const toProtractorPromise = <T = void>(p: Promise<T>): protractor.promise.Promise<T> => {
    var deferred = protractor.promise.defer<T>();
    var promise = deferred.promise;
    return promise
}

Anyway, to solve your original problem, you can write something like this:

const myFilter = async(e: ElementFinder): Promise<boolean> => { ... }
const myFilterForProtractor = (e) => toProtractorPromise(myFilter(e))

0
source

Source: https://habr.com/ru/post/1682393/


All Articles