Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
e34ca32
feat: Support x86 emulator
yabi-zzh Apr 15, 2025
702d064
feat: Add press_keys and go_recent
yabi-zzh Apr 15, 2025
f6975e0
feat: Add message framing using protocol headers (HEADER_BYTES and TR…
yabi-zzh Apr 21, 2025
b413f4e
refactor: Remove unused buff_size parameter
yabi-zzh Apr 21, 2025
35eee03
Update README.md
yabi-zzh Jun 14, 2025
6128175
feat: 支持xpath获取其他属性、优化部分代码及注释
yabi-zzh Jun 15, 2025
3752182
feat: 实现控件查找正则匹配功能
yabi-zzh Sep 16, 2025
c2dba0d
fix: 修复端口转发清理问题
yabi-zzh Sep 23, 2025
8fb542b
feat: 实现 WebView 自动化测试 和 ChromeDriver 版本适配
yabi-zzh Sep 24, 2025
61497e9
docs: 更新 README.md
yabi-zzh Sep 24, 2025
e51a67c
refactor: 优化端口分配并去除冗余命令,显著提升启动速度
yabi-zzh Sep 24, 2025
a332b44
fix: 修复current_app获取、优化部分hdc命令
yabi-zzh Sep 29, 2025
480363a
chore: 添加自动化安装脚本和文档,升级版本到1.5.1
yabi-zzh Sep 29, 2025
33f6d49
fix: 修复因正则功能引起的查找问题
yabi-zzh Oct 11, 2025
3b45e99
chore: 升级agent.so版本,修改proto.py适配最新so
yabi-zzh Oct 11, 2025
a426627
chore: 提升版本到1.5.3
yabi-zzh Oct 11, 2025
8b64d53
fix: 修复 screen_off 逻辑错误和 FreePort 资源泄漏问题
yabi-zzh Oct 29, 2025
a7aa842
perf: 优化元素属性访问性能,使用属性缓存机制
yabi-zzh Oct 29, 2025
3303274
chore: 提升版本到1.5.4
yabi-zzh Oct 29, 2025
17534c8
fix: 修复动态 Session ID 导致的协议响应格式异常
yabi-zzh Oct 30, 2025
77def4d
fix: 修复 send_key 方法中命令参数问题
yabi-zzh Oct 30, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 92 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
[![github actions](https://github.com/codematrixer/hmdriver2/actions/workflows/release.yml/badge.svg)](https://github.com/codematrixer/hmdriver2/actions)
[![pypi version](https://img.shields.io/pypi/v/hmdriver2.svg)](https://pypi.python.org/pypi/hmdriver2)
![python](https://img.shields.io/pypi/pyversions/hmdriver2.svg)
[![downloads](https://pepy.tech/badge/hmdriver2)](https://pepy.tech/project/hmdriver2)
[![downloads](https://pepy.tech/badge/hmdriver2)](https://pepy.tech/project/hmdriver2)


> 写这个项目前github上已有个叫`hmdriver`的项目,但它是侵入式(需要提前在手机端安装一个testRunner app);另外鸿蒙官方提供的hypium自动化框架,使用较为复杂,依赖繁杂。于是决定重写一套。
> **📢 重要通知:**
>
> **由于原项目的维护模式已变更,本仓库(当前项目)将接替成为核心维护分支。会尽量保持更新(包括功能改进、Bug修复等)。**


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

<img src="https://i.ibb.co/Xx9HcKk/wechat.png" alt="wechat" style="float: left" />

> 其他项目:HarmonyScrcpy(HarmonyOS NEXT / OpenHarmony 同屏与应用重签名工具)
> 项目地址:<https://github.com/yabi-zzh/HarmonyScrcpy>

# Key idea
- **无侵入式**
- 无需提前在手机端安装testRunner APP(类似atx app)
Expand Down Expand Up @@ -47,6 +52,11 @@
- 控件点击,长按,拖拽,缩放
- 文本输入,清除
- 获取控件树
- 支持 WebView(WebDriver)自动化
- 自动发现与连接 WebView
- 端口转发(TCP / 应用内部端口)
- ChromeDriver 版本自动适配(114 / 140)
- 自动切换到可见窗口
- 支持Toast获取
- UI Inspector
- [TODO] 全场景弹窗处理
Expand Down Expand Up @@ -779,6 +789,86 @@ toast = d.toast_watcher.get_toast()
# output: 'testMessage'
```

## WebDriver

### 配置 ChromeDriver

- 下载对应版本的 chromedriver,并放置到项目目录:
- `hmdriver2/assets/web_debug_tools/chromedriver_<版本>/chromedriver[.exe]`
- 示例:
- Windows: `hmdriver2/assets/web_debug_tools/chromedriver_114/chromedriver.exe`
- Linux/macOS: `hmdriver2/assets/web_debug_tools/chromedriver_140/chromedriver`
- 仅使用项目内置目录,不从系统 PATH 中查找。

可选配置:
```python
driver.webdriver.configure_chromedriver(
enable_log=False, # 是否启用 chromedriver 日志
log_level="INFO", # OFF/SEVERE/WARNING/INFO/DEBUG/ALL
backup_log=False, # 是否备份日志
port=9515 # chromedriver 服务端口(默认9515)
)
```

### 连接 WebView

```python
from hmdriver2.driver import Driver

d = Driver()
d.start_app("com.huawei.hmos.browser")

wd = d.webdriver.connect("com.huawei.hmos.browser")
# 或指定 chromedriver 版本
# wd = d.webdriver.connect("com.huawei.hmos.browser", chromedriver_version=140)

wd.get("https://www.baidu.com")
print(wd.title)
```

### 超时设置

- 页面加载超时:默认 10 秒
- 脚本执行超时:默认 10 秒
- 元素查找隐式等待:默认 5 秒

如需自定义,可在连接后自行调用 Selenium 原生 API:
```python
wd.set_page_load_timeout(30)
wd.set_script_timeout(30)
wd.implicitly_wait(10)
```

### 窗口管理

- 连接后自动切换到“可见窗口”;如需手动:
```python
d.webdriver.switch_to_visible_window(index=0) # 第N个可见窗口,支持负数
```

### 常用示例

```python
from selenium.webdriver.common.by import By

wd.get("https://www.baidu.com")
search = wd.find_element(By.ID, "index-kw")
search.send_keys("HarmonyOS")
```

### 故障排查

- 无法获取 WebView 版本:
1) 确认应用已启用 WebView 调试
2) 设备未锁屏且 WebView 界面可见
3) WebView 已完全加载
- 端口转发失败:
- 检查是否使用了应用内部端口(abstract socket)或 TCP 端口
- 查看日志中的“系统内部端口转发/应用内部端口转发”信息
- 版本不兼容:
- WebView 114-132 使用 chromedriver_114
- WebView 140+ 使用 chromedriver_140

# 鸿蒙Uitest协议

See [DEVELOP.md](/docs/DEVELOP.md)
Expand Down
4 changes: 3 additions & 1 deletion example.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# -*- coding: utf-8 -*-

import time

from hmdriver2.driver import Driver
from hmdriver2.proto import DeviceInfo, KeyCode, ComponentData, DisplayRotation


# New driver
d = Driver("FMR0223C13000649")

Expand Down Expand Up @@ -135,3 +135,5 @@
d.xpath('//*[@text="showDialog"]').click_if_exists()
d.xpath('//root[1]/Row[1]/Column[1]/Row[1]/Button[3]').click()
d.xpath('//*[@text="showDialog"]').input_text("xxx")
d.xpath('//*[@text="showDialog"]').text()
d.xpath('//*[@text="showDialog"]').clickable()
4 changes: 2 additions & 2 deletions hmdriver2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import logging

formatter = logging.Formatter('[%(asctime)s] %(filename)15s[line:%(lineno)4d] \
[%(levelname)s] %(message)s',
formatter = logging.Formatter('[%(asctime)s] %(filename)18s[line:%(lineno)4d] ' +
'[%(levelname)-7s] %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')

logger = logging.getLogger('hmdriver2')
Expand Down
Loading