异步功能取决于量角器测试中的外部模块
Async function depends on a external module in Protractor test
我想
- 保存远程 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
当你调用executeAsyncScript
时,你传递的函数被序列化并在浏览器中执行。该函数不会在量角器测试的上下文中运行,而是在浏览器上运行。
您需要创建一个承诺,该承诺在下载完文件后解析。
// depend on a external module
var fs = require('fs');
describe('', function () {
// save remote file(url) to local(dest)
var download = function (url, dest) {
// Create a promise that will be resolved after download.
var d = protractor.promise.defer();
var file = fs.createWriteStream(dest);
var request = http.get(url, function (response) {
response.pipe(file);
file.on('finish', function () {
file.close();
// The file has been read, resolve the promise
d. fulfill();
});
});
// Return the promise
d.promise;
};
it('', function () {
browser.get('http://...');
// Get the file and wait for the promise to resolve to move on
download('http://.../foo.jpg', 'foo.jpg').then(function() {
// Make sure you specify a path where you can write and read the file.
var absolutePath = path.resolve(__dirname, 'foo.jpg');
$('input[type=file]').sendKeys(absolutePath);
$('#uploadButton').click();
...
});
让我知道它是否有效
以下是文档:https://code.google.com/p/selenium/wiki/WebDriverJs#Deferred_Objects
相关文章:
- 使用外部模块继承Jade模板的好策略,在Express上
- 如何在RT上添加插件和使用一些外部模块/文件
- 如何将参数传递到外部模块中
- AngularJS参考工厂.来自外部模块的功能
- 在 TypeScript 中使用外部模块声明一个全局变量
- 异步功能取决于量角器测试中的外部模块
- Casperjs赢得'不允许使用外部模块
- Node.JS+Socket.io,将套接字事件处理程序移动到外部模块
- 如何用oclazyload加载的外部模块加载Angular translate
- Angular+TypeScript+外部模块.如何跨文件维护类型信息
- 使用angular js中的外部模块服务解析状态
- 类已经或正在使用名称'SafeUrl'从外部模块获取,但不能命名
- Angular Animation在路由到外部模块时出错[Cannot read property 'play&
- 击倒内部/外部模块!==短暂/单身的生活方式
- Webpack外部模块名称带连字符
- 是否可以将多个内部模块组合到一个外部模块?
- 要求/AMD外部模块分辨率
- Angular.js -不能注入外部模块
- 当类型声明为环境外部模块定义时,如何声明参数类型
- 从外部模块注入提供程序到配置块时出现未知的提供程序错误