This commit is contained in:
shikong 2024-08-01 21:58:33 +08:00
parent 9d3ec6ea4a
commit 2f7abfec96
Signed by: Shikong
GPG Key ID: BD85FF18B373C341
4 changed files with 57 additions and 8 deletions

View File

@ -1,3 +1,34 @@
### 使用 uiautomator2 通过 adb 连接安卓设备 ### 使用 uiautomator2 通过 adb 连接安卓设备
uiautomator2 是基于 uiautomator 的 Python 封装,可以用来对安卓设备进行自动化测试。 uiautomator2 是基于 uiautomator 的 Python 封装,可以用来对安卓设备进行自动化测试。
### 可视化辅助工具 (选其中一个安装即可)
#### uiautodev
##### 安装
```shell
pip3 install -U uiautodev
```
##### 运行
```shell
uiauto.dev
```
```shell
python3 -m uiautodev
```
#### weditor
##### 安装
```shell
git clone https://github.com/openatx/weditor
pip3 install -e weditor
```
##### 运行
```shell
weditor
```

27
main.py
View File

@ -6,6 +6,8 @@ import uiautomator2 as u2
import time import time
import random import random
import tasks.douyin
app = FastAPI() app = FastAPI()
devices = { devices = {
@ -76,20 +78,29 @@ async def douyin_task(item: Device):
width, height = d.window_size() width, height = d.window_size()
random.seed(time.time()) random.seed(time.time())
for i in range(10): for i in range(10):
startX = random.randint(0, int(width)) start_x = random.randint(0, int(width))
endX = round(startX + random.random(), 1) end_x = round(start_x + random.random(), 1)
endY = random.randint(0, int(height / 10)) end_y = random.randint(0, int(height / 10))
startY = round(endY * 9.75, 1) start_y = round(end_y * 9.75, 1)
if startY - endY < (height / 3): if start_y - end_y < (height / 3):
startY += round((height / 3), 1) start_y += round((height / 3), 1)
# d.swipe_ext("up", scale=0.95, duration=0.1) # d.swipe_ext("up", scale=0.95, duration=0.1)
print(f"({startX}, {startY}) => ({endX}, {endY})") print(f"({start_x}, {start_y}) => ({end_x}, {end_y})")
d.swipe(startX, startY, endX + random.random(), endY, duration=0.05 + random.random() * 0.1) d.swipe(start_x, start_y, end_x + random.random(), end_y, duration=0.05 + random.random() * 0.1)
time.sleep(5 + random.random() * 2) time.sleep(5 + random.random() * 2)
return Response() return Response()
@app.post("/task/app/douyin/test")
async def douyin_task(item: Device):
if item.serial not in devices:
return Response(code=500, msg="设备 {} 不存在".format(item.serial))
d = devices[item.serial]
d.app_start("com.ss.android.ugc.aweme", stop=False)
tasks.douyin.click_search(d)
if __name__ == '__main__': if __name__ == '__main__':
uvicorn.run('main:app', host='0.0.0.0', port=8000, reload=True) uvicorn.run('main:app', host='0.0.0.0', port=8000, reload=True)

0
tasks/__init__.py Normal file
View File

7
tasks/douyin/__init__.py Normal file
View File

@ -0,0 +1,7 @@
from uiautomator2 import Device
def click_search(d: Device):
(d
.xpath('//*[@resource-id="com.ss.android.ugc.aweme:id/ybv"]/android.widget.Button[1]/android.widget.FrameLayout[1]')
.click())