exports.flickElement = function (req, res) {

  var element = req.body.element

    , xoffset = req.body.xoffset

    , yoffset = req.body.yoffset

    , speed = req.body.speed;

 

  if (checkMissingParams(req, res, {element: element, xoffset: xoffset, yoffset: yoffset})) {

    req.device.fakeFlickElement(element, xoffset, yoffset, speed, getResponseHandler(req, res));

  }

};

 

exports.execute = function (req, res) {

  var script = req.body.script

    , args = req.body.args;

 

  if (checkMissingParams(req, res, {script: script, args: args})) {

    if (_s.startsWith(script, "mobile: ")) {

      var realCmd = script.replace("mobile: ", "");

      exports.executeMobileMethod(req, res, realCmd);

    } else {

      req.device.execute(script, args, getResponseHandler(req, res));

    }

  }

};

 

exports.executeAsync = function (req, res) {

  var script = req.body.script

    , args = req.body.args

    , responseUrl = '';

 

  responseUrl += 'http://' + req.appium.args.callbackAddress + ':' + req.appium.args.callbackPort;

  responseUrl += '/wd/hub/session/' + req.appium.sessionId + '/receive_async_response';

 

  if (checkMissingParams(req, res, {script: script, args: args})) {

    req.device.executeAsync(script, args, responseUrl, getResponseHandler(req, res));

  }

};

 

exports.executeMobileMethod = function (req, res, cmd) {

  var args = req.body.args

    , params = {};

 

  var suppMethods = req.device.mobileMethodsSupported;

  if (suppMethods && !_.contains(suppMethods, cmd)) {

    return respondError(req, res, status.codes.UnknownCommand.code,

      new Error("That device doesn't know how to respond to 'mobile: '" +

                cmd + "--it's probably not using Appium's API"));

  }

 

  if (args.length) {

    if (args.length !== 1) {

      safely(req, function () {

        res.status(400).send("Mobile methods only take one parameter, which is a " +

                             "hash of named parameters to send to the method");

      });

    } else {

      params = args[0];

    }

  }

 

  if (_.has(mobileCmdMap, cmd)) {

    req.body = params;

    mobileCmdMap[cmd](req, res);

  } else {

    logger.debug("Tried to execute non-existent mobile command '" + cmd + "'" +

                ". Most mobile commands have been ported to official client " +

                "library methods. Please check your Appium library for more " +

                "information and documentation");

    notYetImplemented(req, res);

  }

};

 

exports.title = function (req, res) {

  req.device.title(getResponseHandler(req, res));

};

 

exports.submit = function (req, res) {

  var elementId = req.params.elementId;

  req.device.submit(elementId, getResponseHandler(req, res));

};

 

exports.postUrl = function (req, res) {

  var url = req.body.url;

 

  if (checkMissingParams(req, res, {url: url})) {

    req.device.url(url, getResponseHandler(req, res));

  }

};

 

exports.getUrl = function (req, res) {

  req.device.getUrl(getResponseHandler(req, res));

};

 

exports.active = function (req, res) {

  req.device.active(getResponseHandler(req, res));

};

 

exports.setContext = function (req, res) {

  var name = req.body.name;

 

  if (checkMissingParams(req, res, {name: name})) {

    req.device.setContext(name, getResponseHandler(req, res));

  }

};

 

exports.getCurrentContext = function (req, res) {

  req.device.getCurrentContext(getResponseHandler(req, res));

};

 

exports.getContexts = function (req, res) {

  req.device.getContexts(getResponseHandler(req, res));

};

 

exports.getWindowHandle = function (req, res) {

  req.device.getWindowHandle(getResponseHandler(req, res));

};

 

exports.setWindow = function (req, res) {

  var name = req.body.name;

 

  if (checkMissingParams(req, res, {name: name})) {

    req.device.setWindow(name, getResponseHandler(req, res));

  }

};

 

exports.closeWindow = function (req, res) {

  req.device.closeWindow(getResponseHandler(req, res));

};

 

