Created
April 19, 2013 15:54
-
-
Save xuru/5421256 to your computer and use it in GitHub Desktop.
node.js appium screenshot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Android.prototype.getScreenshot = function(cb) { | |
| this.proxy(["screenshot", {}], function(err, res) { | |
| var me = this; | |
| var delayTimes = 0; | |
| var remotePath = null; | |
| var localPath = "/tmp/"; | |
| if (typeof res.value !== 'undefined' && res.value.length > 0) { | |
| remotePath = res.value; | |
| localPath += path.basename(remotePath); | |
| } else { | |
| res = null; | |
| err = new Error("Unable to get screenshot file."); | |
| cb(err, res); | |
| } | |
| // Copy the file off the device | |
| var cmd = me.adb.adbCmd + ' pull "' + remotePath + '" "' + localPath + '"'; | |
| exec(cmd,function(err, stdout) { | |
| if (err) { | |
| logger.error(err); | |
| cb(err); | |
| } else { | |
| // Useful for debugging. | |
| logger.debug(stdout); | |
| cb(null); | |
| } | |
| }); | |
| var onErr = function() { | |
| delayTimes++; | |
| var next = function() { | |
| if (delayTimes <= 10) { | |
| read(onErr); | |
| } else { | |
| read(); | |
| } | |
| }; | |
| setTimeout(next, 300); | |
| }; | |
| var read = function(onErr) { | |
| var doRead = function() { | |
| fs.readFile(localPath, function read(err, data) { | |
| if (err) { | |
| if (onErr) { | |
| return onErr(); | |
| } else { | |
| res = null; | |
| err = new Error("Timed out waiting for screenshot file. " + err.toString()); | |
| } | |
| } else { | |
| var b64data = new Buffer(data).toString('base64'); | |
| res.value = b64data; | |
| } | |
| cb(err, res); | |
| }); | |
| }; | |
| doRead(); | |
| }; | |
| read(onErr); | |
| }); | |
| }; |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
error: uncaughtException date=Fri Apr 19 2013 10:29:02 GMT-0500 (CDT), pid=85163, uid=501, gid=20, cwd=/Users/plaster/rep/appium, execPath=/usr/local/bin/node, version=v0.10.0, argv=[node, /Users/plaster/rep/appium/server.js, -V], rss=31633408, heapTotal=20686848, heapUsed=12435552, loadavg=[2.1650390625, 1.83251953125, 1.791015625], uptime=486412, trace=[column=19, file=/Users/plaster/rep/appium/app/android.js, function=pull, line=585, method=null, native=false, column=5, file=/Users/plaster/rep/appium/app/android.js, function=null, line=630, method=null, native=false, column=9, file=/Users/plaster/rep/appium/app/device.js, function=exports.respond, line=26, method=respond, native=false, column=16, file=/Users/plaster/rep/appium/app/android.js, function=, line=228, method=null, native=false, column=9, file=/Users/plaster/rep/appium/uiautomator/adb.js, function=, line=566, method=null, native=false, column=17, file=events.js, function=Socket.EventEmitter.emit, line=95, method=EventEmitter.emit, native=false, column=14, file=_stream_readable.js, function=, line=699, method=null, native=false, column=17, file=events.js, function=Socket.EventEmitter.emit, line=92, method=EventEmitter.emit, native=false, column=10, file=stream_readable.js, function=emitReadable, line=382, method=null, native=false, column=5, file=_stream_readable.js, function=emitReadable, line=377, method=null, native=false], stack=[TypeError: Cannot read property 'adb' of undefined, at pull (/Users/plaster/rep/appium/app/android.js:585:19), at /Users/plaster/rep/appium/app/android.js:630:5, at exports.respond (/Users/plaster/rep/appium/app/device.js:26:9), at null. (/Users/plaster/rep/appium/app/android.js:228:16), at null. (/Users/plaster/rep/appium/uiautomator/adb.js:566:9), at Socket.EventEmitter.emit (events.js:95:17), at Socket. (stream_readable.js:699:14), at Socket.EventEmitter.emit (events.js:92:17), at emitReadable (_stream_readable.js:382:10), at emitReadable (_stream_readable.js:377:5)]