Stab add hooking up REST http interface with instruments api.

This commit is contained in:
Sebastian Tiedtke
2013-01-12 13:49:46 -08:00
parent 8dc55b7885
commit 5e32419501
4 changed files with 58 additions and 29 deletions
+31 -10
View File
@@ -1,32 +1,53 @@
// Appium webserver controller methods
// https://github.com/hugs/appium/blob/master/appium/appium.py
var routing = require('./routing');
var routing = require('./routing')
, path = require('path')
, instruments = require('../instruments/instruments');
var Appium = function(app, uuid, verbose) {
this.app = app;
this.uuid = uuid;
this.verbose = verbose;
this.instrumentsProcess = null;
this.instruments = null;
this.rest = null;
this.sessionId = null;
};
Appium.prototype.attachTo = function(rest, cb) {
this.rest = rest;
// Import the routing rules
routing(rest);
routing(this);
if (cb) {
cb();
}
};
Appium.prototype.start = function(err, cb) {
console.log('The appium client start function has been called!');
if (cb) {
cb();
Appium.prototype.start = function(cb) {
if (this.sessionId === null) {
console.log('The appium client start function has been called!');
this.sessionId = new Date().getTime();
this.instruments = instruments(
this.rest
, path.resolve(__dirname, '../' + this.app)
, null
, path.resolve(__dirname, '../instruments/bootstrap_example.js')
, path.resolve(__dirname, 'uiauto/Automation.tracetemplate')
);
var me = this;
me.instruments.launch(function() {
console.log('Instruments launched. Starting command poll loop for new commands.'.yellow);
cb(null, me);
});
} else {
cb('Session already in progress', null);
}
};
Appium.prototype.stop = function(err, cb) {
Appium.prototype.stop = function(cb) {
console.log('The appium client stop function has been called!');
if (cb) {
@@ -34,7 +55,7 @@ Appium.prototype.stop = function(err, cb) {
}
};
Appium.prototype.proxy = function(err, command, cb) {
Appium.prototype.proxy = function(command, cb) {
// was thinking we should use a queue for commands instead of writing to a file
session.queue.push(command);
console.log('Pushed command to appium work queue.' + command);
+12 -7
View File
@@ -4,7 +4,7 @@
exports.status = function(req, res) {
// Build a JSON object to return to the client
var status = {
sessionId: session.sessionId || null
sessionId: appium.sessionId || null
, status: 0
, value: {
build: {
@@ -17,9 +17,14 @@ exports.status = function(req, res) {
exports.createSession = function(req, res) {
// we can talk to the appium client from here
session.client.start();
session.started = true;
res.redirect("/wd/hub/session/"+session.sessionId);
req.appium.start(function(err, instance) {
if (err) {
// of course we need to deal with err according to the WDJP spec.
throw err;
}
res.redirect("/wd/hub/session/" + instance.sessionId);
});
};
exports.getSession = function(req, res) {
@@ -43,8 +48,8 @@ exports.getSession = function(req, res) {
exports.deleteSession = function(req, res) {
var sessionId = req.params.sessionId;
session.client.stop();
session.started = false;
appium.client.stop();
appium.started = false;
var appResponse = {
sessionId: sessionId
, status: 0
@@ -60,7 +65,7 @@ exports.executeScript = function(req, res) {
var iosResponse ='';
var requestData = req.body;
try {
iosResponse = session.client.proxy(requestData.script, true);
iosResponse = appium.client.proxy(requestData.script, true);
}
catch (e) {
var errObj = {sessionId: sessionId, 'status': 13, 'value': JSON.stringify(e)};
+10 -1
View File
@@ -1,6 +1,15 @@
var controller = require('./controller');
module.exports = function(rest) {
module.exports = function(appium) {
var rest = appium.rest
, inject = function(req, res, next) {
req.appium = appium;
next();
};
// Make appium available to all REST http requests.
rest.all('/wd/*', inject);
rest.get('/wd/hub/status', controller.status);
rest.post('/wd/hub/session', controller.createSession);
rest.get('/wd/hub/session/:sessionId?', controller.getSession);
+5 -11
View File
@@ -9,12 +9,6 @@ var http = require('http')
, appium = require('./app/appium')
, parser = require('./app/parser');
session = {
sessionId: null
, client: null
, queue: []
};
rest.configure(function() {
rest.use(express.favicon());
rest.use(express.static(path.join(__dirname, '/app/static')));
@@ -27,13 +21,13 @@ rest.configure(function() {
// Parse the command line arguments
var args = parser().parseArgs();
// Instantiate the appium client
session.client = appium(args.app, args.UDID, args.verbose);
session.client.attachTo(rest);
// Instantiate the appium instance
var appium = appium(args.app, args.UDID, args.verbose);
// Hook up REST http interface
appium.attachTo(rest);
// Start the web server that receives all the commands
server.listen(args.port, args.address, function() {
session.sessionId = new Date().getTime();
var logMessage = "Appium session "+session.sessionId+" started on "+args.address+":"+args.port;
var logMessage = "Appium REST http interface listener started on "+args.address+":"+args.port;
console.log(logMessage.cyan);
});