From 5e3241950106cd3896e0f73e69bc7ffacaa49cf2 Mon Sep 17 00:00:00 2001 From: Sebastian Tiedtke Date: Sat, 12 Jan 2013 13:49:46 -0800 Subject: [PATCH] Stab add hooking up REST http interface with instruments api. --- app/appium.js | 41 +++++++++++++++++++++++++++++++---------- app/controller.js | 19 ++++++++++++------- app/routing.js | 11 ++++++++++- server.js | 16 +++++----------- 4 files changed, 58 insertions(+), 29 deletions(-) diff --git a/app/appium.js b/app/appium.js index 5d02e8ec5..5e5fad89b 100644 --- a/app/appium.js +++ b/app/appium.js @@ -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); diff --git a/app/controller.js b/app/controller.js index 9e4f8e4db..7bfcd60f2 100644 --- a/app/controller.js +++ b/app/controller.js @@ -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)}; diff --git a/app/routing.js b/app/routing.js index b3ae8d651..970bb3c96 100644 --- a/app/routing.js +++ b/app/routing.js @@ -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); diff --git a/server.js b/server.js index d47c96c6a..885b6285b 100644 --- a/server.js +++ b/server.js @@ -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); });