I have the following structure I want to query for some e2e angular test with protractor:
<div id='parentId'>
<div>First</div>
<div>Second</div>
<div>Third</div>
<div>Four</div>
</div>
Currently I want to retrieve a list of the texts of the child div elements :
['First','Second', 'Third', 'Fourth']
I remember being able to do so with protractor in Angular some time ago but I can't get it to work now.
Current code I have is:
mytest.e2e-spec.ts:
import { browser, element, ElementArrayFinder, WebElement } from 'protractor';
describe('A test here ', () => {
it('should find the texts in all four divs', async () => {
const elements: WebElement[] = await element.all(by.css('#parentId > div')).getWebElements();
let array = await Promise.all(mocks.map(async (item)=>{
let text = await item.getText();
// let text = await item.getAttribute('innerText'); = null
// let text = await item.getAttribute('innerHTML'); = null
// let text = await item.getAttribute('value'); = null
// let text = await item.getAttribute('textContent'); = null
return text;
}));
console.log(array);
}
});
The above code returns ['null', 'null', 'null', 'null']
I can get the tagName and length of the WebElement
returned array from the query properly. However, so far the text query keeps returning 'null'.
I have tried without even looping and:
await elements[0].getText();
will also produce null.
Also tried following queries instead:
const elms0: WebElement[] = await (await browser.driver.findElement(by.id('parentId'))).findElements(by.tagName('div'));
const elms: WebElement[] = await browser.driver.findElements(by.css('#parentId > div'));
they both return the length of the elements correctly, however the text returns always null or empty. I suspect this has to do with my async operation somehow racing, but I have tried all remedies out there, nothing seems to work so far. :/
Currently using protractor 7.0.0 , although the same version has been working before. Already went through related questions, I don't think there is anything I haven't tried yet.
Using chrome driver on a MacOS.
EDIT: The parent div had display:none as its styling which made it impossible to fetch the text.