diff --git a/lib/appium.js b/lib/appium.js index 5f4b81d02..48908381b 100644 --- a/lib/appium.js +++ b/lib/appium.js @@ -122,6 +122,19 @@ Appium.prototype.getDeviceType = function(desiredCaps) { throw new Error(errMsg); }; +Appium.prototype.getDeviceTypeFromApp = function(app) { + if (this.args.ipa) { + return "ios"; + } else if (this.args.androidPackage || this.args.avd) { + return "android"; + } else if (/\.app/.test(app)) { + return "ios"; + } else if (/\.apk/.test(app)) { + return "android"; + } + throw new Error("Could not determine your device type from --app"); +}; + Appium.prototype.isMockIos = function() { return this.deviceType === "mock_ios"; }; @@ -176,9 +189,13 @@ Appium.prototype.configure = function(desiredCaps, cb) { app = desiredCaps.app; } try { - this.deviceType = this.getDeviceType(desiredCaps); + if (this.args.launch) { + this.deviceType = this.getDeviceTypeFromApp(this.args.app); + } else { + this.deviceType = this.getDeviceType(desiredCaps); + } } catch (e) { - logger.error(e); + logger.error(e.message); return cb(e); } if (this.isMockIos()) { diff --git a/test/functional/appium/prelaunch.js b/test/functional/appium/prelaunch.js new file mode 100644 index 000000000..01eb01c69 --- /dev/null +++ b/test/functional/appium/prelaunch.js @@ -0,0 +1,53 @@ +"use strict"; +var path = require('path') + , iosApp = path.resolve(__dirname, "..", "..", "..", "sample-code", "apps", + "TestApp", "build", "Release-iphonesimulator", "TestApp.app") + , androidApp = path.resolve(__dirname, "..", "..", "..", "sample-code", + "apps", "ApiDemos", "bin", "ApiDemos-debug.apk") + , spawn = require('child_process').spawn + , crazyPort = 4799; + +var waitForLaunch = function(app, extraArgs, cb) { + var args = [".", "-p", crazyPort, "-l", "-dd", "-m", "--app", app]; + args = args.concat(extraArgs); + var proc = spawn('node', args, {cwd: path.resolve(__dirname, "..", "..", "..")}); + proc.stdout.setEncoding('utf8'); + proc.stderr.setEncoding('utf8'); + var calledBack = false; + var output = ''; + var tm = setTimeout(function() { + calledBack = true; + proc.kill(); + cb(new Error("Appium never started. Output was: " + output)); + }, 30000); + proc.stdout.on('data', function(data) { + output += data; + if (!calledBack &&/Appium REST http interface listener started on/.test(data)) { + clearTimeout(tm); + proc.kill(); + calledBack = true; + cb(); + } + }); + proc.stderr.on('data', function(data) { + output += data; + }); + proc.on('exit', function() { + if (!calledBack) { + calledBack = true; + cb(new Error("Appium never started. Output was: " + output)); + } + }); +}; + +describe('Pre-launching apps', function() { + it('should work for ios', function(done) { + waitForLaunch(iosApp, [], done); + }); + + it('should work for android', function(done) { + var args = ["--app-pkg", "com.example.android.apis", "--app-activity", + ".ApiDemos"]; + waitForLaunch(androidApp, args, done); + }); +});