Check mobile command is executed successfully

I am using UiAutomator2 mobile command to delete files from download folder which is working file.

Map<String, Object> args = new HashMap<>();
args.put(“command”, “rm -f /sdcard/download/calculator.xlsx”);
args.put(“includeStderr”, true);
args.put(“timeout”, 5000);
getBaseMobileDriver().executeScript(“mobile: shell”, args);

how to check command executed successfully?

I’ve never used basemobiledriver before so no idea. care to share a documentation link?

why don’t you just use adb? Besides this wont work on all Android versions/devices anyway, so I’m getting the impression you might be needing to adjust your strategy more subtly the command is correct, but why not just ‘ls’ to check for file existence? That’s what I do all the time for similar problems. Just the /sdcard bit there might be a bit fragile though.

Thanks for the response Here is the documentation link.

Initially, I too decided to use adb shell command as there we have more control. like we do check file present. but I thought to give a try to the Mobile Command.

1 Like

why not use same but with

ls -R /sdcard/download/

and check output

per doc:

Returned Result

Depending on the includeStderr value this API could either return a string, which is equal to the stdout stream content of the given command or a dictionary whose elements are stdout and stderr and values are contents of the corresponding outgoing streams. If the command exits with a non-zero return code then an exception is going to be thrown. The exception message will be equal to the command stderr.
1 Like


Is this correct way or other way to check command exits with a non-zero?

Map<String, Object> args = new HashMap<>();
args.put(“command”, “ls”);
args.put(“args”, List.of(“-R”, “/sdcard/download/calculator.xlsx | grep "calculator.xlsx"”));
args.put(“timeout”, 20000);
args.put(“includeStderr”, true);
try {
String strStdOut = ((Map<String, String>) testContext.getAppiumDriver().executeScript(“mobile: shell”, args))
.filter(m → m.getKey().equals(“stdout”))
System.out.println("str Std Out " + strStdOut);
} catch (Exception e) {
System.out.println(“Exited with code 1”);

not sure but something like this. try first to check when file exists.


… I’m so glad I don’t have to code tests in Java, that code looks very verbose. Just reading the docs myself now, implies that omitting “includeStderr” will cause the call to raise, which is easy to catch in a guard block at the point you choose to deal with the error.

(I opted for writing my own ADB wrapper since I needed to write a identical IOS wrapper using the idevice lib stack tools anyway as a way of writing tests once for both platforms. I would suggest people go that route if platforms really matter to them, especially since Android versions often behave differently based on OS version, or if you care that external storage is being present or not, and the vendor’s security profile.)

1 Like