73 lines
1.6 KiB
Go
73 lines
1.6 KiB
Go
|
|
package main
|
||
|
|
|
||
|
|
import (
|
||
|
|
"bytes"
|
||
|
|
"fmt"
|
||
|
|
"io/ioutil"
|
||
|
|
"net/http"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"github.com/tidwall/gjson"
|
||
|
|
)
|
||
|
|
|
||
|
|
func main() {
|
||
|
|
// 查找设备
|
||
|
|
resp, err := http.Get("http://localhost:5037/devices")
|
||
|
|
if err != nil {
|
||
|
|
panic(err)
|
||
|
|
}
|
||
|
|
defer resp.Body.Close()
|
||
|
|
|
||
|
|
body, err := ioutil.ReadAll(resp.Body)
|
||
|
|
if err != nil {
|
||
|
|
panic(err)
|
||
|
|
}
|
||
|
|
|
||
|
|
// 解析设备信息
|
||
|
|
serial := gjson.GetBytes(body, "devices.0.serial").String()
|
||
|
|
|
||
|
|
// 连接设备
|
||
|
|
resp, err = http.Post(fmt.Sprintf("http://localhost:5037/devices/%s/connect", serial), "", nil)
|
||
|
|
if err != nil {
|
||
|
|
panic(err)
|
||
|
|
}
|
||
|
|
defer resp.Body.Close()
|
||
|
|
|
||
|
|
// 获取应用程序内容页信息
|
||
|
|
resp, err = http.Post(fmt.Sprintf("http://localhost:5037/devices/%s/activities", serial), "application/json", bytes.NewBufferString(`{"package":"com.example.myapp"}`))
|
||
|
|
if err != nil {
|
||
|
|
panic(err)
|
||
|
|
}
|
||
|
|
defer resp.Body.Close()
|
||
|
|
|
||
|
|
body, err = ioutil.ReadAll(resp.Body)
|
||
|
|
if err != nil {
|
||
|
|
panic(err)
|
||
|
|
}
|
||
|
|
|
||
|
|
// 解析应用程序内容页信息
|
||
|
|
layout := gjson.GetBytes(body, "layout")
|
||
|
|
|
||
|
|
// 查找关闭按钮位置
|
||
|
|
var x, y int
|
||
|
|
layout.ForEach(func(key, value gjson.Result) bool {
|
||
|
|
if value.Get("class").String() == "android.widget.Button" && value.Get("text").String() == "Close" {
|
||
|
|
x = int(value.Get("bounds.0").Float())
|
||
|
|
y = int(value.Get("bounds.1").Float())
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
return true
|
||
|
|
})
|
||
|
|
|
||
|
|
// 发送模拟点击指令
|
||
|
|
cmd := fmt.Sprintf("input tap %d %d", x, y)
|
||
|
|
resp, err = http.Post(fmt.Sprintf("http://localhost:5037/devices/%s/shell", serial), "application/octet-stream", bytes.NewBufferString(cmd))
|
||
|
|
if err != nil {
|
||
|
|
panic(err)
|
||
|
|
}
|
||
|
|
defer resp.Body.Close()
|
||
|
|
|
||
|
|
// 等待点击操作完成
|
||
|
|
time.Sleep(time.Second)
|
||
|
|
}
|