Skip to content

Commit 46fddba

Browse files
committed
Fixed API 10 support (mainly text property)
- Changed browser-view-page-source.py to support API 10 - Added check for SD card mounted when UiAutomator dump fails - Added ViewClient.getSdkVersion()
1 parent b645bd9 commit 46fddba

2 files changed

Lines changed: 33 additions & 15 deletions

File tree

AndroidViewClient/examples/browser-view-page-source.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,30 @@
4040
device, serialno = ViewClient.connectToDeviceOrExit()
4141

4242
device.startActivity(component=COMPONENT, uri=URI)
43-
MonkeyRunner.sleep(3)
43+
MonkeyRunner.sleep(5)
4444

4545
vc = ViewClient(device=device, serialno=serialno)
46+
sdkVersion = vc.getSdkVersion()
4647

47-
device.drag((240, 180), (240, 420), 10, 10)
48+
if sdkVersion > 10:
49+
device.drag((240, 180), (240, 420), 1, 20)
50+
else:
51+
for i in range(10):
52+
device.press('KEYCODE_DPAD_UP', MonkeyDevice.DOWN_AND_UP)
53+
MonkeyRunner.sleep(1)
4854

49-
url = vc.findViewByIdOrRaise('id/url')
55+
url = vc.findViewByIdOrRaise('id/url' if sdkVersion > 10 else 'id/title')
5056
url.touch()
5157
MonkeyRunner.sleep(1)
5258

5359
device.press('KEYCODE_DEL', MonkeyDevice.DOWN_AND_UP)
5460
for c in VPS:
55-
device.type(c)
61+
device.type(c)
62+
MonkeyRunner.sleep(1)
5663
device.press('KEYCODE_ENTER', MonkeyDevice.DOWN_AND_UP)
5764
MonkeyRunner.sleep(3)
5865

5966
vc.dump()
6067
print vc.findViewByIdOrRaise('id/message').getText().replace('\\n', "\n")
6168

62-
device.press('KEYCODE_BACK', MonkeyDevice.DOWN_AND_UP)
69+
device.press('KEYCODE_BACK' if sdkVersion > 10 else 'KEYCODE_ENTER', MonkeyDevice.DOWN_AND_UP)

AndroidViewClient/src/com/dtmilano/android/viewclient.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
@author: diego
1818
'''
1919

20-
__version__ = '2.3.6'
20+
__version__ = '2.3.7'
2121

2222
import sys
2323
import subprocess
@@ -33,11 +33,11 @@
3333
from com.android.monkeyrunner import MonkeyDevice, MonkeyRunner
3434

3535
DEBUG = False
36-
DEBUG_DEVICE = DEBUG and True
37-
DEBUG_RECEIVED = DEBUG and False
36+
DEBUG_DEVICE = DEBUG and False
37+
DEBUG_RECEIVED = DEBUG and True
3838
DEBUG_TREE = DEBUG and False
3939
DEBUG_GETATTR = DEBUG and False
40-
DEBUG_COORDS = DEBUG and True
40+
DEBUG_COORDS = DEBUG and False
4141
DEBUG_TOUCH = DEBUG and False
4242
DEBUG_STATUSBAR = DEBUG and False
4343
DEBUG_WINDOWS = DEBUG and False
@@ -927,8 +927,10 @@ def __init__(self, device, serialno, adb=None, autodump=True, forceviewserveruse
927927
if self.useUiAutomator:
928928
self.textProperty = TEXT_PROPERTY_UI_AUTOMATOR
929929
else:
930-
# FIXME: not true for API <= 10
931-
self.textProperty = TEXT_PROPERTY
930+
if self.build[VERSION_SDK_PROPERTY] <= 10:
931+
self.textProperty = TEXT_PROPERTY_API_10
932+
else:
933+
self.textProperty = TEXT_PROPERTY
932934
if startviewserver:
933935
if not self.serviceResponse(device.shell('service call window 3')):
934936
try:
@@ -1296,7 +1298,7 @@ def __splitAttrs(self, strArgs, addViewToViewsById=False):
12961298
raise RuntimeError("This method is not compatible with UIAutomator")
12971299
# replace the spaces in text:mText to preserve them in later split
12981300
# they are translated back after the attribute matches
1299-
textRE = re.compile('%s=%s,' % (TEXT_PROPERTY, __nd('len')))
1301+
textRE = re.compile('%s=%s,' % (self.textProperty, __nd('len')))
13001302
m = textRE.search(strArgs)
13011303
if m:
13021304
__textStart = m.end()
@@ -1316,7 +1318,7 @@ def __splitAttrs(self, strArgs, addViewToViewsById=False):
13161318
if m:
13171319
viewId = m.group('viewId')
13181320
if DEBUG:
1319-
print >>sys.stderr, "found %s" % viewId
1321+
print >>sys.stderr, "found view with id=%s" % viewId
13201322

13211323
for attr in strArgs.split():
13221324
m = attrRE.match(attr)
@@ -1327,7 +1329,7 @@ def __splitAttrs(self, strArgs, addViewToViewsById=False):
13271329
__val = m.group('val')
13281330
if WARNINGS and __len != len(__val):
13291331
warnings.warn("Invalid len: expected: %d found: %d s=%s e=%s" % (__len, len(__val), __val[:50], __val[-50:]))
1330-
if __attr == TEXT_PROPERTY:
1332+
if __attr == self.textProperty:
13311333
# restore spaces that have been replaced
13321334
__val = __val.replace(WS, ' ')
13331335
attrs[__attr + __parens] = __val
@@ -1476,6 +1478,7 @@ def dump(self, windowId=-1, sleep=1):
14761478
MonkeyRunner.sleep(sleep)
14771479

14781480
if self.useUiAutomator:
1481+
# FIXME: this might not be the path on some devices
14791482
windowDump = '/mnt/sdcard/window_dump.xml'
14801483
output = self.device.shell('uiautomator dump %s' % windowDump)
14811484
if not output:
@@ -1486,7 +1489,12 @@ def dump(self, windowId=-1, sleep=1):
14861489
if received:
14871490
received = received.encode('ascii', 'ignore')
14881491
else:
1489-
raise RuntimeError("ERROR: Received empty UIAutomator dump")
1492+
msg = ''
1493+
output = self.device.shell('mount')
1494+
if output:
1495+
if not re.search('sdcard', output):
1496+
msg = ", it seems there's no sdcard mounted and uiautomator creates dump there."
1497+
raise RuntimeError("ERROR: Received empty UIAutomator dump" + msg)
14901498
if DEBUG:
14911499
self.received = received
14921500
if DEBUG_RECEIVED:
@@ -1707,6 +1715,9 @@ def getViewIds(self):
17071715

17081716
def __getFocusedWindowPosition(self):
17091717
return self.__getFocusedWindowId()
1718+
1719+
def getSdkVersion(self):
1720+
return self.build[VERSION_SDK_PROPERTY]
17101721

17111722

17121723
if __name__ == "__main__":

0 commit comments

Comments
 (0)