Moshe George
Moshe George

Reputation: 310

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

Upvotes: 5

Views: 12244

Answers (3)

ntabee
ntabee

Reputation: 436

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.

Upvotes: 6

Romel P&#233;rez
Romel P&#233;rez

Reputation: 917

I used the following strategy to iterate over DOM elements using Nightwatch:

// Executing a function in the application context.
client.execute(function () {

  // Get elements by CSS selector.
  var elements = document.querySelectorAll('.elements');

  // Iterate over them.
  [].forEach.call(elements, function (element) {

    // Manipulate each element.
    element.click();
  });
});

That snippet is inside a test of course.

If you use jQuery or something similar you can use that too.

Upvotes: 2

MarkUp
MarkUp

Reputation: 1

I think the error is getting generated by your console.log() statement.

From the elements() command, allButtons.value will be an array of several objects. To access key pairs in that array, you need need to specify where in the array and then reference the object: allButtons.value[index].ELEMENT

Because you gave your .forEach() loop only one arg, it's interpreting that as the index for the array, and in my code sample below I replaced your local variable element with index for clarity. There is also no need to use the .elementIdAttribute() function; the number returned by allButtons.value[0].ELEMENT will work as the id.

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

    allButtons.value.forEach(function (index) {
    console.log('Element value is '+index.ELEMENT)
    client.elementIdClick(index.ELEMENT);}})

Hope that helps.

Upvotes: 0

Related Questions