Skip to content

Commit 80695fc

Browse files
committed
feat: add dump_hierarchy and ToastWatcher
1 parent 37e1304 commit 80695fc

File tree

9 files changed

+192
-48
lines changed

9 files changed

+192
-48
lines changed

.flake8

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ ignore =
55
F401
66
E402
77
W292
8-
F403
8+
F403
9+
F821

.github/workflows/release.yml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
name: Release
22

3+
# on:
4+
# release:
5+
# types: [created]
36
on:
4-
release:
5-
types: [created]
7+
push:
8+
tags:
9+
- '*.*.*'
610

711
jobs:
812
build-n-publish:
@@ -21,6 +25,15 @@ jobs:
2125
pip install setuptools wheel twine poetry
2226
poetry install
2327
28+
- name: Get the version from the tag
29+
id: get_version
30+
run: echo "::set-output name=VERSION::${GITHUB_REF/refs\/tags\//}"
31+
32+
- name: Update version in pyproject.toml
33+
run: |
34+
version=${{ steps.get_version.outputs.VERSION }}
35+
poetry version $version
36+
2437
- name: Build a binary wheel and a source tarball
2538
run: poetry build
2639

README.md

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ d.screenshot(lpath)
6363

6464
# Device touch
6565
d.click(500, 1000)
66-
d.click(0.5, 0.4)
66+
d.click(0.5, 0.4) # "If of type float, it represents percentage coordinates."
6767
d.double_click(500, 1000)
6868
d.double_click(0.5, 0.4)
6969
d.long_click(500, 1000)
@@ -100,21 +100,28 @@ d(text="showToast").info
100100
# }
101101

102102
d(id="swiper").exists()
103-
d(type="Button", text="showToast").exists()
104-
d(text="showToast", isAfter=True).exists()
105-
d(text="showToast").click_if_exists()
103+
d(type="Button", text="tab_recrod").exists()
104+
d(text="tab_recrod", isAfter=True).exists()
105+
d(text="tab_recrod").click_if_exists()
106106
d(type="Button", index=3).click()
107-
d(text="showToast").double_click()
108-
d(text="showToast").long_click()
107+
d(text="tab_recrod").double_click()
108+
d(text="tab_recrod").long_click()
109109

110110
component: ComponentData = d(type="ListItem", index=1).find_component()
111111
d(type="ListItem").drag_to(component)
112112

113-
d(text="showToast").input_text("abc")
114-
d(text="showToast").clear_text()
115-
d(text="showToast").pinch_in()
116-
d(text="showToast").pinch_out()
113+
d(text="tab_recrod").input_text("abc")
114+
d(text="tab_recrod").clear_text()
115+
d(text="tab_recrod").pinch_in()
116+
d(text="tab_recrod").pinch_out()
117117

118+
# Dump hierarchy
119+
d.dump_hierarchy()
120+
121+
# Toast Watcher
122+
d.toast_watcher.start()
123+
d(type="Button", text="tab_recrod").click() # 触发toast的操作
124+
toast = d.toast_watcher.get()
118125

119126
```
120127

docs/DEVELOP.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,27 @@
168168
{"result":"UiWindow#10"}
169169
```
170170

171+
### uiEventObserverOnce
172+
**send**
173+
```
174+
{"module":"com.ohos.devicetest.hypiumApiHelper","method":"callHypiumApi","params":{"api":"Driver.uiEventObserverOnce","this":"Driver#0","args":["toastShow"],"message_type":"hypium"},"request_id":"20240905144543056211","client":"127.0.0.1"}
175+
```
176+
**recv**
177+
```
178+
{"result":true}
179+
```
180+
181+
182+
### getRecentUiEvent
183+
**send**
184+
```
185+
{"module":"com.ohos.devicetest.hypiumApiHelper","method":"callHypiumApi","params":{"api":"Driver.getRecentUiEvent","this":"Driver#0","args":[3000],"message_type":"hypium"},"request_id":"20240905143857794307","client":"127.0.0.1"}
186+
```
187+
**recv**
188+
```
189+
{"result":{"bundleName":"com.samples.test.uitest","text":"testMessage","type":"Toast"}}
190+
```
191+
171192

172193
## Component
173194
### Component.getId

hmdriver2/_client.py

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,15 @@ def _send_msg(self, msg: typing.Dict):
6464
}
6565
"""
6666
msg = json.dumps(msg, ensure_ascii=False, separators=(',', ':'))
67-
logger.debug(f"send msg: {msg}")
67+
logger.debug(f"sendMsg: {msg}")
6868
self.sock.sendall(msg.encode('utf-8') + b'\n')
6969

7070
def _recv_msg(self, buff_size: int = 1024, decode=False) -> typing.Union[bytearray, str]:
7171
try:
7272
relay = self.sock.recv(buff_size)
7373
if decode:
7474
relay = relay.decode()
75-
logger.debug(f"recv msg: {relay}")
75+
logger.debug(f"recvMsg: {relay}")
7676
return relay
7777
except (socket.timeout, UnicodeDecodeError) as e:
7878
logger.warning(e)
@@ -100,26 +100,13 @@ def invoke(self, api: str, this: str = "Driver#0", args: typing.List = []) -> Hy
100100
return HypiumResponse(**(json.loads(result)))
101101

102102
def start(self):
103-
"""
104-
105-
Args:
106-
file_path (str): Path where the recorded video will be saved.
107-
108-
Raises:
109-
RuntimeError: If the screen capture fails to start.
110-
"""
111103
logger.info("Start client connection")
112104
self._init_so_resource()
113105
self._restart_uitest_service()
114106

115107
self._connect_sock()
116108

117109
def release(self):
118-
"""
119-
120-
Raises:
121-
RuntimeError: If the screen capture fails to start.
122-
"""
123110
logger.info("Release client connection")
124111
try:
125112
if self.sock:
@@ -176,6 +163,7 @@ def __get_local_md5sum(f: str) -> str:
176163
def _restart_uitest_service(self):
177164
"""
178165
Restart the UITest daemon.
166+
179167
Note: 'hdc shell aa test' will also start a uitest daemon.
180168
$ hdc shell ps -ef |grep uitest
181169
shell 44306 1 25 11:03:37 ? 00:00:16 uitest start-daemon singleness

hmdriver2/_toast.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from .proto import HypiumResponse
4+
5+
6+
class ToastWatcher:
7+
def __init__(self, session: "Driver"): # type: ignore
8+
self.session = session
9+
10+
def start(self) -> bool:
11+
"""
12+
Initiates the observer to listen for a UI toast event
13+
14+
Returns:
15+
bool: True if the observer starts successfully, else False.
16+
"""
17+
api = "Driver.uiEventObserverOnce"
18+
resp: HypiumResponse = self.session._invoke(api, args=["toastShow"])
19+
return resp.result
20+
21+
def get(self, timeout: int = 5) -> str:
22+
"""
23+
Read the latest toast message content from the recent period.
24+
25+
Args:
26+
timeout (int): The maximum time to wait for a toast to appear if there are no matching toasts within the given time.
27+
28+
Returns:
29+
str: The content of the latest toast message.
30+
"""
31+
api = "Driver.getRecentUiEvent"
32+
resp: HypiumResponse = self.session._invoke(api, args=[timeout])
33+
if resp.result:
34+
return resp.result.get("text")
35+
return None

0 commit comments

Comments
 (0)