0

I'm trying to pass a function to be called inside the nightmarejs evaluate statement and return a value. Something like this:

var thisfunction = function() {
    var tags = [];
    var list = document.querySelector('#selectSomething');
    list.forEach(function(item) {
        tags.push({
            name: item.attributes[1].value,
            id: item.attributes[2].value
        })
    });
    return tags;
};

return nightmare
    .goto('url')
    .wait('#something')
    .evaluate(function(getValues) {
        getValues();
    }, thisfunction)
    .then(function(list) {
        console.log(list);
    })

I'm getting ReferenceError: getValues is not defined. Tried different approaches with return statements in various place, but no luck.

How can this be done?

Thanks!

Seb
  • 983
  • 1
  • 11
  • 26

2 Answers2

2

I suppose that you can simply write the following code:

var thisfunction = function() {
    var tags = [];
    var list = document.querySelector('#selectSomething');
    list.forEach(function(item) {
        tags.push({
            name: item.attributes[1].value,
            id: item.attributes[2].value
        })
    });
    return tags;
};

return nightmare
    .goto('url')
    .wait('#something')
    .evaluate(thisfunction)
    .then(function(list) {
        console.log(list);
    })
vsenko
  • 1,129
  • 8
  • 20
2

Reason Behind the Behaviour

You can pass strings/numbers as parameters but not functions/object as they serialised before passing to the evaluate method.

You can checkout the explaination here

Also there is ticket on Github of users with similar experiences.

Prabodh M
  • 2,132
  • 2
  • 17
  • 23