我想要
- 保存远程 Web 服务器中的图像文件,以及
- 将其上传到另一台服务器
在量角器测试中。
// depend on a external module
var fs = require('fs');
// save remote file(url) to local(dest)
var download = function (url, dest) {
// let this function be async
browser.executeAsyncScript(function (url, dest, done) {
var file = fs.createWriteStream(dest);
var request = http.get(url, function (response) {
response.pipe(file);
file.on('finish', function () {
file.close(done);
});
});
}, url, dest);
};
describe('', function () {
it('', function () {
browser.get('http://...');
download('http://.../foo.jpg', 'foo.jpg'); /*** DOESN'T WORK! ***/
var absolutePath = path.resolve(__dirname, 'foo.jpg');
$('input[type=file]').sendKeys(absolutePath);
$('#uploadButton').click();
...
但这不起作用:
Stacktrace:
UnknownError: javascript error: fs is not defined
当我var fs = require('fs');
输入download
函数时,错误消息如下:
Stacktrace:
UnknownError: javascript error: require is not defined