Skip to content

Commit a71afe6

Browse files
committed
UiAutomatorHelper take optional adbClient argument
- Change examples
1 parent 1db2513 commit a71afe6

6 files changed

Lines changed: 30 additions & 27 deletions

File tree

examples/helper/calculator-wait-uiautomator-helper renamed to examples/helper/calculator-wait-by-selector

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#! /usr/bin/env python3
22
# -*- coding: utf-8 -*-
33
"""
4-
Copyright (C) 2013-2019 Diego Torres Milano
4+
Copyright (C) 2013-2023 Diego Torres Milano
55
Created on 2022-01-15 by Culebra v20.4.5
66
__ __ __ __
77
/ \ / \ / \ / \
@@ -23,18 +23,14 @@ TAG = 'CULEBRA'
2323

2424
def start_calculator():
2525
time.sleep(10)
26-
vc.uiAutomatorHelper.target_context.start_activity('com.google.android.calculator',
27-
'com.android.calculator2.Calculator')
26+
helper.target_context.start_activity('com.google.android.calculator',
27+
'com.android.calculator2.Calculator')
2828

2929

30-
kwargs1 = {'verbose': False}
31-
device, serialno = ViewClient.connectToDeviceOrExit(**kwargs1)
32-
33-
kwargs2 = {'useuiautomatorhelper': True, 'debug': {}}
34-
vc = ViewClient(device, serialno, **kwargs2)
30+
helper = ViewClient.view_client_helper()
3531

3632
threading.Thread(target=start_calculator).start()
3733

38-
search_condition_ref = vc.uiAutomatorHelper.until.find_object(by_selector='text@π').oid
34+
search_condition_ref = helper.until.find_object(by_selector='text@π')
3935
print("Let's wait until PI appears on the screen (Calculator)...")
40-
print(vc.uiAutomatorHelper.ui_device.wait(search_condition_ref=search_condition_ref, timeout=30000))
36+
print(helper.ui_device.wait(oid=search_condition_ref.oid, timeout=30000))

examples/helper/write_image_to_file_uiautomator_helper.py renamed to examples/helper/culebra/write_image_to_file_uiautomator_helper.py

File renamed without changes.

examples/helper/start-activity-followed-by-dump

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ if len(sys.argv) != 2:
1010

1111
app = sys.argv.pop(1)
1212
device, serialno = ViewClient.connectToDeviceOrExit()
13+
# we need adbClient because we call `forceStop()` in this script
1314
helper = ViewClient(device, serialno, useuiautomatorhelper=True).uiAutomatorHelper
1415

1516
t = [time.time()]

src/com/dtmilano/android/common.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ def is_exe(_fpath, _isWindows):
9595
return None
9696

9797

98-
def obtainAdbPath():
99-
'''
98+
def obtainAdbPath() -> str:
99+
"""
100100
Obtains the ADB path attempting know locations for different OSs
101-
'''
101+
"""
102102

103103
FORCE_FAIL = False
104104
''' Sometimes, you want it to fail to check the error messages '''

src/com/dtmilano/android/uiautomator/uiautomatorhelper.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22
"""
3-
Copyright (C) 2012-2022 Diego Torres Milano
3+
Copyright (C) 2012-2023 Diego Torres Milano
44
Created on Feb 2, 2015
55
66
Licensed under the Apache License, Version 2.0 (the "License");
@@ -112,7 +112,8 @@ class UiAutomatorHelper:
112112
TEST_CLASS = PACKAGE + '.test'
113113
TEST_RUNNER = 'com.dtmilano.android.culebratester2.UiAutomatorHelper'
114114

115-
def __init__(self, adbclient, adb=None, localport=9987, remoteport=9987, hostname='localhost', api_version='v2'):
115+
def __init__(self, adbclient: Optional[AdbClient] = None, adb=None, localport=9987, remoteport=9987,
116+
hostname='localhost', api_version='v2'):
116117
"""
117118
UiAutomatorHelper constructor used when the backend selected is **CulebraTester2-public**.
118119
This class holds references to the different API's:
@@ -124,14 +125,14 @@ def __init__(self, adbclient, adb=None, localport=9987, remoteport=9987, hostnam
124125
- UiObject2
125126
- Until
126127
127-
:param adbclient: the adb client
128-
:param adb: adb if known
129-
:param localport: the local port used in CulebraTester2-public port forwarding
130-
:param remoteport: the remote port used in CulebraTester2-public port forwarding
128+
:param adbclient: the adb client (optional and usually not needed for normal cases with this backend)
129+
:param adb: adb if known (also optional)
130+
:param localport: the local port used in CulebraTester2-public port forwarding (must be forwarded already)
131+
:param remoteport: the remote port used in CulebraTester2-public port forwarding (not usually needed)
131132
:param hostname: the hostname used by CulebraTester2-public
132133
:param api_version: the api version
133134
"""
134-
self.adbClient = adbclient
135+
self.adbClient: Optional[AdbClient] = adbclient
135136
''' The adb client (a.k.a. device) '''
136137
self.adb = self.__whichAdb(adb)
137138
''' The adb command '''
@@ -189,7 +190,7 @@ def __connectSession(self):
189190
return session
190191

191192
@staticmethod
192-
def __whichAdb(adb):
193+
def __whichAdb(adb: Optional[str]):
193194
if adb:
194195
if not os.access(adb, os.X_OK):
195196
raise Exception('adb="%s" is not executable' % adb)
@@ -200,7 +201,10 @@ def __whichAdb(adb):
200201

201202
return adb
202203

203-
def __redirectPort(self, localport, remoteport):
204+
def __redirectPort(self, localport: int, remoteport: int) -> None:
205+
if not self.adbClient:
206+
print("__redirectPort: cannot redirect port when adbClient is not set")
207+
return
204208
self.localPort = localport
205209
self.remotePort = remoteport
206210
subprocess.check_call([self.adb, '-s', self.adbClient.serialno, 'forward', 'tcp:%d' % self.localPort,
@@ -209,6 +213,8 @@ def __redirectPort(self, localport, remoteport):
209213
def __runTests(self):
210214
if DEBUG:
211215
print("__runTests: start", file=sys.stderr)
216+
if not self.adbClient:
217+
return
212218
# We need a new AdbClient instance with timeout=None (means, no timeout) for the long running test service
213219
newAdbClient = AdbClient(self.adbClient.serialno, self.adbClient.hostname, self.adbClient.port, timeout=None)
214220
self.thread = RunTestsThread(adbClient=newAdbClient, testClass=self.TEST_CLASS, testRunner=self.TEST_RUNNER)

src/com/dtmilano/android/viewclient.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22
"""
3-
Copyright (C) 2012-2022 Diego Torres Milano
3+
Copyright (C) 2012-2023 Diego Torres Milano
44
Created on Feb 2, 2012
55
66
Licensed under the Apache License, Version 2.0 (the "License");
@@ -26,7 +26,7 @@
2626
import culebratester_client
2727
from culebratester_client import WindowHierarchyChild, WindowHierarchy
2828

29-
__version__ = '22.7.2'
29+
__version__ = '23.0.0'
3030

3131
import sys
3232
import warnings
@@ -2761,8 +2761,9 @@ def __del__(self):
27612761
self.uiAutomatorHelper.quit()
27622762

27632763
@staticmethod
2764-
def view_client_helper(kato=False):
2765-
helper = ViewClient(*ViewClient.connectToDeviceOrExit(), useuiautomatorhelper=True).uiAutomatorHelper
2764+
def view_client_helper(hostname: str = "localhost", port: int = CULEBRATESTER2_PORT,
2765+
kato: bool = False) -> UiAutomatorHelper:
2766+
helper = UiAutomatorHelper(hostname=hostname, localport=port)
27662767
helper.kato.enabled = kato
27672768
return helper
27682769

@@ -2773,7 +2774,6 @@ def __obtainAdbPath():
27732774
@staticmethod
27742775
def __mapSerialNo(serialno):
27752776
serialno = serialno.strip()
2776-
# ipRE = re.compile('^\d+\.\d+.\d+.\d+$')
27772777
if IP_RE.match(serialno):
27782778
if DEBUG_DEVICE: print("ViewClient: adding default port to serialno", serialno, ADB_DEFAULT_PORT,
27792779
file=sys.stderr)

0 commit comments

Comments
 (0)