From 2f7abfec964fb2c31fafb2a880e4fbc544e42f27 Mon Sep 17 00:00:00 2001 From: shikong <919411476@qq.com> Date: Thu, 1 Aug 2024 21:58:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.MD | 31 +++++++++++++++++++++++++++++++ main.py | 27 +++++++++++++++++++-------- tasks/__init__.py | 0 tasks/douyin/__init__.py | 7 +++++++ 4 files changed, 57 insertions(+), 8 deletions(-) create mode 100644 tasks/__init__.py create mode 100644 tasks/douyin/__init__.py diff --git a/README.MD b/README.MD index 7b61fe1..5247c2d 100644 --- a/README.MD +++ b/README.MD @@ -1,3 +1,34 @@ ### 使用 uiautomator2 通过 adb 连接安卓设备 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 +``` + diff --git a/main.py b/main.py index 5f5402d..abfdeef 100644 --- a/main.py +++ b/main.py @@ -6,6 +6,8 @@ import uiautomator2 as u2 import time import random +import tasks.douyin + app = FastAPI() devices = { @@ -76,20 +78,29 @@ async def douyin_task(item: Device): width, height = d.window_size() random.seed(time.time()) for i in range(10): - startX = random.randint(0, int(width)) - endX = round(startX + random.random(), 1) + start_x = random.randint(0, int(width)) + end_x = round(start_x + random.random(), 1) - endY = random.randint(0, int(height / 10)) - startY = round(endY * 9.75, 1) - if startY - endY < (height / 3): - startY += round((height / 3), 1) + end_y = random.randint(0, int(height / 10)) + start_y = round(end_y * 9.75, 1) + if start_y - end_y < (height / 3): + start_y += round((height / 3), 1) # d.swipe_ext("up", scale=0.95, duration=0.1) - print(f"({startX}, {startY}) => ({endX}, {endY})") - d.swipe(startX, startY, endX + random.random(), endY, duration=0.05 + random.random() * 0.1) + print(f"({start_x}, {start_y}) => ({end_x}, {end_y})") + 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) 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__': uvicorn.run('main:app', host='0.0.0.0', port=8000, reload=True) diff --git a/tasks/__init__.py b/tasks/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tasks/douyin/__init__.py b/tasks/douyin/__init__.py new file mode 100644 index 0000000..c5e5e92 --- /dev/null +++ b/tasks/douyin/__init__.py @@ -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())