generalize notion of device and kind of app

This commit is contained in:
Jonathan Lipps 2013-02-27 15:55:22 -08:00
Родитель 2a529da7dc
Коммит 532d6e88ae
2 изменённых файлов: 59 добавлений и 14 удалений

Просмотреть файл

@ -12,6 +12,7 @@ var routing = require('./routing')
, UUID = require('uuid-js')
, _ = require('underscore')
, ios = require('./ios')
, android = require('./android')
, status = require("./uiauto/lib/status");
var Appium = function(args) {
@ -37,7 +38,7 @@ var Appium = function(args) {
}
this.rest = null;
this.devices = {};
this.active = null;
this.deviceType = null;
this.device = null;
this.sessionId = null;
this.desiredCapabilities = {};
@ -91,10 +92,41 @@ Appium.prototype.start = function(desiredCaps, cb) {
}, this));
};
Appium.prototype.getDeviceType = function(desiredCaps) {
// yay for HACKS!!
if (desiredCaps.device) {
if (desiredCaps.device.indexOf('iPhone') !== -1) {
return "ios";
} else {
return "android";
}
} else if (desiredCaps.browserName) {
if (desiredCaps.browserName[0].toLowerCase() === "i") {
return "ios";
} else {
return "android";
}
}
return "ios";
};
Appium.prototype.isIos = function() {
return this.deviceType === "ios";
};
Appium.prototype.isAndroid = function() {
return this.deviceType === "android";
};
Appium.prototype.getAppExt = function() {
return this.isIos() ? ".app" : ".apk";
};
Appium.prototype.configure = function(desiredCaps, cb) {
var hasAppInCaps = (typeof desiredCaps !== "undefined" &&
typeof desiredCaps.app !== "undefined" &&
desiredCaps.app);
this.deviceType = this.getDeviceType(desiredCaps);
if (hasAppInCaps) {
if (desiredCaps.app[0] === "/") {
var appPath = desiredCaps.app
@ -121,8 +153,8 @@ Appium.prototype.configure = function(desiredCaps, cb) {
cb(err);
}
} else {
logger.error("Using local app, but didn't end in .zip or .app");
cb("Your app didn't end in .app or .zip!");
logger.error("Using local app, but didn't end in .zip or .app/.apk");
cb("Your app didn't end in .app/.apk or .zip!");
}
} else if (desiredCaps.app.substring(0, 4) === "http") {
var appUrl = desiredCaps.app;
@ -181,7 +213,7 @@ Appium.prototype.unzipLocalApp = function(localZipPath, cb) {
Appium.prototype.unzipApp = function(zipPath, cb) {
this.tempFiles.push(zipPath);
var me = this;
unzipApp(zipPath, function(err, appPath) {
unzipApp(zipPath, me.getAppExt(), function(err, appPath) {
if (err) {
cb(err, null);
} else {
@ -202,12 +234,24 @@ Appium.prototype.invoke = function() {
this.sessionId = UUID.create().hex;
logger.info('Creating new appium session ' + this.sessionId);
// in future all the blackberries go here.
this.active = 'iOS';
if (typeof this.devices[this.active] === 'undefined') {
this.devices[this.active] = ios(this.rest, this.args.app, this.args.udid, this.args.verbose, this.args.remove, this.args.warp, this.args.reset);
if (typeof this.devices[this.deviceType] === 'undefined') {
if (this.isIos()) {
this.devices[this.deviceType] = ios(this.rest, this.args.app, this.args.udid, this.args.verbose, this.args.remove, this.args.warp, this.args.reset);
} else if (this.isAndroid()) {
var androidOpts = {
rest: this.rest
, apkPath: this.args.app
, verbose: this.args.verbose
, appPackage: this.args.androidPackage
, appActivity: this.args.androidActivity
};
this.devices[this.deviceType] = android(androidOpts);
} else {
throw new Error("Tried to start a device that doesn't exist: " +
this.deviceType);
}
}
this.device = this.devices[this.active];
this.device = this.devices[this.deviceType];
this.device.start(function(err) {
me.progress++;
@ -227,14 +271,14 @@ Appium.prototype.onDeviceDie = function(code, cb) {
logger.info('Clearing out appium devices');
this.devices = [];
this.device = null;
//logger.info(this.progress + " sessions active =" + this.sessions.length);
//logger.info(this.progress + " sessions.deviceType =" + this.sessions.length);
this.sessions[this.progress] = {};
} else {
logger.info('Not clearing out appium devices');
}
if (cb) {
if (this.active !== null) {
this.active = null;
if (this.deviceType !== null) {
this.deviceType = null;
this.invoke();
}
cb(null, {status: status.codes.Success.code, value: null,

Просмотреть файл

@ -77,10 +77,11 @@ exports.testZipArchive = function(zipPath, cb) {
});
};
exports.unzipApp = function(zipPath, cb) {
exports.unzipApp = function(zipPath, appExt, cb) {
exports.unzipFile(zipPath, function(err, output) {
if (!err) {
var match = /inflating: ([^\/]+\.app)\//.exec(output);
var reg = new RegExp("inflating: (.+" + appExt + ")/?");
var match = reg.exec(output);
if (match) {
var appPath = path.resolve(path.dirname(zipPath), match[1]);
cb(null, appPath);