exports.getWindowHandles = function (req, res) {

  req.device.getWindowHandles(getResponseHandler(req, res));

};

 

exports.setCommandTimeout = function (req, res) {

  var timeout = req.body.timeout;

 

  if (checkMissingParams(req, res, {timeout: timeout})) {

    timeout = parseInt(timeout, 10);

    req.appium.setCommandTimeout(timeout, getResponseHandler(req, res));

  }

};

 

exports.receiveAsyncResponse = function (req, res) {

  var asyncResponse = req.body;

  req.device.receiveAsyncResponse(asyncResponse);

  safely(req, function () {

    res.sendStatus(200);

  });

};

 

exports.setValueImmediate = function (req, res) {

  var element = req.params.elementId

    , value = req.body.value;

  if (checkMissingParams(req, res, {element: element, value: value})) {

    req.device.setValueImmediate(element, value, getResponseHandler(req, res));

  }

};

 

exports.getCookies = function (req, res) {

  req.device.getCookies(getResponseHandler(req, res));

};

 

exports.setCookie = function (req, res) {

  var cookie = req.body.cookie;

  if (checkMissingParams(req, res, {cookie: cookie})) {

    if (typeof cookie.name !== "string" || typeof cookie.value !== "string") {

      return respondError(req, res, status.codes.UnknownError,

          "setCookie requires cookie of form {name: 'xxx', value: 'yyy'}");

    }

    req.device.setCookie(cookie, getResponseHandler(req, res));

  }

};

 

exports.deleteCookie = function (req, res) {

  var cookie = req.params.name;

  req.device.deleteCookie(cookie, getResponseHandler(req, res));

};

 

exports.deleteCookies = function (req, res) {

  req.device.deleteCookies(getResponseHandler(req, res));

};

 

exports.getCurrentActivity = function (req, res) {

  req.device.getCurrentActivity(getResponseHandler(req, res));

};

 

exports.getLog = function (req, res) {

  var logType = req.body.type;

 

  if (checkMissingParams(req, res, {logType: logType})) {

    req.device.getLog(logType, getResponseHandler(req, res));

  }

};

 

exports.getLogTypes = function (req, res) {

  req.device.getLogTypes(getResponseHandler(req, res));

};

 

exports.getStrings = function (req, res) {

  req.body = _.defaults(req.body, {

    language: null,

    stringFile: null

  });

  var language = req.body.language,

      stringFile = req.body.stringFile;

  req.device.getStrings(language, stringFile, getResponseHandler(req, res));

};

 

exports.unknownCommand = function (req, res) {

  logger.debug("Responding to client that we did not find a valid resource");

  safely(req, function () {

    res.set('Content-Type', 'text/plain');

    res.status(404).send("That URL did not map to a valid JSONWP resource");

  });

};

 

