Skip to content

Commit 74753e8

Browse files
committed
fix: litter change
1 parent aaec31e commit 74753e8

File tree

7 files changed

+68
-64
lines changed

7 files changed

+68
-64
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ d.swipe(0.5, 0.8, 0.5, 0.4, speed=2000)
363363

364364
#### 输入
365365
```python
366-
d.input_text(x, y, text)
366+
d.input_text(text)
367367

368368
# eg.
369369
d.input_text(0.3, 0.5, "adbcdfg")

example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
d.long_click(500, 1000)
5353
d.long_click(0.5, 0.4)
5454
d.swipe(0.5, 0.8, 0.5, 0.4, speed=2000)
55-
d.input_text(0.5, 0.5, "adbcdfg")
55+
d.input_text("adbcdfg")
5656

5757
# Device touch gersture
5858
d.gesture.start(630, 984, interval=.5).move(0.2, 0.4, interval=.5).pause(interval=1).move(0.5, 0.6, interval=.5).pause(interval=1).action()

hmdriver2/_client.py

Lines changed: 44 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
from . import logger
1414
from .hdc import HdcWrapper
1515
from .proto import HypiumResponse, DriverData
16-
from .exception import InvokeHypiumError
16+
from .exception import InvokeHypiumError, InvokeCaptures
1717

1818

1919
UITEST_SERVICE_PORT = 8012
20-
SOCKET_TIMEOUT = 30
20+
SOCKET_TIMEOUT = 10
2121

2222

2323
class HmClient:
@@ -63,43 +63,27 @@ def _send_msg(self, msg: typing.Dict):
6363
logger.debug(f"sendMsg: {msg}")
6464
self.sock.sendall(msg.encode('utf-8') + b'\n')
6565

66-
def _recv_msg(self, buff_size: int = 1024, decode=False, print=True) -> typing.Union[bytearray, str]:
66+
def _recv_msg(self, buff_size: int = 4096, decode=False, print=True) -> typing.Union[bytearray, str]:
67+
full_msg = bytearray()
6768
try:
68-
full_message = bytearray()
69-
while True:
70-
relay = self.sock.recv(buff_size)
71-
if not relay:
72-
break
73-
74-
full_message.extend(relay)
75-
76-
if decode:
77-
try:
78-
decoded_message = full_message.decode()
79-
# Validate if it's a complete JSON
80-
json.loads(decoded_message)
81-
if print:
82-
logger.debug(f"recvMsg (complete JSON): {decoded_message}")
83-
return decoded_message
84-
except (UnicodeDecodeError, json.JSONDecodeError):
85-
# Incomplete JSON, continue receiving
86-
continue
87-
else:
88-
if print:
89-
logger.debug(f"recvMsg (partial): {full_message}")
90-
91-
# If decode is False, return the full message as bytearray
92-
return full_message
69+
# FIXME
70+
relay = self.sock.recv(buff_size)
71+
if decode:
72+
relay = relay.decode()
73+
if print:
74+
logger.debug(f"recvMsg: {relay}")
75+
full_msg = relay
9376

9477
except (socket.timeout, UnicodeDecodeError) as e:
9578
logger.warning(e)
9679
if decode:
97-
return ''
98-
return bytearray()
80+
full_msg = ""
81+
82+
return full_msg
9983

100-
def invoke(self, api: str, this: str = "Driver#0", args: typing.List = [], method: str = None) -> HypiumResponse:
84+
def invoke(self, api: str, this: str = "Driver#0", args: typing.List = []) -> HypiumResponse:
10185
"""
102-
Invokes a given API method with the specified arguments and handles exceptions.
86+
Hypium invokes given API method with the specified arguments and handles exceptions.
10387
10488
Args:
10589
api (str): The name of the API method to invoke.
@@ -113,25 +97,16 @@ def invoke(self, api: str, this: str = "Driver#0", args: typing.List = [], metho
11397
"""
11498

11599
request_id = datetime.now().strftime("%Y%m%d%H%M%S%f")
116-
117-
if method is None:
118-
# When method is None, include 'this' in params
119-
params = {
120-
"api": api,
121-
"this": this,
122-
"args": args,
123-
"message_type": "hypium"
124-
}
125-
else:
126-
# When method is specified, 'this' is not needed in params
127-
params = {
128-
"api": api,
129-
"args": args
130-
}
100+
params = {
101+
"api": api,
102+
"this": this,
103+
"args": args,
104+
"message_type": "hypium"
105+
}
131106

132107
msg = {
133108
"module": "com.ohos.devicetest.hypiumApiHelper",
134-
"method": method if method else "callHypiumApi",
109+
"method": "callHypiumApi",
135110
"params": params,
136111
"request_id": request_id
137112
}
@@ -143,6 +118,27 @@ def invoke(self, api: str, this: str = "Driver#0", args: typing.List = [], metho
143118
raise InvokeHypiumError(data.exception)
144119
return data
145120

121+
def invoke_captures(self, api: str, args: typing.List = []) -> HypiumResponse:
122+
request_id = datetime.now().strftime("%Y%m%d%H%M%S%f")
123+
params = {
124+
"api": api,
125+
"args": args
126+
}
127+
128+
msg = {
129+
"module": "com.ohos.devicetest.hypiumApiHelper",
130+
"method": "Captures",
131+
"params": params,
132+
"request_id": request_id
133+
}
134+
135+
self._send_msg(msg)
136+
raw_data = self._recv_msg(decode=True)
137+
data = HypiumResponse(**(json.loads(raw_data)))
138+
if data.exception:
139+
raise InvokeCaptures(data.exception)
140+
return data
141+
146142
def start(self):
147143
logger.info("Start HmClient connection")
148144
self._init_so_resource()

hmdriver2/_screenrecord.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def stop(self) -> str:
121121
self.release()
122122

123123
# Invalidate the cached property
124-
self.driver.invalidate_cache('screenrecord')
124+
self.driver._invalidate_cache('screenrecord')
125125

126126
except Exception as e:
127127
logger.error(f"An error occurred: {e}")

hmdriver2/driver.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ def _is_device_online(self):
5050
_serials = list_devices()
5151
return True if self.serial in _serials else False
5252

53-
def _invoke(self, api: str, args: List = [], method: str = None) -> HypiumResponse:
54-
return self._client.invoke(api, this="Driver#0", args=args, method=method)
53+
def _invoke(self, api: str, args: List = []) -> HypiumResponse:
54+
return self._client.invoke(api, this="Driver#0", args=args)
5555

5656
@delay
5757
def start_app(self, package_name: str, page_name: str = "MainAbility"):
@@ -177,8 +177,8 @@ def device_info(self) -> DeviceInfo:
177177
)
178178

179179
@delay
180-
def open_url(self, url: str, system_browser: bool = None):
181-
if system_browser is True:
180+
def open_url(self, url: str, system_browser: bool = True):
181+
if system_browser:
182182
# Use the system browser
183183
self.hdc.shell(f"aa start -A ohos.want.action.viewData -e entity.system.browsable -U {url}")
184184
else:
@@ -291,22 +291,26 @@ def swipe(self, x1, y1, x2, y2, speed=2000):
291291
self._invoke(api, args=[point1.x, point1.y, point2.x, point2.y, speed])
292292

293293
@delay
294-
def input_text(self, x: int = 1, y: int = 1, text: str = ""):
295-
"""
296-
input_text(100, 100, text="测试")
297-
input_text(text="测试")
294+
def input_text(self, text: str):
298295
"""
296+
Inputs text into the currently focused input field.
297+
298+
Note: The input field must have focus before calling this method.
299299
300-
return self._invoke("Driver.inputText", args=[{"x": x, "y": y}, text])
300+
Args:
301+
text (str): input value
302+
"""
303+
return self._invoke("Driver.inputText", args=[{"x": 1, "y": 1}, text])
301304

302-
def dump_hierarchy(self) -> HypiumResponse:
305+
def dump_hierarchy(self) -> Dict:
303306
"""
304307
Dump the UI hierarchy of the device screen.
305308
306309
Returns:
307310
Dict: The dumped UI hierarchy as a dictionary.
308311
"""
309-
return self._invoke("captureLayout", method="Captures")
312+
# return self._client.invoke_captures("captureLayout").result
313+
return self.hdc.dump_hierarchy()
310314

311315
@cached_property
312316
def gesture(self):
@@ -318,7 +322,7 @@ def screenrecord(self):
318322
from ._screenrecord import RecordClient
319323
return RecordClient(self.serial, self)
320324

321-
def invalidate_cache(self, attribute_name):
325+
def _invalidate_cache(self, attribute_name):
322326
"""
323327
Invalidate the cached property.
324328

hmdriver2/exception.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ class InvokeHypiumError(Exception):
2424
pass
2525

2626

27+
class InvokeCaptures(Exception):
28+
pass
29+
30+
2731
class InjectGestureError(Exception):
2832
pass
2933

tests/test_driver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def test_swipe(d):
145145

146146

147147
def test_input_text(d):
148-
d.input_text(0.5, 0.5, "adbcdfg")
148+
d.input_text("adbcdfg")
149149

150150

151151
def test_dump_hierarchy(d):

0 commit comments

Comments
 (0)