Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TestCafe loop through DOM

I'm having some trouble with accessing the DOM node of the nth items accessed from Selector in testCafe:

var element = Selector('input');
console.log(element) //logs a function - too early
var elementTest = await element();
console.log(elementTest) // logs first DOM node
var elementsCount = await element.count; // logs 5
console.log(`Elements: ${elementsCount}`);
for(let i = 0; i < elementsCount; i++) {
    const test =  await element.nth(i);
    console.log(`${i}: ${ test }`); // logs a function - too early
    const sanity = await test(); // breaks the loop
}`
like image 304
HSlone Avatar asked Oct 30 '25 13:10

HSlone


1 Answers

It is strange, it should work. Try this solution:

const elements = Selector('input');
var elementsCount = await elements.count;
for(let i = 0; i < elementsCount; i++) {
    const elementSelector = elements.nth(i);
    const sanity = await elementSelector();
}
like image 56
hdorgeval Avatar answered Nov 01 '25 05:11

hdorgeval



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!