Search code examples
javascriptnode.jsnightmare

Pass a function to nighmarejs evaluate()


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!


Solution

  • 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);
        })