Assert text value of list of webelements using nightwatch.js

vibhor picture vibhor · May 12, 2015 · Viewed 16.1k times · Source

I am new to using nightwatch.js. I want to get a list of elements and verify text value of each and every element with a given string. I have tried :

function iter(elems) {
      elems.value.forEach(function(element) {
        client.elementIdValue(element.ELEMENT)
      })
    };
    client.elements('css selector', 'button.my-button.to-iterate', iter);

For another stackoverflow question But what I am using right now is

waitForElementPresent('elementcss', 5000).assert.containsText('elementcss','Hello')

and it is returning me the output

Warn: WaitForElement found 5 elements for selector "elementcss". Only the first one will be checked.

So I want that it should verify text value of each and every element of list.

Answer

Juhi Saxena picture Juhi Saxena · May 13, 2015

All the things can not be done by nightwatch js simple commands , so they have provided the custom command means selenium protocol. Here you can have all the selenium protocol. I have used following code to assert text value of each and every element with a given string "text". Hope it will help you

    module.exports = {
  '1. test if multiple elements have the same text' : function (browser) {
    function iter(elems) {
       elems.value.forEach(function(element) {
        browser.elementIdText(element.ELEMENT, function(result){
          browser.assert.equal(result.value,'text')
        })
       })
     };

    browser
      .url('file:///home/user/test.html')
      .elements('tag name', 'a', iter);

    }

  };

My HTML snippet

<div id="test">
<a href="google.com" class='red'> text </a>
<a href="#" class='red'> text </a>
<a href="#" class='red'> text 1</a>
</div>