exports.pushFile = function (req, res) {

  var data = req.body.data; // base64 data

  var path = req.body.path; // remote path

 

  if (checkMissingParams(req, 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(req, res, {path: path})) {

    req.device.pullFile(path, getResponseHandler(req, res));

  }

};

 

exports.pullFolder = function (req, res) {

  var path = req.body.path; // remote path

 

  if (checkMissingParams(req, res, {path: path})) {

    req.device.pullFolder(path, getResponseHandler(req, res));

  }

};

 

exports.endCoverage = function (req, res) {

  var intent = req.body.intent;

  var path = req.body.path;

 

  if (checkMissingParams(req, res, {intent: intent, path: path})) {

    req.device.endCoverage(intent, path, getResponseHandler(req, res));

  }

};

 

exports.toggleData = function (req, res) {

  req.device.toggleData(getResponseHandler(req, res));

};

 

exports.toggleFlightMode = function (req, res) {

  req.device.toggleFlightMode(getResponseHandler(req, res));

};

 

exports.toggleWiFi = function (req, res) {

  req.device.toggleWiFi(getResponseHandler(req, res));

};

 

exports.toggleLocationServices = function (req, res) {

  req.device.toggleLocationServices(getResponseHandler(req, res));

};

 

exports.notYetImplemented = notYetImplemented;

var mobileCmdMap = {

  'tap': exports.mobileTap

, 'drag': exports.mobileDrag

, 'flick': exports.mobileFlick

, 'scrollTo': exports.mobileScrollTo

, 'scroll': exports.mobileScroll

, 'longClick' : exports.touchLongClick

, 'down' : exports.touchDown

, 'up' : exports.touchUp

, 'move' : exports.touchMove

, 'pinchClose': exports.mobilePinchClose

, 'pinchOpen': exports.mobilePinchOpen

};

 

exports.produceError = function (req, res) {

  req.device.proxy("thisisnotvalidjs", getResponseHandler(req, res));

};

 

exports.crash = function () {

  throw new Error("We just tried to crash Appium!");

};

 

exports.guineaPig = function (req, res) {

  var delay = req.param('delay') ? parseInt(req.param('delay'), 10) : 0;

  setTimeout(function () {

   var params = {

      serverTime: parseInt(new Date().getTime() / 1000, 10)

    , userAgent: req.headers['user-agent']

    , comment: "None"

    };

    if (req.method === "POST") {

      params.comment = req.body.comments || params.comment;

    }

    safely(req, function () {

      res.set('Content-Type', 'text/html');

      res.cookie('guineacookie1', 'i am a cookie value', {path: '/'});

      res.cookie('guineacookie2', 'cookié2', {path: '/'});

      res.cookie('guineacookie3', 'cant access this', {

        domain: '.blargimarg.com',

        path: '/'

      });

      res.send(exports.getTemplate('guinea-pig')(params));

    });

  }, delay);

};

 

exports.welcome = function (req, res) {

  var params = { message: 'Let\'s browse!' };

  res.send(exports.getTemplate('welcome')(params));

};

 

exports.getTemplate = function (templateName) {

  return swig.compileFile(path.resolve(__dirname, "templates",

        templateName + ".html"));

};

 

exports.openNotifications = function (req, res) {

  req.device.openNotifications(getResponseHandler(req, res));

};

 

exports.availableIMEEngines = function (req, res) {

  req.device.availableIMEEngines(getResponseHandler(req, res));

};

 

exports.isIMEActivated = function (req, res) {

  req.device.isIMEActivated(getResponseHandler(req, res));

};

 

exports.getActiveIMEEngine = function (req, res) {

  req.device.getActiveIMEEngine(getResponseHandler(req, res));

};

 

exports.activateIMEEngine = function (req, res) {

  var imeId = req.body.engine;

  req.device.activateIMEEngine(imeId, getResponseHandler(req, res));

};

 

exports.deactivateIMEEngine = function (req, res) {

  req.device.deactivateIMEEngine(getResponseHandler(req, res));

};

 

exports.getNetworkConnection = function (req, res) {

  req.device.getNetworkConnection(getResponseHandler(req, res));

};

 

exports.setNetworkConnection = function (req, res) {

  var type = req.body.type || req.body.parameters.type;

  req.device.setNetworkConnection(type, getResponseHandler(req, res));

};

 

exports.getSettings = function (req, res) {

  req.device.getSettings(getResponseHandler(req, res));

};

 

exports.updateSettings = function (req, res) {

  var settings = req.body.settings || req.body.parameters.settings;

  if (checkMissingParams(req, res, {settings: settings})) {

    req.device.updateSettings(settings, getResponseHandler(req, res));

  }

};

 

 

这里面定义了一系列的方法,这些方法是一个一个单独的存在,作用就是在路由器模块中定义的一些映射,一一对应的关系,当一个url过来的时候,会调用对应的方法,这些方法就定义在控制器模块中。 
上面的方法大多数都会调用appium.js中device模块,以及都会使用getResponseHandler方法,所以我们从一个doClick方法开始讲解这两个点的作用。