Skip to content

Commit d6a87e7

Browse files
committed
Merge pull request #961 from uchida/kill-orphaned-processes
kill orphaned processes after test has finished
2 parents eb10a68 + 12bdeb3 commit d6a87e7

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

selendroid-standalone/src/main/java/io/selendroid/standalone/android/impl/AbstractDevice.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ public void kill(AndroidApp aut) throws AndroidDeviceException, AndroidSdkExcept
201201
CommandLine command = adbCommand("shell", "am", "force-stop", aut.getBasePackage());
202202
executeCommandQuietly(command);
203203
} finally {
204+
killProcesses(aut.getBasePackage());
204205
freeSelendroidPort();
205206
}
206207

@@ -210,6 +211,25 @@ public void kill(AndroidApp aut) throws AndroidDeviceException, AndroidSdkExcept
210211
}
211212
}
212213

214+
private void killProcesses(String packageName) {
215+
CommandLine command = adbCommand("shell", "ps");
216+
String processes = "";
217+
try {
218+
processes = ShellCommand.exec(command);
219+
} catch (ShellCommandException e) {
220+
String logMessage = String.format("Could not execute command: %s", command);
221+
log.log(Level.WARNING, logMessage, e);
222+
}
223+
224+
for (String process: processes.split("\\r\\n|\\r|\\n")) {
225+
if (process.endsWith(packageName)) {
226+
String pid = process.split("\\s+")[1];
227+
command = adbCommand("shell", "run-as", packageName, "kill", pid);
228+
executeCommandQuietly(command);
229+
}
230+
}
231+
}
232+
213233
private void freeSelendroidPort() {
214234
if (this.port == null) {
215235
// Not set

0 commit comments

Comments
 (0)