appium-adb-test
Version:
Android Debug Bridge interface
1,672 lines (1,360 loc) • 102 kB
JavaScript
'use strict';
var _regeneratorRuntime = require('babel-runtime/regenerator')['default'];
var _Promise = require('babel-runtime/core-js/promise')['default'];
var _getIterator = require('babel-runtime/core-js/get-iterator')['default'];
var _interopRequireDefault = require('babel-runtime/helpers/interop-require-default')['default'];
Object.defineProperty(exports, '__esModule', {
value: true
});
var _loggerJs = require('../logger.js');
var _loggerJs2 = _interopRequireDefault(_loggerJs);
var _helpersJs = require('../helpers.js');
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
var _lodash = require('lodash');
var _lodash2 = _interopRequireDefault(_lodash);
var _appiumSupport = require('appium-support');
var _net = require('net');
var _net2 = _interopRequireDefault(_net);
var _logcat = require('../logcat');
var _logcat2 = _interopRequireDefault(_logcat);
var _asyncbox = require('asyncbox');
var _teen_process = require('teen_process');
var methods = {};
methods.getAdbWithCorrectAdbPath = function callee$0$0() {
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
context$1$0.next = 2;
return _regeneratorRuntime.awrap(this.getSdkBinaryPath("adb"));
case 2:
this.executable.path = context$1$0.sent;
this.binaries.adb = this.executable.path;
return context$1$0.abrupt('return', this.adb);
case 5:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
methods.initAapt = function callee$0$0() {
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
context$1$0.next = 2;
return _regeneratorRuntime.awrap(this.getSdkBinaryPath("aapt"));
case 2:
this.binaries.aapt = context$1$0.sent;
case 3:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
methods.initZipAlign = function callee$0$0() {
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
context$1$0.next = 2;
return _regeneratorRuntime.awrap(this.getSdkBinaryPath("zipalign"));
case 2:
this.binaries.zipalign = context$1$0.sent;
case 3:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
methods.getApiLevel = function callee$0$0() {
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
if (this._apiLevel) {
context$1$0.next = 10;
break;
}
context$1$0.prev = 1;
context$1$0.next = 4;
return _regeneratorRuntime.awrap(this.shell(['getprop', 'ro.build.version.sdk']));
case 4:
this._apiLevel = context$1$0.sent;
context$1$0.next = 10;
break;
case 7:
context$1$0.prev = 7;
context$1$0.t0 = context$1$0['catch'](1);
_loggerJs2['default'].errorAndThrow('Error getting device API level. Original error: ' + context$1$0.t0.message);
case 10:
_loggerJs2['default'].debug('Device API level: ' + this._apiLevel);
return context$1$0.abrupt('return', this._apiLevel);
case 12:
case 'end':
return context$1$0.stop();
}
}, null, this, [[1, 7]]);
};
methods.getPlatformVersion = function callee$0$0() {
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
_loggerJs2['default'].info("Getting device platform version");
context$1$0.prev = 1;
context$1$0.next = 4;
return _regeneratorRuntime.awrap(this.shell(['getprop', 'ro.build.version.release']));
case 4:
return context$1$0.abrupt('return', context$1$0.sent);
case 7:
context$1$0.prev = 7;
context$1$0.t0 = context$1$0['catch'](1);
_loggerJs2['default'].errorAndThrow('Error getting device platform version. Original error: ' + context$1$0.t0.message);
case 10:
case 'end':
return context$1$0.stop();
}
}, null, this, [[1, 7]]);
};
methods.isDeviceConnected = function callee$0$0() {
var devices;
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
context$1$0.next = 2;
return _regeneratorRuntime.awrap(this.getConnectedDevices());
case 2:
devices = context$1$0.sent;
return context$1$0.abrupt('return', devices.length > 0);
case 4:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
methods.mkdir = function callee$0$0(remotePath) {
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
context$1$0.next = 2;
return _regeneratorRuntime.awrap(this.shell(['mkdir', '-p', remotePath]));
case 2:
return context$1$0.abrupt('return', context$1$0.sent);
case 3:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
methods.isValidClass = function (classString) {
// some.package/some.package.Activity
return new RegExp(/^[a-zA-Z0-9\./_]+$/).exec(classString);
};
methods.forceStop = function callee$0$0(pkg) {
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
context$1$0.next = 2;
return _regeneratorRuntime.awrap(this.shell(['am', 'force-stop', pkg]));
case 2:
return context$1$0.abrupt('return', context$1$0.sent);
case 3:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
methods.clear = function callee$0$0(pkg) {
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
context$1$0.next = 2;
return _regeneratorRuntime.awrap(this.shell(['pm', 'clear', pkg]));
case 2:
return context$1$0.abrupt('return', context$1$0.sent);
case 3:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
methods.stopAndClear = function callee$0$0(pkg) {
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
context$1$0.prev = 0;
context$1$0.next = 3;
return _regeneratorRuntime.awrap(this.forceStop(pkg));
case 3:
context$1$0.next = 5;
return _regeneratorRuntime.awrap(this.clear(pkg));
case 5:
context$1$0.next = 10;
break;
case 7:
context$1$0.prev = 7;
context$1$0.t0 = context$1$0['catch'](0);
_loggerJs2['default'].errorAndThrow('Cannot stop and clear ' + pkg + '. Original error: ' + context$1$0.t0.message);
case 10:
case 'end':
return context$1$0.stop();
}
}, null, this, [[0, 7]]);
};
methods.availableIMEs = function callee$0$0() {
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
context$1$0.prev = 0;
context$1$0.next = 3;
return _regeneratorRuntime.awrap(this.shell(['ime', 'list', '-a']));
case 3:
context$1$0.t0 = context$1$0.sent;
return context$1$0.abrupt('return', (0, _helpersJs.getIMEListFromOutput)(context$1$0.t0));
case 7:
context$1$0.prev = 7;
context$1$0.t1 = context$1$0['catch'](0);
_loggerJs2['default'].errorAndThrow('Error getting available IME\'s. Original error: ' + context$1$0.t1.message);
case 10:
case 'end':
return context$1$0.stop();
}
}, null, this, [[0, 7]]);
};
methods.enabledIMEs = function callee$0$0() {
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
context$1$0.prev = 0;
context$1$0.next = 3;
return _regeneratorRuntime.awrap(this.shell(['ime', 'list']));
case 3:
context$1$0.t0 = context$1$0.sent;
return context$1$0.abrupt('return', (0, _helpersJs.getIMEListFromOutput)(context$1$0.t0));
case 7:
context$1$0.prev = 7;
context$1$0.t1 = context$1$0['catch'](0);
_loggerJs2['default'].errorAndThrow('Error getting enabled IME\'s. Original error: ' + context$1$0.t1.message);
case 10:
case 'end':
return context$1$0.stop();
}
}, null, this, [[0, 7]]);
};
methods.enableIME = function callee$0$0(imeId) {
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
context$1$0.next = 2;
return _regeneratorRuntime.awrap(this.shell(['ime', 'enable', imeId]));
case 2:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
methods.disableIME = function callee$0$0(imeId) {
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
context$1$0.next = 2;
return _regeneratorRuntime.awrap(this.shell(['ime', 'disable', imeId]));
case 2:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
methods.setIME = function callee$0$0(imeId) {
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
context$1$0.next = 2;
return _regeneratorRuntime.awrap(this.shell(['ime', 'set', imeId]));
case 2:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
methods.defaultIME = function callee$0$0() {
var engine;
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
context$1$0.prev = 0;
context$1$0.next = 3;
return _regeneratorRuntime.awrap(this.shell(['settings', 'get', 'secure', 'default_input_method']));
case 3:
engine = context$1$0.sent;
return context$1$0.abrupt('return', engine.trim());
case 7:
context$1$0.prev = 7;
context$1$0.t0 = context$1$0['catch'](0);
_loggerJs2['default'].errorAndThrow('Error getting default IME. Original error: ' + context$1$0.t0.message);
case 10:
case 'end':
return context$1$0.stop();
}
}, null, this, [[0, 7]]);
};
methods.keyevent = function callee$0$0(keycode) {
var code;
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
code = parseInt(keycode, 10);
context$1$0.next = 3;
return _regeneratorRuntime.awrap(this.shell(['input', 'keyevent', code]));
case 3:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
methods.inputText = function callee$0$0(text) {
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
/* jshint ignore:start */
// need to escape whitespace and ( ) < > | ; & * \ ~ " '
text = text.replace('\\', '\\\\').replace('(', '\(').replace(')', '\)').replace('<', '\<').replace('>', '\>').replace('|', '\|').replace(';', '\;').replace('&', '\&').replace('*', '\*').replace('~', '\~').replace('"', '\"').replace("'", "\'").replace(' ', '%s');
/* jshint ignore:end */
context$1$0.next = 3;
return _regeneratorRuntime.awrap(this.shell(['input', 'text', text]));
case 3:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
methods.clearTextField = function callee$0$0() {
var length = arguments.length <= 0 || arguments[0] === undefined ? 100 : arguments[0];
var args, i;
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
// assumes that the EditText field already has focus
_loggerJs2['default'].debug('Clearing up to ' + length + ' characters');
if (!(length === 0)) {
context$1$0.next = 3;
break;
}
return context$1$0.abrupt('return');
case 3:
args = ['input', 'keyevent'];
for (i = 0; i < length; i++) {
// we cannot know where the cursor is in the text field, so delete both before
// and after so that we get rid of everything
// https://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_DEL
// https://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_FORWARD_DEL
args.push('67', '112');
}
context$1$0.next = 7;
return _regeneratorRuntime.awrap(this.shell(args));
case 7:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
methods.lock = function callee$0$0() {
var locked;
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
var _this = this;
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
context$1$0.next = 2;
return _regeneratorRuntime.awrap(this.isScreenLocked());
case 2:
locked = context$1$0.sent;
context$1$0.next = 5;
return _regeneratorRuntime.awrap(this.isScreenLocked());
case 5:
locked = context$1$0.sent;
if (locked) {
context$1$0.next = 14;
break;
}
_loggerJs2['default'].debug("Pressing the KEYCODE_POWER button to lock screen");
context$1$0.next = 10;
return _regeneratorRuntime.awrap(this.keyevent(26));
case 10:
context$1$0.next = 12;
return _regeneratorRuntime.awrap((0, _asyncbox.retryInterval)(10, 500, function callee$1$0() {
return _regeneratorRuntime.async(function callee$1$0$(context$2$0) {
while (1) switch (context$2$0.prev = context$2$0.next) {
case 0:
context$2$0.next = 2;
return _regeneratorRuntime.awrap(this.isScreenLocked());
case 2:
locked = context$2$0.sent;
if (!locked) {
_loggerJs2['default'].errorAndThrow("Waiting for screen to lock.");
}
case 4:
case 'end':
return context$2$0.stop();
}
}, null, _this);
}));
case 12:
context$1$0.next = 15;
break;
case 14:
_loggerJs2['default'].debug("Screen is already locked. Doing nothing.");
case 15:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
methods.back = function callee$0$0() {
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
_loggerJs2['default'].debug("Pressing the BACK button");
context$1$0.next = 3;
return _regeneratorRuntime.awrap(this.keyevent(4));
case 3:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
methods.goToHome = function callee$0$0() {
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
_loggerJs2['default'].debug("Pressing the HOME button");
context$1$0.next = 3;
return _regeneratorRuntime.awrap(this.keyevent(3));
case 3:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
methods.getAdbPath = function () {
return this.executable.path;
};
methods.getScreenOrientation = function callee$0$0() {
var stdout;
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
context$1$0.next = 2;
return _regeneratorRuntime.awrap(this.shell(['dumpsys', 'input']));
case 2:
stdout = context$1$0.sent;
return context$1$0.abrupt('return', (0, _helpersJs.getSurfaceOrientation)(stdout));
case 4:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
methods.isScreenLocked = function callee$0$0() {
var stdout, dumpsysFile;
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
context$1$0.next = 2;
return _regeneratorRuntime.awrap(this.shell(['dumpsys', 'window']));
case 2:
stdout = context$1$0.sent;
if (!process.env.APPIUM_LOG_DUMPSYS) {
context$1$0.next = 8;
break;
}
dumpsysFile = _path2['default'].resolve(process.cwd(), "dumpsys.log");
_loggerJs2['default'].debug('Writing dumpsys output to ' + dumpsysFile);
context$1$0.next = 8;
return _regeneratorRuntime.awrap(_appiumSupport.fs.writeFile(dumpsysFile, stdout));
case 8:
return context$1$0.abrupt('return', (0, _helpersJs.isShowingLockscreen)(stdout) || (0, _helpersJs.isCurrentFocusOnKeyguard)(stdout) || !(0, _helpersJs.isScreenOnFully)(stdout));
case 9:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
methods.isSoftKeyboardPresent = function callee$0$0() {
var stdout, isKeyboardShown, canCloseKeyboard, inputShownMatch, isInputViewShownMatch;
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
context$1$0.prev = 0;
context$1$0.next = 3;
return _regeneratorRuntime.awrap(this.shell(['dumpsys', 'input_method']));
case 3:
stdout = context$1$0.sent;
isKeyboardShown = false, canCloseKeyboard = false, inputShownMatch = /mInputShown=\w+/gi.exec(stdout);
if (inputShownMatch && inputShownMatch[0]) {
isKeyboardShown = inputShownMatch[0].split('=')[1] === 'true';
isInputViewShownMatch = /mIsInputViewShown=\w+/gi.exec(stdout);
if (isInputViewShownMatch && isInputViewShownMatch[0]) {
canCloseKeyboard = isInputViewShownMatch[0].split('=')[1] === 'true';
}
}
return context$1$0.abrupt('return', { isKeyboardShown: isKeyboardShown, canCloseKeyboard: canCloseKeyboard });
case 9:
context$1$0.prev = 9;
context$1$0.t0 = context$1$0['catch'](0);
_loggerJs2['default'].errorAndThrow('Error finding softkeyboard. Original error: ' + context$1$0.t0.message);
case 12:
case 'end':
return context$1$0.stop();
}
}, null, this, [[0, 9]]);
};
methods.sendTelnetCommand = function callee$0$0(command) {
var port;
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
_loggerJs2['default'].debug('Sending telnet command to device: ' + command);
context$1$0.next = 3;
return _regeneratorRuntime.awrap(this.getEmulatorPort());
case 3:
port = context$1$0.sent;
return context$1$0.abrupt('return', new _Promise(function (resolve, reject) {
var conn = _net2['default'].createConnection(port, 'localhost'),
connected = false,
readyRegex = /^OK$/m,
dataStream = "",
res = null;
conn.on('connect', function () {
_loggerJs2['default'].debug("Socket connection to device created");
});
conn.on('data', function (data) {
data = data.toString('utf8');
if (!connected) {
if (readyRegex.test(data)) {
connected = true;
_loggerJs2['default'].debug("Socket connection to device ready");
conn.write(command + "\n");
}
} else {
dataStream += data;
if (readyRegex.test(data)) {
res = dataStream.replace(readyRegex, "").trim();
res = _lodash2['default'].last(res.trim().split('\n'));
_loggerJs2['default'].debug('Telnet command got response: ' + res);
conn.write("quit\n");
}
}
});
conn.on('close', function () {
if (res === null) {
reject(new Error("Never got a response from command"));
} else {
resolve(res);
}
});
}));
case 5:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
methods.isAirplaneModeOn = function callee$0$0() {
var stdout;
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
context$1$0.next = 2;
return _regeneratorRuntime.awrap(this.shell(['settings', 'get', 'global', 'airplane_mode_on']));
case 2:
stdout = context$1$0.sent;
return context$1$0.abrupt('return', parseInt(stdout, 10) !== 0);
case 4:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
/*
* on: true (to turn on) or false (to turn off)
*/
methods.setAirplaneMode = function callee$0$0(on) {
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
context$1$0.next = 2;
return _regeneratorRuntime.awrap(this.shell(['settings', 'put', 'global', 'airplane_mode_on', on ? 1 : 0]));
case 2:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
/*
* on: true (to turn on) or false (to turn off)
*/
methods.broadcastAirplaneMode = function callee$0$0(on) {
var args;
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
args = ['am', 'broadcast', '-a', 'android.intent.action.AIRPLANE_MODE', '--ez', 'state', on ? 'true' : 'false'];
context$1$0.next = 3;
return _regeneratorRuntime.awrap(this.shell(args));
case 3:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
methods.isWifiOn = function callee$0$0() {
var stdout;
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
context$1$0.next = 2;
return _regeneratorRuntime.awrap(this.shell(['settings', 'get', 'global', 'wifi_on']));
case 2:
stdout = context$1$0.sent;
return context$1$0.abrupt('return', parseInt(stdout, 10) !== 0);
case 4:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
/*
* on: true (to turn on) or false (to turn off)
*/
methods.setWifiState = function callee$0$0(on) {
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
context$1$0.next = 2;
return _regeneratorRuntime.awrap(this.shell(['settings', 'put', 'global', 'wifi_on', on ? 1 : 0]));
case 2:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
methods.isDataOn = function callee$0$0() {
var stdout;
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
context$1$0.next = 2;
return _regeneratorRuntime.awrap(this.shell(['settings', 'get', 'global', 'mobile_data']));
case 2:
stdout = context$1$0.sent;
return context$1$0.abrupt('return', parseInt(stdout, 10) !== 0);
case 4:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
/*
* on: true (to turn on) or false (to turn off)
*/
methods.setDataState = function callee$0$0(on) {
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
context$1$0.next = 2;
return _regeneratorRuntime.awrap(this.shell(['settings', 'put', 'global', 'mobile_data', on ? 1 : 0]));
case 2:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
/*
* opts: { wifi: true/false, data true/false } (true to turn on, false to turn off)
*/
methods.setWifiAndData = function callee$0$0(_ref) {
var wifi = _ref.wifi;
var data = _ref.data;
var wifiOpts, dataOpts, opts;
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
wifiOpts = [], dataOpts = [];
if (!_lodash2['default'].isUndefined(wifi)) {
wifiOpts = ['-e', 'wifi', wifi ? 'on' : 'off'];
}
if (!_lodash2['default'].isUndefined(data)) {
dataOpts = ['-e', 'data', data ? 'on' : 'off'];
}
opts = ['am', 'start', '-n', 'io.appium.settings/.Settings'];
context$1$0.next = 6;
return _regeneratorRuntime.awrap(this.shell(opts.concat(wifiOpts, dataOpts)));
case 6:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
methods.rimraf = function callee$0$0(path) {
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
context$1$0.next = 2;
return _regeneratorRuntime.awrap(this.shell(['rm', '-rf', path]));
case 2:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
methods.push = function callee$0$0(localPath, remotePath, opts) {
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
context$1$0.next = 2;
return _regeneratorRuntime.awrap(this.adbExec(['push', localPath, remotePath], opts));
case 2:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
methods.pull = function callee$0$0(remotePath, localPath) {
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
context$1$0.next = 2;
return _regeneratorRuntime.awrap(this.adbExec(['pull', remotePath, localPath], { timeout: 60000 }));
case 2:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
methods.processExists = function callee$0$0(processName) {
var stdout, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, line, pkgColumn;
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
context$1$0.prev = 0;
if (this.isValidClass(processName)) {
context$1$0.next = 3;
break;
}
throw new Error('Invalid process name: ' + processName);
case 3:
context$1$0.next = 5;
return _regeneratorRuntime.awrap(this.shell("ps"));
case 5:
stdout = context$1$0.sent;
_iteratorNormalCompletion = true;
_didIteratorError = false;
_iteratorError = undefined;
context$1$0.prev = 9;
_iterator = _getIterator(stdout.split(/\r?\n/));
case 11:
if (_iteratorNormalCompletion = (_step = _iterator.next()).done) {
context$1$0.next = 20;
break;
}
line = _step.value;
line = line.trim().split(/\s+/);
pkgColumn = line[line.length - 1];
if (!(pkgColumn && pkgColumn.indexOf(processName) !== -1)) {
context$1$0.next = 17;
break;
}
return context$1$0.abrupt('return', true);
case 17:
_iteratorNormalCompletion = true;
context$1$0.next = 11;
break;
case 20:
context$1$0.next = 26;
break;
case 22:
context$1$0.prev = 22;
context$1$0.t0 = context$1$0['catch'](9);
_didIteratorError = true;
_iteratorError = context$1$0.t0;
case 26:
context$1$0.prev = 26;
context$1$0.prev = 27;
if (!_iteratorNormalCompletion && _iterator['return']) {
_iterator['return']();
}
case 29:
context$1$0.prev = 29;
if (!_didIteratorError) {
context$1$0.next = 32;
break;
}
throw _iteratorError;
case 32:
return context$1$0.finish(29);
case 33:
return context$1$0.finish(26);
case 34:
return context$1$0.abrupt('return', false);
case 37:
context$1$0.prev = 37;
context$1$0.t1 = context$1$0['catch'](0);
_loggerJs2['default'].errorAndThrow('Error finding if process exists. Original error: ' + context$1$0.t1.message);
case 40:
case 'end':
return context$1$0.stop();
}
}, null, this, [[0, 37], [9, 22, 26, 34], [27,, 29, 33]]);
};
methods.forwardPort = function callee$0$0(systemPort, devicePort) {
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
_loggerJs2['default'].debug('Forwarding system: ' + systemPort + ' to device: ' + devicePort);
context$1$0.next = 3;
return _regeneratorRuntime.awrap(this.adbExec(['forward', 'tcp:' + systemPort, 'tcp:' + devicePort]));
case 3:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
methods.removePortForward = function callee$0$0(systemPort) {
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
_loggerJs2['default'].debug('Removing forwarded port socket connection: ' + systemPort + ' ');
context$1$0.next = 3;
return _regeneratorRuntime.awrap(this.adbExec(['forward', '--remove', 'tcp:' + systemPort]));
case 3:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
methods.forwardAbstractPort = function callee$0$0(systemPort, devicePort) {
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
_loggerJs2['default'].debug('Forwarding system: ' + systemPort + ' to abstract device: ' + devicePort);
context$1$0.next = 3;
return _regeneratorRuntime.awrap(this.adbExec(['forward', 'tcp:' + systemPort, 'localabstract:' + devicePort]));
case 3:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
methods.ping = function callee$0$0() {
var stdout;
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
context$1$0.next = 2;
return _regeneratorRuntime.awrap(this.shell(["echo", "ping"]));
case 2:
stdout = context$1$0.sent;
if (!(stdout.indexOf("ping") === 0)) {
context$1$0.next = 5;
break;
}
return context$1$0.abrupt('return', true);
case 5:
throw new Error('ADB ping failed, returned ' + stdout);
case 6:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
methods.restart = function callee$0$0() {
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
context$1$0.prev = 0;
context$1$0.next = 3;
return _regeneratorRuntime.awrap(this.stopLogcat());
case 3:
context$1$0.next = 5;
return _regeneratorRuntime.awrap(this.restartAdb());
case 5:
context$1$0.next = 7;
return _regeneratorRuntime.awrap(this.waitForDevice(60));
case 7:
context$1$0.next = 9;
return _regeneratorRuntime.awrap(this.startLogcat());
case 9:
context$1$0.next = 14;
break;
case 11:
context$1$0.prev = 11;
context$1$0.t0 = context$1$0['catch'](0);
_loggerJs2['default'].errorAndThrow('Restart failed. Orginial error: ' + context$1$0.t0.message);
case 14:
case 'end':
return context$1$0.stop();
}
}, null, this, [[0, 11]]);
};
methods.startLogcat = function callee$0$0() {
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
if (this.logcat !== null) {
_loggerJs2['default'].errorAndThrow("Trying to start logcat capture but it's already started!");
}
this.logcat = new _logcat2['default']({
adb: this.executable,
debug: false,
debugTrace: false
});
context$1$0.next = 4;
return _regeneratorRuntime.awrap(this.logcat.startCapture());
case 4:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
methods.stopLogcat = function callee$0$0() {
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
if (!(this.logcat !== null)) {
context$1$0.next = 4;
break;
}
context$1$0.next = 3;
return _regeneratorRuntime.awrap(this.logcat.stopCapture());
case 3:
this.logcat = null;
case 4:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
methods.getLogcatLogs = function () {
if (this.logcat === null) {
_loggerJs2['default'].errorAndThrow("Can't get logcat logs since logcat hasn't started");
}
return this.logcat.getLogs();
};
methods.getPIDsByName = function callee$0$0(name) {
var stdout, pids, _iteratorNormalCompletion2, _didIteratorError2, _iteratorError2, _iterator2, _step2, line, match;
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
_loggerJs2['default'].debug('Getting all processes with ' + name);
context$1$0.prev = 1;
// ps <comm> where comm is last 15 characters of package name
if (name.length > 15) {
name = name.substr(name.length - 15);
}
context$1$0.next = 5;
return _regeneratorRuntime.awrap(this.shell(["ps"]));
case 5:
stdout = context$1$0.sent.trim();
pids = [];
_iteratorNormalCompletion2 = true;
_didIteratorError2 = false;
_iteratorError2 = undefined;
context$1$0.prev = 10;
_iterator2 = _getIterator(stdout.split("\n"));
case 12:
if (_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done) {
context$1$0.next = 24;
break;
}
line = _step2.value;
if (!(line.indexOf(name) !== -1)) {
context$1$0.next = 21;
break;
}
match = /[^\t ]+[\t ]+([0-9]+)/.exec(line);
if (!match) {
context$1$0.next = 20;
break;
}
pids.push(parseInt(match[1], 10));
context$1$0.next = 21;
break;
case 20:
throw new Error('Could not extract PID from ps output: ' + line);
case 21:
_iteratorNormalCompletion2 = true;
context$1$0.next = 12;
break;
case 24:
context$1$0.next = 30;
break;
case 26:
context$1$0.prev = 26;
context$1$0.t0 = context$1$0['catch'](10);
_didIteratorError2 = true;
_iteratorError2 = context$1$0.t0;
case 30:
context$1$0.prev = 30;
context$1$0.prev = 31;
if (!_iteratorNormalCompletion2 && _iterator2['return']) {
_iterator2['return']();
}
case 33:
context$1$0.prev = 33;
if (!_didIteratorError2) {
context$1$0.next = 36;
break;
}
throw _iteratorError2;
case 36:
return context$1$0.finish(33);
case 37:
return context$1$0.finish(30);
case 38:
return context$1$0.abrupt('return', pids);
case 41:
context$1$0.prev = 41;
context$1$0.t1 = context$1$0['catch'](1);
_loggerJs2['default'].errorAndThrow('Unable to get pids for ' + name + '. Orginial error: ' + context$1$0.t1.message);
case 44:
case 'end':
return context$1$0.stop();
}
}, null, this, [[1, 41], [10, 26, 30, 38], [31,, 33, 37]]);
};
methods.killProcessesByName = function callee$0$0(name) {
var pids, _iteratorNormalCompletion3, _didIteratorError3, _iteratorError3, _iterator3, _step3, pid;
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
context$1$0.prev = 0;
_loggerJs2['default'].debug('Attempting to kill all ' + name + ' processes');
context$1$0.next = 4;
return _regeneratorRuntime.awrap(this.getPIDsByName(name));
case 4:
pids = context$1$0.sent;
if (!(pids.length < 1)) {
context$1$0.next = 8;
break;
}
_loggerJs2['default'].info('No ' + name + ' process found to kill, continuing...');
return context$1$0.abrupt('return');
case 8:
_iteratorNormalCompletion3 = true;
_didIteratorError3 = false;
_iteratorError3 = undefined;
context$1$0.prev = 11;
_iterator3 = _getIterator(pids);
case 13:
if (_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done) {
context$1$0.next = 20;
break;
}
pid = _step3.value;
context$1$0.next = 17;
return _regeneratorRuntime.awrap(this.killProcessByPID(pid));
case 17:
_iteratorNormalCompletion3 = true;
context$1$0.next = 13;
break;
case 20:
context$1$0.next = 26;
break;
case 22:
context$1$0.prev = 22;
context$1$0.t0 = context$1$0['catch'](11);
_didIteratorError3 = true;
_iteratorError3 = context$1$0.t0;
case 26:
context$1$0.prev = 26;
context$1$0.prev = 27;
if (!_iteratorNormalCompletion3 && _iterator3['return']) {
_iterator3['return']();
}
case 29:
context$1$0.prev = 29;
if (!_didIteratorError3) {
context$1$0.next = 32;
break;
}
throw _iteratorError3;
case 32:
return context$1$0.finish(29);
case 33:
return context$1$0.finish(26);
case 34:
context$1$0.next = 39;
break;
case 36:
context$1$0.prev = 36;
context$1$0.t1 = context$1$0['catch'](0);
_loggerJs2['default'].errorAndThrow('Unable to kill ' + name + ' processes. Original error: ' + context$1$0.t1.message);
case 39:
case 'end':
return context$1$0.stop();
}
}, null, this, [[0, 36], [11, 22, 26, 34], [27,, 29, 33]]);
};
methods.killProcessByPID = function callee$0$0(pid) {
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
_loggerJs2['default'].debug('Attempting to kill process ' + pid);
context$1$0.next = 3;
return _regeneratorRuntime.awrap(this.shell(['kill', pid]));
case 3:
return context$1$0.abrupt('return', context$1$0.sent);
case 4:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
methods.broadcastProcessEnd = function callee$0$0(intent, processName) {
var start, timeoutMs;
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
// start the broadcast without waiting for it to finish.
this.broadcast(intent);
// wait for the process to end
start = Date.now();
timeoutMs = 40000;
context$1$0.prev = 3;
case 4:
if (!(Date.now() - start < timeoutMs)) {
context$1$0.next = 14;
break;
}
context$1$0.next = 7;
return _regeneratorRuntime.awrap(this.processExists(processName));
case 7:
if (!context$1$0.sent) {
context$1$0.next = 11;
break;
}
context$1$0.next = 10;
return _regeneratorRuntime.awrap((0, _asyncbox.sleep)(400));
case 10:
return context$1$0.abrupt('continue', 4);
case 11:
return context$1$0.abrupt('return');
case 14:
throw new Error('Process never died within ' + timeoutMs + ' ms');
case 17:
context$1$0.prev = 17;
context$1$0.t0 = context$1$0['catch'](3);
_loggerJs2['default'].errorAndThrow('Unable to broadcast process end. Original error: ' + context$1$0.t0.message);
case 20:
case 'end':
return context$1$0.stop();
}
}, null, this, [[3, 17]]);
};
methods.broadcast = function callee$0$0(intent) {
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
if (!this.isValidClass(intent)) {
_loggerJs2['default'].errorAndThrow('Invalid intent ' + intent);
}
_loggerJs2['default'].debug('Broadcasting: ' + intent);
context$1$0.next = 4;
return _regeneratorRuntime.awrap(this.shell(['am', 'broadcast', '-a', intent]));
case 4:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
methods.endAndroidCoverage = function callee$0$0() {
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
if (!this.instrumentProc) {
context$1$0.next = 3;
break;
}
context$1$0.next = 3;
return _regeneratorRuntime.awrap(this.instrumentProc.stop());
case 3:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
methods.instrument = function callee$0$0(pkg, activity, instrumentWith) {
var pkgActivity, stdout;
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
if (activity[0] !== ".") {
pkg = "";
}
pkgActivity = (pkg + activity).replace(/\.+/g, '.');
context$1$0.next = 4;
return _regeneratorRuntime.awrap(this.shell(['am', 'instrument', '-e', 'main_activity', pkgActivity, instrumentWith]));
case 4:
stdout = context$1$0.sent;
if (stdout.indexOf("Exception") !== -1) {
_loggerJs2['default'].errorAndThrow('Unknown exception during instrumentation. ' + ('Original error ' + stdout.split("\n")[0]));
}
case 6:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
methods.androidCoverage = function callee$0$0(instrumentClass, waitPkg, waitActivity) {
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
var _this2 = this;
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
if (!this.isValidClass(instrumentClass)) {
_loggerJs2['default'].errorAndThrow('Invalid class ' + instrumentClass);
}
return context$1$0.abrupt('return', new _Promise(function callee$1$0(resolve, reject) {
var args;
return _regeneratorRuntime.async(function callee$1$0$(context$2$0) {
while (1) switch (context$2$0.prev = context$2$0.next) {
case 0:
args = this.executable.defaultArgs.concat(['shell', 'am', 'instrument', '-e', 'coverage', 'true', '-w']).concat([instrumentClass]);
_loggerJs2['default'].debug('Collecting coverage data with: ' + [this.executable.path].concat(args).join(' '));
context$2$0.prev = 2;
// am instrument runs for the life of the app process.
this.instrumentProc = new _teen_process.SubProcess(this.executable.path, args);
context$2$0.next = 6;
return _regeneratorRuntime.awrap(this.instrumentProc.start(0));
case 6:
this.instrumentProc.on('output', function (stdout, stderr) {
if (stderr) {
reject(new Error('Failed to run instrumentation. Original error: ' + stderr));
}
});
context$2$0.next = 9;
return _regeneratorRuntime.awrap(this.waitForActivity(waitPkg, waitActivity));
case 9:
resolve();
context$2$0.next = 15;
break;
case 12:
context$2$0.prev = 12;
context$2$0.t0 = context$2$0['catch'](2);
reject(new Error('Android coverage failed. Original error: ' + context$2$0.t0.message));
case 15:
case 'end':
return context$2$0.stop();
}
}, null, _this2, [[2, 12]]);
}));
case 2:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
methods.getDeviceProperty = function callee$0$0(property) {
var stdout, val;
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
context$1$0.next = 2;
return _regeneratorRuntime.awrap(this.shell(['getprop', property]));
case 2:
stdout = context$1$0.sent;
val = stdout.trim();
_loggerJs2['default'].debug('Current device property \'' + property + '\': ' + val);
return context$1$0.abrupt('return', val);
case 6:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
methods.setDeviceProperty = function callee$0$0(prop, val) {
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
_loggerJs2['default'].debug('Setting device property \'' + prop + '\' to \'' + val + '\'');
context$1$0.next = 3;
return _regeneratorRuntime.awrap(this.shell(['setprop', prop, val]));
case 3:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
methods.getDeviceSysLanguage = function callee$0$0() {
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
context$1$0.next = 2;
return _regeneratorRuntime.awrap(this.getDeviceProperty("persist.sys.language"));
case 2:
return context$1$0.abrupt('return', context$1$0.sent);
case 3:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
methods.setDeviceSysLanguage = function callee$0$0(language) {
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
context$1$0.next = 2;
return _regeneratorRuntime.awrap(this.setDeviceProperty("persist.sys.language", language.toLowerCase()));
case 2:
return context$1$0.abrupt('return', context$1$0.sent);
case 3:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
methods.getDeviceSysCountry = function callee$0$0() {
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
context$1$0.next = 2;
return _regeneratorRuntime.awrap(this.getDeviceProperty("persist.sys.country"));
case 2:
return context$1$0.abrupt('return', context$1$0.sent);
case 3:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
methods.setDeviceSysCountry = function callee$0$0(country) {
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
context$1$0.next = 2;
return _regeneratorRuntime.awrap(this.setDeviceProperty("persist.sys.country", country.toUpperCase()));
case 2:
return context$1$0.abrupt('return', context$1$0.sent);
case 3:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
methods.getDeviceSysLocale = function callee$0$0() {
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
context$1$0.next = 2;
return _regeneratorRuntime.awrap(this.getDeviceProperty("persist.sys.locale"));
case 2:
return context$1$0.abrupt('return', context$1$0.sent);
case 3:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
methods.setDeviceSysLocale = function callee$0$0(locale) {
return _regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
context$1$0.next = 2;
return _regeneratorRuntime.awrap(this.setDeviceProperty("persist.sys.locale", locale));
case 2: