Skip to content

Commit 7d0bf71

Browse files
yimeliaadolli
authored andcommitted
添加android poco.stop_running接口
1 parent da17f1f commit 7d0bf71

File tree

1 file changed

+36
-17
lines changed

1 file changed

+36
-17
lines changed

poco/drivers/android/uiautomation.py

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,34 @@ def on_bind_driver(self, driver):
9898
self.input.add_preaction_cb(driver)
9999

100100

101+
class KeepRunningInstrumentationThread(threading.Thread):
102+
"""Thread class with a stop() method. The thread itself has to check
103+
regularly for the stopped() condition."""
104+
105+
def __init__(self, poco, port_to_ping):
106+
super(KeepRunningInstrumentationThread, self).__init__()
107+
self._stop_event = threading.Event()
108+
self.poco = poco
109+
self.port_to_ping = port_to_ping
110+
111+
def stop(self):
112+
self._stop_event.set()
113+
114+
def stopped(self):
115+
return self._stop_event.is_set()
116+
117+
def run(self):
118+
while not self.stopped():
119+
if getattr(self.poco, "_instrument_proc", None) is not None:
120+
stdout, stderr = self.poco._instrument_proc.communicate()
121+
print('[pocoservice.apk] stdout: {}'.format(stdout))
122+
print('[pocoservice.apk] stderr: {}'.format(stderr))
123+
if not self.stopped():
124+
print('[pocoservice.apk] retrying instrumentation PocoService')
125+
self.poco._start_instrument(self.port_to_ping) # 尝试重启
126+
time.sleep(1)
127+
128+
101129
class AndroidUiautomationPoco(Poco):
102130
"""
103131
Poco Android implementation for testing **Android native apps**.
@@ -179,7 +207,8 @@ def __init__(self, device=None, using_proxy=True, force_restart=False, use_airte
179207
raise RuntimeError("unable to launch AndroidUiautomationPoco")
180208
if ready:
181209
# 首次启动成功后,在后台线程里监控这个进程的状态,保持让它不退出
182-
self._keep_running_instrumentation(p0)
210+
self._keep_running_thread = KeepRunningInstrumentationThread(self, p0)
211+
self._keep_running_thread.start()
183212

184213
endpoint = "http://{}:{}".format(self.device_ip, p1)
185214
agent = AndroidPocoAgent(endpoint, self.ime, use_airtest_input)
@@ -198,22 +227,6 @@ def _is_running(self, package_name):
198227
return True
199228
return False
200229

201-
def _keep_running_instrumentation(self, port_to_ping):
202-
print('[pocoservice.apk] background daemon started.')
203-
204-
def loop():
205-
while True:
206-
if self._instrument_proc is not None:
207-
stdout, stderr = self._instrument_proc.communicate()
208-
print('[pocoservice.apk] stdout: {}'.format(stdout))
209-
print('[pocoservice.apk] stderr: {}'.format(stderr))
210-
print('[pocoservice.apk] retrying instrumentation PocoService')
211-
self._start_instrument(port_to_ping) # 尝试重启
212-
time.sleep(1)
213-
t = threading.Thread(target=loop)
214-
t.daemon = True
215-
t.start()
216-
217230
def _start_instrument(self, port_to_ping, force_restart=False):
218231
if not force_restart:
219232
try:
@@ -281,6 +294,12 @@ def on_pre_action(self, action, ui, args):
281294
msg = msg.decode('utf-8')
282295
snapshot(msg=msg)
283296

297+
def stop_running(self):
298+
print('[pocoservice.apk] stopping PocoService')
299+
self._keep_running_thread.stop()
300+
self._keep_running_thread.join(3)
301+
self.adb_client.shell(['am', 'force-stop', PocoServicePackage])
302+
284303

285304
class AndroidUiautomationHelper(object):
286305
_nuis = {}

0 commit comments

Comments
 (0)