André Djo Djo
André Djo Djo

Reputation: 31

how to test text value (regexp) in a nodelist (div)

I need to run a test to check if all my div from a given class are containing a number.

the line I am writing in casper is :

'document.querySelectorAll(".line-1 .nb-follower")'.should.have.text(/[0-9]+/);

the console is returning the following error :

expected 'document.querySelectorAll(".line-1 .af-nb-follower")' to contain /[0-9]+/, but it was ""

I try to select item[0] but I get the same error..

I checked the selector in my console, and I can see my nodeList. Any ideas would be welcome !! Best, André

Upvotes: 1

Views: 43

Answers (1)

adeneo
adeneo

Reputation: 318222

You'd have to iterate, for instance

var elements   = document.querySelectorAll(".line-1 .nb-follower");

var elemArray  = [].slice.call(elements);

var allNumbers = elemArray.every(function(elem) {
    return /[0-9]+/.test.elem.textContent;
});

would test each elements text, and return true if every element contained a number between 0 and 9

In ES6

var elements   = document.querySelectorAll(".line-1 .nb-follower");
var allNumbers = [...elements].every( elem => /[0-9]+/.test.elem.textContent );

Upvotes: 1

Related Questions