android / android/android-test

UiDevice.executeShellCommand doesn't support full adb shell functionality

Open
#772 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
Java
Stars
1.2k
Forks
342
Avg merge
11h 29m
Merged PRs (30d)
2

Description

### Description

`UiDevice.executeShellCommand` doesn't have an affordance for stdin, and doesn't allow piping between multiple commands, or redirects.

### Steps to Reproduce

```
// The following line will fail:
assertEquals("foo\n", mUiDevice.executeShellCommand("echo foo | xargs echo $1"))
```

### AndroidX Test and Android OS Versions

uiautomator 2.2.0, any supported (example above requires API 26, due to `xargs` usage)

### Proposal

I worked around this (in the Jetpack repo) by adding an extension - [`UiDevice.executeShellScript(script: String, stdin: String? = null)`](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-master-dev:benchmark/perfetto/src/main/java/androidx/benchmark/perfetto/ShellUtils.kt;l=37;drc=2193701d13f8c18dc3540a3c5e6f34cb93fae6cc)

Would you be interested in accepting this functionality upstream?

It works by writing the 'script' param to a shell-readable location, copying it to a shell-runnable location (needed in Q+), and executing it there. As this adds additional binder calls, I think it makes sense to add it as a separate method. Can also as part of this link to [Android's shell and utilities](https://android.googlesource.com/platform/system/core/+/master/shell_and_utilities/README.md#) from the docs of both methods, as that's a *very* necessary reference for running shell commands on Android.

Contributor guide

Open the contributing guide

Research direction

Start by locating UiDevice.executeShellCommand and compare its behavior with the referenced ShellUtils.kt implementation. Reproduce the documented pipeline example, then determine how the proposed script and optional stdin support should be exposed and documented, including the Android shell utilities reference. Done means shell scripts can use stdin, pipes, and redirects through the upstream API.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, shell
Domain
cli, testing
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.