Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protractor: Store ElementArrayFinder getTexts in Array and return array from method

I have a situation in protractor where I want to store ElementArrayFinder getTexts in Array and return array from method. I have written the method so far like this:

static getAllTexts(elements: ElementArrayFinder) {
    const data: string[] = [];
    elements.each(function(elem) {
        elem.getText().then(function (text) {
            data.push(text);
        });
    });
    return data;
}

Here the method is returning blank array but if I print array content inside promise, it is showing the correct data. Can anyone please help me to rewrite the method so it returns all the array data instead of returning null.

like image 235
Sitam Jana Avatar asked May 28 '26 07:05

Sitam Jana


1 Answers

static async getAllTexts(elements: ElementArrayFinder): Promise<string[]> {
    return await elements.map(async (element: ElementFinder) => {
       await element.getText();
    }
}

NOTE: you should turn off Control Flow in your protractor.conf.ts: SELENIUM_PROMISE_MANAGER: false

like image 80
Oleksii Avatar answered May 31 '26 08:05

Oleksii