Olivvv
Olivvv

Reputation: 1200

Typescript / protractor . loop through and ElementFinderArray and resolve promises

I want to do the following in protractor (but with typescript):

let cellsTxt = await Promise.all(cells.map(cell => cell.getText()));

Here I want go through an ElementArrayFinder to get the text of each element.

So far I came up with the following, which is verbose and does not work:

let cellsTxt: Array<string> = [];
let cellsTxtPromise: Array<promise.Promise<string>> = [];
cells.forEach((cell: ElementFinder, i:number) => {
  cellsTxtPromise[i] = cell.getText();
})
cellsTxt = await Promise.all(cellsTxt);

I welcome any pointer toward the right solution.

thanks!

EDIT: THIS IS NOT A RELEVANT QUESTION; SINCE THE ISSUE I FACED WAS CAUSED BY ANOTHER PIECE OF CODE.

Upvotes: 0

Views: 147

Answers (1)

yong
yong

Reputation: 13712

I thinks there are several approaches to archive same goal:

1) let cellsTxt = await cells.getText(); // you can call getText() on ElementArrayFinder

2) let cellsTxt = await cells.map(cell => await cell.getText()));

3) let cellsTxt = await Promise.all(cells.map(cell => cell.getText()));

4) let tmp = []; 
   let cellsTxt = await cells.each(cell => tmp.push( await cell.getText() ))
                             .then(()=> return tmp;)

Upvotes: 1

Related Questions