Files
appium/instruments/instruments.js
T
2013-01-11 18:08:51 -08:00

114 lines
3.4 KiB
JavaScript

// Wrapper around Apple's Instruments app
//
var spawn = require('child_process').spawn;
var Instruments = function(server, app, udid, bootstrap, template) {
this.server = server;
this.app = app;
this.udid = udid;
this.bootstrap = bootstrap;
this.template = template;
this.curCommand = null;
this.curCommandId = -1;
this.commandCallbacks = [];
this.resultHandler = this.defaultResultHandler;
this.readyHandler = this.defaultReadyHandler;
this.extendServer();
};
Instruments.prototype.launch = function(cb) {
if (typeof cb !== "undefined") {
this.readyHandler = cb;
}
var args = ["-t", this.template], proc;
if (this.udid) {
args = args.concat(["-w", this.udid]);
}
args = args.concat([this.app]);
args = args.concat(["-e", "UIASCRIPT", this.bootstrap]);
args = args.concat(["-e", "UIARESULTSPATH", '/tmp']);
proc = spawn("/usr/bin/instruments", args);
var self = this;
proc.stdout.on('data', function(data) {
self.outputStreamHandler(data);
});
proc.stderr.on('data', function(data) {
self.errorStreamHandler(data)
});
proc.stderr.on('exit', function(code) {
console.log("Instruments exited with code " + code);
});
};
Instruments.prototype.sendCommand = function(cmd, cb) {
if (this.curCommand) {
cb("Command in progress");
} else {
this.curCommandId++;
this.curCommand = cmd;
this.commandCallbacks[this.curCommandId] = cb;
}
};
Instruments.prototype.extendServer = function(err, cb) {
var self = this;
this.server.get('/instruments/next_command', function(req, res) {
// add timing logic etc...
// if ( should rate limit ) {
// res.send(404, "Not Found");
// } else {
console.log("instruments asking for command, it is " + self.curCommand);
if (self.curCommand) {
res.send(self.curCommandId+"|"+self.curCommand);
} else {
res.send("NONE");
}
// }
});
this.server.get('/instruments/send_result/:commandId?/:result?', function(req, res) {
console.log(req.params);
var commandId = parseInt(req.params.commandId);
var result = req.params.result;
if (typeof commandId != "undefined" && typeof result != "undefined") {
self.curCommand = null;
self.commandCallbacks[commandId](result);
res.send('OK');
} else {
res.send('ERROR');
}
});
this.server.post('/instruments/ready', function(req, res) {
self.readyHandler()
res.send('OK');
});
};
Instruments.prototype.setResultHandler = function(handler) {
this.resultHandler = handler;
};
Instruments.prototype.defaultResultHandler = function(output) {
console.log("Got output from instruments: " + output);
};
Instruments.prototype.defaultReadyHandler = function() {
console.log("Instruments is ready and waiting!");
};
Instruments.prototype.outputStreamHandler = function(output) {
// do any kind of output nice-ification
var result = output;
// if we're ready to send output back....
console.log(output);
this.resultHandler(result);
};
Instruments.prototype.errorStreamHandler = function(output) {
console.log("Stderr: " + output);
};
module.exports = function(server, app, udid, bootstrap, template) {
return new Instruments(server, app, udid, bootstrap, template);
};