我对spookyjs/casperjs还不太熟悉,我正试图找出执行流程。
这就是我要达到的目标:
加载页面
存储页面的图像
将此图像传递给函数并执行它(此过程相当长:~15秒)
等待函数返回结果
使用返回值在加载的页面中填充表单中的字段
提交表格
这段代码试图解释我提出的解决方案:

var globProcessedImage;

try {
    var Spooky = require('spooky');
} catch (e) {
    var Spooky = require('../lib/spooky');
}

var spooky = new Spooky({
    child: {
        transport: 'http'
    },
    casper: {
        logLevel: 'debug',
        verbose: true
    }
}, function (err) {
    if (err) {
        e = new Error('Failed to initialize SpookyJS');
        e.details = err;
        throw e;
    }

    spooky.start('http://example.com/');

    spooky.then(function() {
        this.captureSelector('./image.png', '#img-node');
    });

    spooky.waitFor(function() {
        this.emit('image.processed');
        return globProcessedImage !== undefined;
    }, function then() {
        processedImage = globProcessedImage;
        this.sendKeys('#imagePassword', processedImage);
    });

    spooky.then(function() {
        this.capture('./page.png');
    });

    spooky.run();

    spooky.on('image.processed', function() {
        setTimeout(function() {
            globProcessedImage = 'my_result_string';
        }, 15000);
    });
});

spooky.on('error', function (e, stack) {
    console.error(e);

    if (stack) {
        console.log(stack);
    }
});

spooky.on('log', function (log) {
    if (log.space === 'remote') {
        console.log(log.message.replace(/ \- .*/, ''));
    }
});

运行应用程序时,收到以下错误:
ReferenceError: Can't find variable: globProcessedImage
如何使globProcessedImage在Spookyjs中可见?这是在web自动化过程中处理外部功能的正确方法吗?
提前谢谢。

最佳答案

编辑:在编写本文时,spookyjs不支持按值向waitFor回调传递对象的附加语法。所以这个代码不起作用。
与casperjs相反,spookyjs有三种可能的上下文:spooky上下文、casper上下文和page上下文。
您尝试的是从casper上下文访问在怪异上下文中定义的变量。您可以在上下文之间传递变量,如本期所述:How to make global variables available to functions inside casper?
因此,根据您的代码调整它,您应该:

var globProcessedImage;

try {
    var Spooky = require('spooky');
} catch (e) {
    var Spooky = require('../lib/spooky');
}

var spooky = new Spooky({
    child: {
        transport: 'http'
    },
    casper: {
        logLevel: 'debug',
        verbose: true
    }
}, function (err) {
    if (err) {
        e = new Error('Failed to initialize SpookyJS');
        e.details = err;
        throw e;
    }

    spooky.start('http://example.com/');

    spooky.then(function() {
        this.captureSelector('./image.png', '#img-node');
        // start longRunning here
    });

    spooky.waitFor([{spookyGlobProcessedImage: globProcessedImage}, function() {
        return spookyGlobProcessedImage !== undefined;
    }], [{spookyGlobProcessedImage: globProcessedImage}, function then() {
        this.emit('image.processed', spookyGlobProcessedImage);
        processedImage = spookyGlobProcessedImage;
        this.sendKeys('#imagePassword', processedImage);
    }]);

    spooky.then(function() {
        this.capture('./page.png');
    });

    spooky.run();

    spooky.on('image.processed', function(spookyGlobProcessedImage) {
        this.echo("spookyGlobProcessedImage is " + spookyGlobProcessedImage);
    });
});

spooky.on('error', function (e, stack) {
    console.error(e);

    if (stack) {
        console.log(stack);
    }
});

spooky.on('log', function (log) {
    if (log.space === 'remote') {
        console.log(log.message.replace(/ \- .*/, ''));
    }
});

我不知道spooky.waitFor是否支持这种语法。

10-06 03:51