Skip to content
This repository was archived by the owner on Jul 29, 2024. It is now read-only.
This repository was archived by the owner on Jul 29, 2024. It is now read-only.

TypeScript: ElementArrayFinder::filter uses wdpromise instead of a standard Promise, breaking backwards compatibility #4049

Closed
@jan-molak

Description

@jan-molak

Protractor 5.1.0
File: element.ts:232


Hey @sjelin!

It seems like the change in the signature of the ElementArrayFinder::filter introduced in commit 995b1466, breaks some of the more advanced filter functions.

Changing the definition of the filterFn from

(element: ElementFinder, index?: number) => boolean | wdpromise.Promise<boolean>

to

(element: ElementFinder, index?: number) => boolean | Promise<boolean>

or even

(element: ElementFinder, index?: number) => boolean | PromiseLike<boolean>

should fix the problem.


The problem in more detail.

From what I can see, the signature has changed from:

filter(
    filterFn: (element: ElementFinder, index?: number) => any
): ElementArrayFinder

to

filter(
    filterFn: (element: ElementFinder, index?: number) => boolean | wdpromise.Promise<boolean>
): ElementArrayFinder

This breaks backwards compatibility and forces any existing filterFns to now return a wdpromise.Promise, rather than a standard Promise or even better - a PromiseLike.

One example of such filterFn, coming from the Serenity/JS code base, could look like this:

const hasRequiredText      = (option: ElementFinder) => option.getText().then(value => !!~this.values.indexOf(value)),
      isAlreadySelected    = (option: ElementFinder) => option.isSelected(),
      ensureOnlyOneApplies = (list: boolean[]) => list.filter(_ => _ === true).length === 1,
      select               = (option: ElementFinder) => option.click();

const optionsToClick = (option: ElementFinder) => Promise.all([
        hasRequiredText(option),
        isAlreadySelected(option),
    ])
    .then(ensureOnlyOneApplies);

element(by.css('select'))
    .all(by.css('option'))
    .filter(optionsToClick)            // <- here's the trouble
    .each(select);

As you can see, the signature of the optionsToClick function above is

(element: ElementFinder) => Promise<boolean>

which, even though working perfectly fine with Protractor 5.0.0, no longer works with 5.1.0.

Looking forward to hearing your thoughts!

Best,
Jan

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions