Skip to content

Commit af44aeb

Browse files
committed
Inject connect to ViewClient.connectToDeviceOrExit
- Update findViewBy* to support WindowHiearchyChild properties - Update printFindViewBy* to support WindowHierarchyChild properties - Remove continuation lines in print
1 parent dc45b95 commit af44aeb

4 files changed

Lines changed: 49 additions & 26 deletions

File tree

publish

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ eval $(grep version setup.py | sed 's/,$//')
55
tag="v$version"
66
git tag "$tag" || printf 'WARNING: tag "%s" already exists\n' "$tag"
77
git push --tags
8-
python2.7 setup.py clean build --executable='/usr/bin/env python2.7' bdist_egg bdist_wheel --universal
8+
#python2.7 setup.py clean build --executable='/usr/bin/env python2.7' bdist_egg bdist_wheel --universal
9+
python setup.py clean build --executable='/usr/bin/env python' bdist_egg bdist_wheel --universal
910
# let's use pyenv's twine, installed by pip
1011
twine=~/.pyenv/shims/twine
1112
set -x

src/com/dtmilano/android/viewclient.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2758,8 +2758,8 @@ def setAlarm(timeout):
27582758

27592759
@staticmethod
27602760
def connectToDeviceOrExit(timeout=60, verbose=False, ignoresecuredevice=False, ignoreversioncheck=False,
2761-
serialno=None):
2762-
'''
2761+
serialno=None, connect=adbclient.connect):
2762+
"""
27632763
Connects to a device which serial number is obtained from the script arguments if available
27642764
or using the default regex C{.*}.
27652765
@@ -2782,9 +2782,11 @@ def connectToDeviceOrExit(timeout=60, verbose=False, ignoresecuredevice=False, i
27822782
@param ignoreversioncheck: Ignores the check for a supported ADB version
27832783
@type serialno: str
27842784
@param serialno: The device or emulator serial number
2785+
@type connect: function
2786+
@param connect: Connect method to use to connect to ADB
27852787
27862788
@return: the device and serialno used for the connection
2787-
'''
2789+
"""
27882790
progname = os.path.basename(sys.argv[0])
27892791
if serialno is None:
27902792
# eat all the extra options the invoking script may have added
@@ -2803,7 +2805,7 @@ def connectToDeviceOrExit(timeout=60, verbose=False, ignoresecuredevice=False, i
28032805
ViewClient.setAlarm(timeout + 5)
28042806
# NOTE: timeout is used for 2 different timeouts, the one to set the alarm to timeout the connection with
28052807
# adb and the timeout used by adb (once connected) for the sockets
2806-
device = adbclient.AdbClient(serialno, ignoreversioncheck=ignoreversioncheck, timeout=timeout)
2808+
device = adbclient.AdbClient(serialno, ignoreversioncheck=ignoreversioncheck, timeout=timeout, connect=connect)
28072809
ViewClient.setAlarm(0)
28082810
if verbose:
28092811
print('Connected to device with serialno=%s' % serialno, file=sys.stderr)
@@ -3283,6 +3285,7 @@ def __attributesFromWindowHierarchyChild(child):
32833285

32843286
def __processWindowHierarchyChild(self, node, idCount, version):
32853287
if node.id != 'hierarchy':
3288+
# FIXME: as WindowHierarchyChild is generated we may need a descendant
32863289
# NOTE: we are extending WindowHierarchyChild adding unique_id
32873290
node.unique_id = 'id/no_id/%d' % idCount
32883291
attributes = ViewClient.__attributesFromWindowHierarchyChild(node)
@@ -3656,15 +3659,28 @@ def findViewById(self, viewId, root="ROOT", viewFilter=None):
36563659
if root == "ROOT":
36573660
return self.findViewById(viewId, self.root, viewFilter)
36583661

3659-
if root.getId() == viewId:
3662+
try:
3663+
rootId = root.getId()
3664+
except AttributeError as ex:
3665+
rootId = root.id
3666+
3667+
if rootId == viewId:
36603668
if viewFilter:
36613669
if viewFilter(root):
36623670
return root
36633671
else:
36643672
return root
36653673

36663674
if re.match('^id/no_id', viewId) or re.match('^id/.+/.+', viewId):
3667-
if root.getUniqueId() == viewId:
3675+
try:
3676+
unique_id = root.getUniqueId()
3677+
except AttributeError as ex:
3678+
try:
3679+
unique_id = root.unique_id
3680+
except AttributeError as ex:
3681+
unique_id = -1
3682+
3683+
if unique_id == viewId:
36683684
if viewFilter:
36693685
if viewFilter(root):
36703686
return root;

tools/culebra

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def shortAndLongOptions():
110110

111111
short_opts = CulebraOptions.SHORT_OPTS.replace(':', '')
112112
if len(short_opts) != len(CulebraOptions.LONG_OPTS):
113-
s = ""
113+
_s = ""
114114
for i in range(max(len(short_opts), len(CulebraOptions.LONG_OPTS))):
115115
l = ''
116116
try:
@@ -122,9 +122,9 @@ def shortAndLongOptions():
122122
L = CulebraOptions.LONG_OPTS[i]
123123
except IndexError:
124124
pass
125-
s += "%3s - %s\n" % (l, L)
125+
_s += "%3s - %s\n" % (l, L)
126126
raise Exception('There is a mismatch between short and long options: short=%d, long=%d\n%s' %
127-
(len(short_opts), len(CulebraOptions.LONG_OPTS), s))
127+
(len(short_opts), len(CulebraOptions.LONG_OPTS), _s))
128128
t = tuple(short_opts) + tuple(CulebraOptions.LONG_OPTS)
129129
l2 = int(len(t) / 2)
130130
sl = []
@@ -259,9 +259,11 @@ def printFindViewWithText(view, useregexp, op=Operation.ASSIGN, arg=None):
259259
@param view: the View
260260
'''
261261

262-
text = view.text
263-
if callable(text):
264-
text = text()
262+
if isinstance(view, View):
263+
text = view.getText()
264+
else:
265+
text = view.text
266+
265267
isUnicode = isinstance(text, str)
266268
if isUnicode and sys.stdout.encoding is None:
267269
warnings.warn('''\
@@ -348,7 +350,11 @@ def printFindViewWithContentDescription(view, useregexp, op=Operation.ASSIGN, ar
348350
@param view: the View
349351
'''
350352

351-
contentDescription = view.content_description
353+
if isinstance(view, View):
354+
contentDescription = view.getContentDescription()
355+
else:
356+
contentDescription = view.content_description
357+
352358
if contentDescription:
353359
var = variableNameFromIdOrKey(view)
354360
if useregexp:
@@ -430,8 +436,12 @@ def printFindViewById(view, op=Operation.ASSIGN, arg=None):
430436
@param view: the View
431437
'''
432438

439+
if isinstance(view, View):
440+
_id = view.getId() or view.getUniqueId()
441+
else:
442+
_id = view.resource_id or view.unique_id
443+
433444
var = variableNameFromIdOrKey(view)
434-
_id = view.id if view.id else view.unique_id
435445
if op == Operation.ASSIGN:
436446
if options[CulebraOptions.MULTI_DEVICE]:
437447
logAction('finding view with id=%s on ${serialno}' % _id)
@@ -1397,25 +1407,21 @@ else:
13971407
print('''
13981408
kwargs1 = %s
13991409
device, serialno = ViewClient.connectToDeviceOrExit(**kwargs1)
1400-
''' % kwargs1, end=' ')
1410+
''' % kwargs1)
14011411

14021412
if options[CulebraOptions.START_ACTIVITY] and not options[CulebraOptions.UNIT_TEST_METHOD]:
1403-
print('''\
1404-
device.startActivity(component='%s')''' % options[CulebraOptions.START_ACTIVITY])
1413+
print('''device.startActivity(component='%s')''' % options[CulebraOptions.START_ACTIVITY])
14051414

14061415
if not options[CulebraOptions.UNIT_TEST_METHOD]:
1407-
print('''\
1408-
kwargs2 = %s
1416+
print('''kwargs2 = %s
14091417
vc = ViewClient(device, serialno, **kwargs2)
1410-
#vc.dump(window=%s) # FIXME: seems not needed
1411-
''' % (kwargs2, getWindowOption()))
1418+
''' % (kwargs2))
14121419

14131420
if options[CulebraOptions.USE_DICTIONARY]:
14141421
print('''views = dict()''')
14151422

14161423
if options[CulebraOptions.SAVE_SCREENSHOT]:
1417-
print('''\
1418-
vc.writeImageToFile('%s', 'PNG', %s, %s, %s)
1424+
print('''vc.writeImageToFile('%s', 'PNG', %s, %s, %s)
14191425
''' % (options[CulebraOptions.SAVE_SCREENSHOT], options[CulebraOptions.DEVICE_ART], options[CulebraOptions.DROP_SHADOW],
14201426
options[CulebraOptions.SCREEN_GLARE]))
14211427

tools/dump

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def usage(exitVal=1):
121121
sys.exit(exitVal)
122122

123123

124-
def help():
124+
def _help():
125125
print(USAGE % progname, file=sys.stderr)
126126
print(file=sys.stderr)
127127
print("Options:", file=sys.stderr)
@@ -164,7 +164,7 @@ transform = ViewClient.TRAVERSE_CIT
164164
for o, a in opts:
165165
o = o.strip('-')
166166
if o in ['H', HELP]:
167-
help()
167+
_help()
168168
elif o in ['V', VERBOSE]:
169169
kwargs1[VERBOSE] = True
170170
elif o in ['v', VERSION]:

0 commit comments

Comments
 (0)