Linux与Bash脚本

环境

  • android sdk
  • emulator
  • adb

https://developer.android.com/studio#downloads

Linux

  • adb shell input tap 200 400
  • adb shell input text ceshiren.com
  • adb shell “uiautomator dump && cat /sdcard/window_dump.xml”

android控件信息

<node index="0" text="jiminy 请求,嵌入式测试相关问题 This topic has 3 replies 霍格沃兹答疑区 1h" resource-id="" class="android.view.View" package="com.android.chrome" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[26,1162][1057,1336]">
                                                                        <node index="0" text="" resource-id="" class="android.view.View" package="com.android.chrome" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[26,1183][147,1304]">
                                                                            <node index="0" text="" resource-id="" class="android.view.View" package="com.android.chrome" content-desc="jiminy" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[26,1183][147,1304]">
                                                                                <node index="0" text="jiminy" resource-id="" class="android.widget.Image" package="com.android.chrome" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[26,1183][147,1304]" />
                                                                            </node>
                                                                        </node>
                                                                        <node index="1" text="" resource-id="" class="android.view.View" package="com.android.chrome" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[170,1183][863,1241]">
                                                                            <node index="0" text="请求,嵌入式测试相关问题" resource-id="" class="android.view.View" package="com.android.chrome" content-desc="" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[170,1160][719,1294]" />
                                                                        </node>
                                                                        <node index="2" text="This topic has 3 replies" resource-id="" class="android.view.View" package="com.android.chrome" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[1023,1183][1057,1257]">
                                                                            <node index="0" text="This topic has 3 replies" resource-id="" class="android.widget.Button" package="com.android.chrome" content-desc="" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[1023,1191][1057,1246]" />
                                                                        </node>
                                                                        <node index="3" text="" resource-id="" class="android.view.View" package="com.android.chrome" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[170,1257][1057,1315]">
                                                                            <node index="0" text="  " resource-id="" class="android.view.View" package="com.android.chrome" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[170,1257][1057,1315]" />
                                                                            <node index="1" text="" resource-id="" class="android.view.View" package="com.android.chrome" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[170,1257][483,1315]">
                                                                                <node index="0" text="" resource-id="" class="android.view.View" package="com.android.chrome" content-desc="霍格沃兹答疑区" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[170,1262][451,1315]">
                                                                                    <node index="0" text="霍格沃兹测试学院测试VIP学员答疑区,外部同学可浏览,不可发帖。" resource-id="" class="android.view.View" package="com.android.chrome" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[210,1262][451,1315]" />
                                                                                </node>
                                                                            </node>
                                                                            <node index="2" text="" resource-id="" class="android.view.View" package="com.android.chrome" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[1010,1257][1057,1315]">
                                                                                <node index="0" text="First post: Jan 16, 2022 8:40 am Posted: Jan 16, 2022 1:57 pm" resource-id="" class="android.view.View" package="com.android.chrome" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[1010,1262][1057,1312]">
                                                                                    <node index="0" text="" resource-id="" class="android.view.View" package="com.android.chrome" content-desc="1h" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[1010,1262][1057,1312]">
                                                                                        <node index="0" text="1h" resource-id="" class="android.widget.TextView" package="com.android.chrome" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[1010,1262][1057,1312]" />
                                                                                    </node>
                                                                                </node>
                                                                            </node>
                                                                            <node index="3" text="  " resource-id="" class="android.view.View" package="com.android.chrome" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,0][0,0]" />
                                                                            <node index="4" text="  " resource-id="" class="android.view.View" package="com.android.chrome" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,0][0,0]" />
                                                                            <node index="5" text="  " resource-id="" class="android.view.View" package="com.android.chrome" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,0][0,0]" />
                                                                        </node>
                                                                    </node>

完整的xml

Bash

脚本

编写一个微信app的自动化工具

DEVICE=""

page_source() {
    adbs shell "uiautomator dump && cat /sdcard/window_dump.xml" |
        sed 's#><#>^<#g' |
        awk 'RS="^"'
}

click() {
    local keyword="$1"
    #todo: 根据文字获得中心坐标
    page_source |
        grep "$keyword" |
        awk -F '\\[|\\]|,' '{print ($(NF-5)+$(NF-2))/2,($(NF-4)+$(NF-1))/2}' |
        {
            read x y
            echo $keyword = $y >$x
            adbs shell input tap $x $y
        }

}

send_keys() {
    local text="$1"
    adbs shell input text $text
}

back() {
    adbs shell input keyevent 4
}

get_text() {
    sed 's#.* text="\([^"]*\)".*#\1#'
}

#adbs shell ps  #use default device
#adb_select device #use spec device
adbs() {
    adb -s $DEVICE "$@"
}

adb_select() {
    local name="$1"
    DEVICE=$name
}

dingqunhongbao() {
    local count=0
    while true; do
        local tmp_count=$(page_source | grep id/u1 | wc -l)
        if [ ! "$tmp_count" = "$count" -a "$tmp_count" -gt 0 ]; then
            click 微信红包
            click 开
            back
        else
            echo same count $tmp_count
        fi
        count=$tmp_count
    done
}

中场休息 4:25 回来

相关链接

https://github.com/Genymobile/scrcpy

报到~

报道!!!!!

报到

报道

报道

报道!

报道

报道

报道

报道

报道

报到

报到

报到

报到

adb shell “uiautomator dump | cat /sdcard/window_dump.xml” | sed ‘s/></>^</g’ | awk ‘BEGIN{RS="^"}{print}’ | grep “最新版本下载地址”" | awk -F ‘\[|\]|,’ ‘{print ($(NF-5)+$(NF-2))/2,($(NF-4)+$(NF-1))/2}’