javascript - Async function depends on a external module in Protractor test -
i want
- save image file in remote web server, ,
- upload it server
in protractor test.
// depend on external module var fs = require('fs'); // save remote file(url) local(dest) var download = function (url, dest) { // let function 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(); ...
but doesn't work:
stacktrace: unknownerror: javascript error: fs not defined
when put var fs = require('fs');
in download
function, error message below:
stacktrace: unknownerror: javascript error: require not defined
when call executeasyncscript
function pass serialized executed inside browser. function not run in context of protractor test, on browser.
you need create promise resolves when done downloading file.
// depend on external module var fs = require('fs'); describe('', function () { // save remote file(url) local(dest) var download = function (url, dest) { // create promise 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(); // file has been read, resolve promise d. fulfill(); }); }); // return promise d.promise; }; it('', function () { browser.get('http://...'); // file , wait promise resolve move on download('http://.../foo.jpg', 'foo.jpg').then(function() { // make sure specify path can write , read file. var absolutepath = path.resolve(__dirname, 'foo.jpg'); $('input[type=file]').sendkeys(absolutepath); $('#uploadbutton').click(); ... });
let me know if works
here documentation: https://code.google.com/p/selenium/wiki/webdriverjs#deferred_objects
Comments
Post a Comment