Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterating elements using NightWatchJS

How do i click a button returned by elements command in night watch

client.elements('xpath', ".//a[@class='abcd')]", function (allButtons){
        console.log('Element value is '+element)
        allButtons.value.forEach(function (element) {
            this.elementIdClick(element, function(res){});
    }
}

While running i am getting an error as

Element value is [object Object]

TypeError: Object #<Object> has no method 'elementIdClick'

So how do i get each element from the element list returned by client.elements

I realized the parameters for elementIdClick is wrong, i updated the code as

client.elements('xpath', ".//a[@class='abcd')]", function (allButtons){

allButtons.value.forEach(function (element) {
    console.log('Element value is '+element)
    this.elementIdClick(this.elementIdAttribute(allButtons.value[element].ELEMENT, 'id'), function(res){});

Now the error is

Element value is [object Object]
    TypeError: Cannot read property 'ELEMENT' of undefined

So again back to original question. How do i get individual elements from a list of webelements using nightwatchJS

like image 690
Moshe George Avatar asked Dec 11 '14 20:12

Moshe George


1 Answers

The following worked for me:

function iter(elems) {
    elems.value.forEach(function(element) {
        client.elementIdClick(element.ELEMENT)
    })
};
client.elements('css selector', 'button.my-button.to-iterate', iter);
  • Each element is a JSON object of the form { ELEMENT: string } (so, has no method itself.)
  • this in forEach does not point to the element, nor client: you need to invoke client.elementIdClick() or will get a TypeError.

Hope it helps.

like image 173
ntabee Avatar answered Sep 18 '22 17:09

ntabee