#!/usr/bin/env bash # uiautomator helper bound to ONE emulator. Usage: # SERIAL=emulator-5600 OUT=docs/qa/ ui.sh tap |text= # ui.sh has |text= -> count of matching nodes # ui.sh dump -> writes $XML # ui.sh shot -> $OUT/.png # ui.sh resumed -> current resumed activity set -euo pipefail SERIAL=${SERIAL:-emulator-5600} OUT=${OUT:-docs/qa/screens} XML=${XML:-${TMPDIR:-/tmp}/ui.xml} A=(adb -s "$SERIAL") dump() { "${A[@]}" shell uiautomator dump /sdcard/ui.xml >/dev/null; "${A[@]}" pull /sdcard/ui.xml "$XML" >/dev/null; } attr_of() { # $1 = id or text=.. if [[ $1 == text=* ]]; then echo "text=\"${1#text=}\""; else echo "resource-id=\"$1\""; fi } case "${1:-}" in dump) dump; echo "$XML" ;; has) dump; tr '>' '\n' < "$XML" | grep -c -F "$(attr_of "$2")" || true ;; tap) dump node=$(tr '>' '\n' < "$XML" | grep -F "$(attr_of "$2")" | head -1 || true) [ -z "$node" ] && { echo "not found: $2" >&2; exit 1; } b=$(echo "$node" | sed -E 's/.*bounds="\[([0-9]+),([0-9]+)\]\[([0-9]+),([0-9]+)\]".*/\1 \2 \3 \4/') read -r x1 y1 x2 y2 <<<"$b" "${A[@]}" shell input tap $(( (x1+x2)/2 )) $(( (y1+y2)/2 )) echo "tapped $2" ;; shot) mkdir -p "$OUT"; "${A[@]}" exec-out screencap -p > "$OUT/$2.png"; echo "$OUT/$2.png" ;; resumed) "${A[@]}" shell dumpsys activity activities | grep -E 'mResumedActivity|topResumedActivity' ;; *) echo "usage: ui.sh tap|has|dump|shot|resumed" >&2; exit 2 ;; esac