From 6494df98d6d1749d295246b8b51332fd00e108c8 Mon Sep 17 00:00:00 2001 From: bootstraponline Date: Fri, 14 Mar 2014 14:58:15 -0400 Subject: [PATCH] Android mobile push & pull Ruby examples: mobile :pushFile, data: Base64.encode64('test ruby'), path: '/data/local/tmp/test.txt' pull_result = mobile :pullFile, path: '/data/local/tmp/test.txt' Base64.decode64 pull_result --- lib/devices/android/android-controller.js | 49 +++++++++++++++++++ lib/devices/ios/ios-controller.js | 8 +++ lib/server/controller.js | 19 +++++++ .../functional/android/apidemos/file-specs.js | 26 ++++++++++ 4 files changed, 102 insertions(+) create mode 100644 test/functional/android/apidemos/file-specs.js diff --git a/lib/devices/android/android-controller.js b/lib/devices/android/android-controller.js index 52e48ecbd..c9d68fa6c 100644 --- a/lib/devices/android/android-controller.js +++ b/lib/devices/android/android-controller.js @@ -448,6 +448,55 @@ androidController.localScreenshot = function (file, cb) { }); }; +androidController.pullFile = function (remotePath, cb) { + var localFile = temp.path({prefix: 'appium', suffix: '.tmp'}); + var b64data = ""; + + async.series([ + function (cb) { + this.adb.pull(remotePath, localFile, cb); + }.bind(this), + function (cb) { + fs.readFile(localFile, function (err, data) { + if (err) return cb(err); + b64data = new Buffer(data).toString('base64'); + cb(); + }); + }.bind(this), + ], + function (err) { + if (fs.existsSync(localFile)) fs.unlinkSync(localFile); + if (err) return cb(err); + cb(null, { + status: status.codes.Success.code + , value: b64data + }); + }); +}; + +androidController.pushFile = function (base64Data, remotePath, cb) { + var localFile = temp.path({prefix: 'appium', suffix: '.tmp'}); + + async.series([ + function (cb) { + var content = new Buffer(base64Data, 'base64'); + var fd = fs.openSync(localFile, 'w'); + fs.writeSync(fd, content, 0, content.length, 0); + fs.closeSync(fd); + + // adb push creates folders and overwrites existing files. + this.adb.push(localFile, remotePath, cb); + }.bind(this), + ], + function (err) { + if (fs.existsSync(localFile)) fs.unlinkSync(localFile); + if (err) return cb(err); + cb(null, { + status: status.codes.Success.code + }); + }); +}; + androidController.getScreenshot = function (cb) { var localfile = temp.path({prefix: 'appium', suffix: '.png'}); var b64data = ""; diff --git a/lib/devices/ios/ios-controller.js b/lib/devices/ios/ios-controller.js index a2d71f7b4..a773ec9f1 100644 --- a/lib/devices/ios/ios-controller.js +++ b/lib/devices/ios/ios-controller.js @@ -263,6 +263,14 @@ iOSController.nonSyntheticWebClick = function (elementId, cb) { }.bind(this)); }; +iOSController.pushFile = function (base64Data, remotePath, cb) { + cb(new NotYetImplementedError(), null); +}; + +iOSController.pullFile = function (remotePath, cb) { + cb(new NotYetImplementedError(), null); +}; + iOSController.touchLongClick = function (elementId, cb) { cb(new NotYetImplementedError(), null); }; diff --git a/lib/server/controller.js b/lib/server/controller.js index 85fd0c893..ec8d183dd 100644 --- a/lib/server/controller.js +++ b/lib/server/controller.js @@ -975,6 +975,23 @@ exports.localScreenshot = function (req, res) { } }; +exports.pushFile = function (req, res) { + var data = req.body.data; // base64 data + var path = req.body.path; // remote path + + if (checkMissingParams(res, {data: data, path: path})) { + req.device.pushFile(data, path, getResponseHandler(req, res)); + } +}; + +exports.pullFile = function (req, res) { + var path = req.body.path; // remote path + + if (checkMissingParams(res, {path: path})) { + req.device.pullFile(path, getResponseHandler(req, res)); + } +}; + exports.endCoverage = function (req, res) { var intent = req.body.intent; var path = req.body.path; @@ -1045,6 +1062,8 @@ var mobileCmdMap = { , 'toggleWiFi': exports.toggleWiFi , 'toggleLocationServices': exports.toggleLocationServices , 'endCoverage': exports.endCoverage +, 'pushFile': exports.pushFile +, 'pullFile': exports.pullFile }; exports.produceError = function (req, res) { diff --git a/test/functional/android/apidemos/file-specs.js b/test/functional/android/apidemos/file-specs.js new file mode 100644 index 000000000..c5d2c3472 --- /dev/null +++ b/test/functional/android/apidemos/file-specs.js @@ -0,0 +1,26 @@ +"use strict"; + +var setup = require("../../common/setup-base") + , desired = require("./desired") + , fs = require('fs'); + +describe("apidemos - push & pull file -", function () { + var driver; + setup(this, desired).then(function (d) { driver = d; }); + + it('should push and pull a file', function (done) { + var stringData = "random string data " + Math.random(); + var base64Data = new Buffer(stringData).toString('base64'); + var remotePath = '/data/local/tmp/remote.txt'; + + driver.execute("mobile: pushFile", [{data: base64Data, path: remotePath}]) + .then(function () { + return driver.execute("mobile: pullFile", [{path: remotePath}]); + }) + .then(function (remoteData64) { + var remoteData = new Buffer(remoteData64, 'base64').toString(); + remoteData.should.equal(stringData); + }) + .nodeify(done); + }); +}); \ No newline at end of file