Skip to content

Commit

Permalink
refactor: Update the way apps are activated on Android 7+ (#744)
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach authored May 18, 2022
1 parent 8333117 commit 2a68022
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
47 changes: 32 additions & 15 deletions lib/commands/app-management.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,39 @@ commands.queryAppState = async function queryAppState (appId) {
* @param {string} appId - Application package identifier
*/
commands.activateApp = async function activateApp (appId) {
const cmd = ['monkey',
// https://stackoverflow.com/questions/44860475/how-to-use-the-monkey-command-with-an-android-system-that-doesnt-have-physical
'--pct-syskeys', '0',
'-p', appId,
'-c', 'android.intent.category.LAUNCHER',
'1'];
let output = '';
try {
this.log.debug(`Activating '${appId}' with 'adb shell ${cmd.join(' ')}' command`);
output = await this.adb.shell(cmd);
this.log.debug(`Command stdout: ${output}`);
} catch (e) {
this.log.errorAndThrow(`Cannot activate '${appId}'. Original error: ${e.message}`);
this.log.debug(`Activating '${appId}'`);
// Fallback to Monkey in older APIs
if (await this.adb.getApiLevel() < 24) {
const cmd = ['monkey',
// https://stackoverflow.com/questions/44860475/how-to-use-the-monkey-command-with-an-android-system-that-doesnt-have-physical
'--pct-syskeys', '0',
'-p', appId,
'-c', 'android.intent.category.LAUNCHER',
'1'];
let output = '';
try {
output = await this.adb.shell(cmd);
this.log.debug(`Command stdout: ${output}`);
} catch (e) {
this.log.errorAndThrow(`Cannot activate '${appId}'. Original error: ${e.message}`);
}
if (output.includes('monkey aborted')) {
this.log.errorAndThrow(`Cannot activate '${appId}'. Are you sure it is installed?`);
}
return;
}
if (output.includes('monkey aborted')) {
this.log.errorAndThrow(`Cannot activate '${appId}'. Are you sure it is installed?`);

const stdout = await this.adb.shell([
'am', 'start',
'-a', 'android.intent.action.MAIN',
'-c', 'android.intent.category.LAUNCHER',
// FLAG_ACTIVITY_REORDER_TO_FRONT | FLAG_ACTIVITY_BROUGHT_TO_FRONT | FLAG_ACTIVITY_NEW_TASK
'-f', '0x10420000',
'-n', await this.adb.resolveLaunchableActivity(appId),
]);
this.log.debug(stdout);
if (/^error:/mi.test(stdout)) {
throw new Error(`Cannot activate '${appId}'. Original error: ${stdout}`);
}
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"@appium/base-driver": "^8.5.2",
"@appium/support": "^2.58.0",
"@babel/runtime": "^7.0.0",
"appium-adb": "^9.0.0",
"appium-adb": "^9.5.0",
"appium-chromedriver": "^5.0.1",
"asyncbox": "^2.8.0",
"axios": "^0.x",
Expand Down

0 comments on commit 2a68022

Please sign in to comment.