Nicolae Daian
Nicolae Daian

Reputation: 1145

Function that returns Protractor "element.all" has output type Promise<string> instead of Promise<string[]> in Typescript

Please take a look at the code below. getAllGroupIds() function returns an array of ids of the "group" elements. Basically I get all the group-ids before the test action and all the group-ids after the test action and then I want to compare them.

However the below code doesn't compile because the "groupIdsAfter.filter" :

Property 'filter' does not exist on type 'string'.ts(2339)

Basically TS thinks that getAllGroupIds function returns a Promise(string). It doesn't notice that I have "element.all". "getAttribute" when used with "element.all" returns and array of strings instead of a string.

I tried to set the output of the function to Promise(string[]) but then I get a different compilation error:

Type 'Promise' is missing the following properties from type 'Promise': [Symbol.toStringTag], finallyts(2739)

    testCreateEmptyActivity2 = () => 
{
    this.page.navigateTo().then(() =>
    {
        this.getAllGroupIds().then((groupIdsBefore) =>
        {
            console.log(groupIdsBefore);

            // do something: test action

            this.getAllGroupIds().then((groupIdsAfter) =>
            {
                console.log(groupIdsAfter);

                let diff = groupIdsAfter.filter(e => !groupIdsBefore.includes(e));
                console.log(diff);
            });
        });

        browser.sleep(2000);
    });
}

private getAllGroupIds = () =>
{
    return element.all(by.css("rect.group")).getAttribute("id"); 
}

Upvotes: 0

Views: 236

Answers (1)

Yevhen Laichenkov
Yevhen Laichenkov

Reputation: 8672

This is a known issue.

You need to iterate over the array, you can do it like this:

  private getIds(): promise.Promise<string[]> { // promise is imported from protractor
    return element.all(by.css("rect.group")).map(elem => {
      if (elem) {
        return elem.getAttribute('id');
      }
    });
  }

Output will be an array of ids.

Upvotes: 1

Related Questions