Last Updated: February 25, 2016
·
1.425K
· jsantix

Selenium IDE: check if all links have text

I needed to check if all links of a menu (extracted from the database of a CMS) had their corresponding link text properly set, because the final contributor of the website could forget to set that field when creating a new page.

My first thought was to use the CSS :empty selector, but it doesn't match elements with a simple whitespace (sight...), so I had to use a javascript snippet instead.
But I realized that it wouldn't be an easy task, as the command verifyEval is too restrictive. I cannot use jQuery for example, nor can I use functions, loops or other useful structures, but just a simple javascript expression to eval.

So, after 1 hour spent googling, I succeeded creating the expression I needed:


Array.prototype.slice.call(document.querySelectorAll("#menu ul.col a")).every(function(e){return e.textContent.trim().length > 0})

What I'm doing there is:

  1. Take every element that satisfies the CSS selector with document.querySelectorAll()
  2. As the previous function returns a NodeList object, I need to convert it to an array so I can work properly with it. That's done with the Array.prototype.slice.call() method.
  3. Now I need to check if every element in the array meet certain condition, so I take advantage of the Array.every() method for that purpose.
  4. Finally the easy stuff: a callback for the Array.every() method that returns a boolean indicating whether the text of the element is present or not.

Regards!