Skip to content

Commit

Permalink
Merge pull request appium#584 from penguinho/master
Browse files Browse the repository at this point in the history
Android Works on Windows (must use --no-reset)
  • Loading branch information
penguinho committed May 14, 2013
2 parents 3a4d156 + 8267234 commit b2bd527
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 43 deletions.
28 changes: 20 additions & 8 deletions android/adb.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ var spawn = require('win-spawn')
, async = require('async')
, ncp = require('ncp')
, mkdirp = require('mkdirp')
, _ = require('underscore');
, _ = require('underscore')
, helpers = require('../app/helpers')
, isWindows = helpers.isWindows();

var noop = function() {};

Expand Down Expand Up @@ -48,7 +50,7 @@ var ADB = function(opts, android) {
this.debugMode = true;
this.fastReset = opts.fastReset;
this.cleanApp = opts.cleanApp || this.fastReset;
this.cleanAPK = '/tmp/' + this.appPackage + '.clean.apk';
this.cleanAPK = path.resolve(helpers.getTempPath(), this.appPackage + '.clean.apk');
// This is set to true when the bootstrap jar crashes.
this.restartBootstrap = false;
// The android ref is used to resend the command that
Expand All @@ -61,10 +63,20 @@ var ADB = function(opts, android) {
ADB.prototype.checkSdkBinaryPresent = function(binary, cb) {
logger.info("Checking whether " + binary + " is present");
var binaryLoc = null;
var binaryName = binary;
if (isWindows) {
if (binaryName === "android") {
binaryName += ".bat";
} else {
if (binaryName.indexOf(".exe", binaryName.length - 4) == -1) {
binaryName += ".exe";
}
}
}
if (this.sdkRoot) {
binaryLoc = path.resolve(this.sdkRoot, "platform-tools", binary);
binaryLoc = path.resolve(this.sdkRoot, "platform-tools", binaryName);
if (!fs.existsSync(binaryLoc)) {
binaryLoc = path.resolve(this.sdkRoot, "tools", binary);
binaryLoc = path.resolve(this.sdkRoot, "tools", binaryName);
if (!fs.existsSync(binaryLoc)) {
cb(new Error("Could not find " + binary + " in tools or platform-tools; " +
"do you have android SDK installed?"),
Expand Down Expand Up @@ -178,14 +190,14 @@ ADB.prototype.insertSelendroidManifest = function(serverPath, cb) {
ADB.prototype.compileManifest = function(manifest, manifestPackage, targetPackage, cb) {
logger.info("Compiling manifest " + manifest);
var androidHome = process.env.ANDROID_HOME
, platforms = androidHome + '/platforms/'
, platforms = path.resolve(androidHome , 'platforms')
, platform = 'android-17';

// android-17 may be called android-4.2
if (!fs.existsSync(platforms + platform)) {
if (!fs.existsSync(path.resolve(platforms, platform))) {
platform = 'android-4.2';

if (!fs.existsSync(platforms + platform)) {
if (!fs.existsSync(path.resolve(platforms, platform))) {
return cb(new Error("Platform doesn't exist " + platform));
}
}
Expand All @@ -194,7 +206,7 @@ ADB.prototype.compileManifest = function(manifest, manifestPackage, targetPackag
var compileManifest = [this.binaries.aapt + ' package -M "', manifest + '"',
' --rename-manifest-package "', manifestPackage + '"',
' --rename-instrumentation-target-package "', targetPackage + '"',
' -I "', platforms + platform + '/android.jar" -F "',
' -I "', path.resolve(platforms, platform, 'android.jar') +'" -F "',
manifest, '.apk" -f'].join('');
logger.debug(compileManifest);
exec(compileManifest, {}, function(err, stdout, stderr) {
Expand Down
6 changes: 4 additions & 2 deletions app/appium.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ var routing = require('./routing')
, android = require('./android')
, selendroid = require('./selendroid')
, firefoxOs = require('./firefoxos')
, status = require("./uiauto/lib/status");
, status = require("./uiauto/lib/status")
, helpers = require('./helpers')
, isWindows = helpers.isWindows();

var Appium = function(args) {
this.args = args;
Expand Down Expand Up @@ -217,7 +219,7 @@ Appium.prototype.configureApp = function(desiredCaps, hasAppInCaps, cb) {
, isPackageOrBundle = /([a-zA-Z0-9]+\.[a-zA-Z0-9]+)+/.exec(appPath)
, origin = (hasAppInCaps ? "desiredCaps" : "command line");

if (appPath[0] === "/") {
if (appPath[0] === "/" || (isWindows && appPath!== null && appPath.length > 2 && appPath[1] === ":" && appPath[2] === "\\") ) {
this.configureLocalApp(appPath, origin, cb);
} else if (appPath.substring(0, 4) === "http") {
this.configureDownloadedApp(appPath, origin, cb);
Expand Down
86 changes: 56 additions & 30 deletions app/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ var logger = require('../logger').get('appium')
, rimraf = require('rimraf')
, exec = require('child_process').exec
, util = require('util')
, temp = require('temp');
, temp = require('temp')
, os = require('os').type()
, AdmZip = require('adm-zip');

exports.downloadFile = function(fileUrl, cb) {
// We will be downloading the files to a directory, so make sure it's there
Expand Down Expand Up @@ -40,20 +42,27 @@ exports.copyLocalZip = function(localZipPath, cb) {

exports.unzipFile = function(zipPath, cb) {
logger.info("Unzipping " + zipPath);
var execOpts = {cwd: path.dirname(zipPath)};
exports.testZipArchive(zipPath, function(err, valid) {
if (valid) {
exec('unzip -o ' + zipPath, execOpts, function(err, stderr, stdout) {
if (!err) {
logger.info("Unzip successful");
cb(null, stderr);
} else {
logger.error("Unzip threw error " + err);
logger.error("Stderr: " + stderr);
logger.error("Stdout: " + stdout);
cb("Archive could not be unzipped, check appium logs.", null);
}
});
if (exports.isWindows()) {
var zip = new AdmZip(zipPath);
zip.extractAllTo(path.dirname(zipPath), true);
logger.info("Unzip successful");
cb(null,null);
} else {
var execOpts = {cwd: path.dirname(zipPath)};
exec('unzip -o ' + zipPath, execOpts, function(err, stderr, stdout) {
if (!err) {
logger.info("Unzip successful");
cb(null, stderr);
} else {
logger.error("Unzip threw error " + err);
logger.error("Stderr: " + stderr);
logger.error("Stdout: " + stdout);
cb("Archive could not be unzipped, check appium logs.", null);
}
});
}
} else {
cb(err, null);
}
Expand All @@ -62,26 +71,35 @@ exports.unzipFile = function(zipPath, cb) {

exports.testZipArchive = function(zipPath, cb) {
logger.info("Testing zip archive: " + zipPath);
var execOpts = {cwd: path.dirname(zipPath)};
exec("unzip -t " + zipPath, execOpts, function(err, stderr, stdout) {
if (!err) {
if(/No errors detected/.exec(stderr)) {
logger.info("Zip archive tested clean");
cb(null, true);
if (exports.isWindows()) {
if (fs.existsSync(zipPath)) {
logger.info("Zip archive tested clean");
cb(null, true);
} else {
cb("Zip archive was not found.", false);
}
} else {
var execOpts = {cwd: path.dirname(zipPath)};
exec("unzip -t " + zipPath, execOpts, function(err, stderr, stdout) {
if (!err) {
if(/No errors detected/.exec(stderr)) {
logger.info("Zip archive tested clean");
cb(null, true);
} else {
logger.error("Zip file " + zipPath + " was not valid");
logger.error("Stderr: " + stderr);
logger.error("Stdout: " + stdout);
cb("Zip archive did not test successfully, check appium server logs " +
"for output", false);
}
} else {
logger.error("Zip file " + zipPath + " was not valid");
logger.error("Test zip archive threw error " + err);
logger.error("Stderr: " + stderr);
logger.error("Stdout: " + stdout);
cb("Zip archive did not test successfully, check appium server logs " +
"for output", false);
cb("Error testing zip archive, are you sure this is a zip file?", null);
}
} else {
logger.error("Test zip archive threw error " + err);
logger.error("Stderr: " + stderr);
logger.error("Stdout: " + stdout);
cb("Error testing zip archive, are you sure this is a zip file?", null);
}
});
});
}
};

exports.unzipApp = function(zipPath, appExt, cb) {
Expand Down Expand Up @@ -148,7 +166,7 @@ exports.getBuiltInAppDir = function(version, cb) {
};

exports.copyBuiltInApp = function(appPath, appName, cb) {
var newAppDir = path.resolve('/tmp/Appium-' + appName + '.app');
var newAppDir = path.resolve(exports.getTempPath() + 'Appium-' + appName + '.app');
ncp(appPath, newAppDir, function(err) {
if (err) {
cb(err);
Expand Down Expand Up @@ -291,3 +309,11 @@ exports.rotateImage = function(imgPath, deg, cb) {
cb(null);
});
};

exports.isWindows = function() {
return os === 'Windows_NT';
};

exports.getTempPath = function () {
return exports.isWindows() ? "C:\\Windows\\Temp" : "/tmp";
};
5 changes: 3 additions & 2 deletions grunt-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ var _ = require("underscore")
, parseXmlString = require('xml2js').parseString
, appiumVer = require('./package.json').version
, fs = require('fs')
, os = require('os').type();
, helpers = require('./app/helpers')
, isWindows = helpers.isWindows();

module.exports.startAppium = function(appName, verbose, readyCb, doneCb) {
var app;
Expand Down Expand Up @@ -340,7 +341,7 @@ var buildAndroidProj = function(grunt, projPath, target, cb) {
cmdName = 'mvn';
}
var whichCmd = 'which ';
if (os === 'Windows_NT') {
if (isWindows) {
whichCmd = 'where ';
}
exec(whichCmd + cmdName, function(err, stdout) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
"binary-cookies": "~0.1.1",
"namp": "~0.2.25",
"node-idevice": "~0.1.2",
"win-spawn" : "~1.1.1"
"win-spawn" : "~1.1.1",
"adm-zip" : "~0.4.3"
},
"scripts": {
"test": "grunt travis"
Expand Down

0 comments on commit b2bd527

Please sign in to comment.