【发布时间】:2014-07-04 03:30:17
【问题描述】:
我是 SpookyJS/CasperJS 的新手,我正在尝试弄清楚执行流程。
这就是我想要实现的目标:
加载页面
存储页面图像
将此图像传递给函数并执行它(此过程相当长:~15 秒)
等待函数返回结果
在加载页面中使用返回值填写表单中的一个字段
提交表格
这是一个代码 sn-p,它试图解释我想出的解决方案:
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 与 casperjs...顺便说一句,为什么这个问题被标记为 casperJS?我会删除它
-
是的,对不起。我需要先了解 CasperJS 中的全局变量以及 SpookyJs 中的差异。我只是想避免混淆。
标签: node.js global-variables webautomation spookyjs