Skip to content

Commit

Permalink
feat: faster swipe
Browse files Browse the repository at this point in the history
  • Loading branch information
codematrixer committed Sep 13, 2024
1 parent e007f5e commit 43f47a1
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 15 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
[![downloads](https://pepy.tech/badge/hmdriver2)](https://pepy.tech/project/hmdriver2)


>写这个项目前github上已有个[hmdirver](https://github.com/mrx1203/hmdriver),但它是侵入式(需要提前在手机端安装一个testRunner app)。另外鸿蒙官方提供的hypium自动化框架,使用较为复杂,依赖繁杂。于是决定重写一套。
>写这个项目前github上已有个[hmdirver](https://github.com/mrx1203/hmdriver),但它是侵入式(需要提前在手机端安装一个testRunner app)。另外鸿蒙官方提供的`hypium`自动化框架,使用较复杂,依赖繁杂。于是决定重写一套框架,解决上述两个框架的弊端。

`hmdriver2`是一款支持`鸿蒙Next`系统的UI自动化框架,**无侵入式**,提供应用管理,UI操作,元素定位等功能,轻量高效,上手简单,快速实现鸿蒙应用自动化测试需求。


# Feature
- 支持鸿蒙Next系统的自动化
- 支持鸿蒙Next系统的UI自动化测试
- **无侵入式**,无需在手机安装基于ArkTS的testRunner APP
- 稳定高效,直接和鸿蒙底层uitest服务交互
- 轻量,上手简单,即插即用
Expand Down Expand Up @@ -351,7 +351,7 @@ d.swipe(x1, y1, x2, y2, spped)
d.swipe(600, 2600, 600, 1200, speed=2000) # 上滑
d.swipe(0.5, 0.8, 0.5, 0.4, speed=2000)
```
参数`x1`, `y1`表示滑动的起始点,`x2`, `y2`表示滑动的终点,`speed`为滑动速率, 范围:200~40000, 不在范围内设为默认值为600, 单位: 像素点/秒
参数`x1`, `y1`表示滑动的起始点,`x2`, `y2`表示滑动的终点,`speed`为滑动速率, 范围:200~40000, 不在范围内设为默认值为2000, 单位: 像素点/秒

#### 输入
```python3
Expand Down
10 changes: 10 additions & 0 deletions docs/DEVELOP.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,16 @@
{"result":null}
```

### swipe
**send**
```
{"module":"com.ohos.devicetest.hypiumApiHelper","method":"callHypiumApi","params":{"api":"Driver.swipe","this":"Driver#0","args":[630,2176,630,1360,7344],"message_type":"hypium"},"request_id":"20240913123029322117","client":"127.0.0.1"}
```
**recv**
```
{"result":null}
```


### findComponents
**send**
Expand Down
21 changes: 11 additions & 10 deletions hmdriver2/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@
import json
import uuid
from typing import Type, Any, Tuple, Dict, Union, List

try:
# Python3.8+
from functools import cached_property
except ImportError:
from cached_property import cached_property
from functools import cached_property # python3.8+

from . import logger
from .utils import delay
Expand Down Expand Up @@ -146,7 +141,7 @@ def screen_off(self):
def unlock(self):
self.screen_on()
w, h = self.display_size
self.hdc.swipe(0.5 * w, 0.8 * h, 0.5 * w, 0.2 * h, speed=600)
self.swipe(0.5 * w, 0.8 * h, 0.5 * w, 0.2 * h, speed=6000)

@cached_property
def display_size(self) -> Tuple[int, int]:
Expand Down Expand Up @@ -268,7 +263,7 @@ def long_click(self, x: Union[int, float], y: Union[int, float]):
self._invoke(api, args=[point.x, point.y])

@delay
def swipe(self, x1, y1, x2, y2, speed=1000):
def swipe(self, x1, y1, x2, y2, speed=2000):
"""
Perform a swipe action on the device screen.
Expand All @@ -277,12 +272,18 @@ def swipe(self, x1, y1, x2, y2, speed=1000):
y1 (float): The start Y coordinate as a percentage or absolute value.
x2 (float): The end X coordinate as a percentage or absolute value.
y2 (float): The end Y coordinate as a percentage or absolute value.
speed (int, optional): The swipe speed in pixels per second. Default is 1000. Range: 200-40000. If not within the range, set to default value of 600.
speed (int, optional): The swipe speed in pixels per second. Default is 2000. Range: 200-40000. If not within the range, set to default value of 2000.
"""

point1 = self._to_abs_pos(x1, y1)
point2 = self._to_abs_pos(x2, y2)

self.hdc.swipe(point1.x, point1.y, point2.x, point2.y, speed=speed)
if speed < 200 or speed > 40000:
logger.warning("`speed` is not in the range[200-40000], Set to default value of 2000.")
speed = 2000

api = "Driver.swipe"
self._invoke(api, args=[point1.x, point1.y, point2.x, point2.y, speed])

@delay
def input_text(self, x, y, text: str):
Expand Down
2 changes: 1 addition & 1 deletion hmdriver2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def delay(func):
After each UI operation, it is necessary to wait for a while to ensure the stability of the UI,
so as not to affect the next UI operation.
"""
DELAY_TIME = 0.4
DELAY_TIME = 0.5

@wraps(func)
def wrapper(*args, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description = "UI Automation Framework for Harmony Next"
authors = ["codematrixer <[email protected]>"]
license = "MIT"
readme = "README.md"
include = ["*/asset/*"]
include = ["*/assets/*"]

[tool.poetry.dependencies]
python = "^3.8"
Expand Down

0 comments on commit 43f47a1

Please sign in to comment.