Skip to content

Commit b9043a2

Browse files
committed
Fixed text property use with UiAutomator
- Added trashcan-fullscreenactivity-touches example
1 parent 0cedd54 commit b9043a2

2 files changed

Lines changed: 52 additions & 3 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#! /usr/bin/env monkeyrunner
2+
'''
3+
Copyright (C) 2012 Diego Torres Milano
4+
Created on Mar 13, 2012
5+
6+
@author: diego
7+
'''
8+
9+
10+
import re
11+
import sys
12+
import os
13+
import string
14+
15+
# This must be imported before MonkeyRunner and MonkeyDevice,
16+
# otherwise the import fails.
17+
# PyDev sets PYTHONPATH, use it
18+
try:
19+
for p in os.environ['PYTHONPATH'].split(':'):
20+
if not p in sys.path:
21+
sys.path.append(p)
22+
except:
23+
pass
24+
25+
try:
26+
sys.path.append(os.path.join(os.environ['ANDROID_VIEW_CLIENT_HOME'], 'src'))
27+
except:
28+
pass
29+
30+
from com.dtmilano.android.viewclient import ViewClient
31+
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
32+
33+
package = 'com.example.trashcan'
34+
activity = '.FullScreenActivity'
35+
component = package + "/" + activity
36+
37+
device, serialno = ViewClient.connectToDeviceOrExit()
38+
#device.startActivity(component=component)
39+
#MonkeyRunner.sleep(3)
40+
41+
vc = ViewClient(device, serialno)
42+
button = vc.findViewWithTextOrRaise('Button')
43+
button.touch()
44+
toggle = vc.findViewWithTextOrRaise(re.compile('(ON)|(OFF)'))
45+
toggle.touch()

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,11 @@ def __init__(self, device, serialno, adb=None, autodump=True, localport=VIEW_SER
906906
self.useUiAutomator = (self.build[VERSION_SDK_PROPERTY] >= 16) # jelly bean 4.1 & 4.2
907907
''' If UIAutomator is supported by the device it will be used '''
908908

909-
if not self.useUiAutomator:
909+
if self.useUiAutomator:
910+
self.textProperty = TEXT_PROPERTY_UI_AUTOMATOR
911+
else:
912+
# FIXME: not true for API <= 10
913+
self.textProperty = TEXT_PROPERTY
910914
if startviewserver:
911915
if not self.serviceResponse(device.shell('service call window 3')):
912916
try:
@@ -1595,7 +1599,7 @@ def findViewWithAttributeThatMatches(self, attr, regex, root="ROOT"):
15951599

15961600
def findViewWithText(self, text, root="ROOT"):
15971601
if type(text).__name__ == 'PatternObject':
1598-
return self.findViewWithAttributeThatMatches(TEXT_PROPERTY, text, root)
1602+
return self.findViewWithAttributeThatMatches(self.textProperty, text, root)
15991603
#l = self.findViewWithAttributeThatMatches(TEXT_PROPERTY, text)
16001604
#ll = len(l)
16011605
#if ll == 0:
@@ -1606,7 +1610,7 @@ def findViewWithText(self, text, root="ROOT"):
16061610
# print >>sys.stderr, "WARNING: findViewWithAttributeThatMatches invoked by findViewWithText returns %d items." % ll
16071611
# return l
16081612
else:
1609-
return self.findViewWithAttribute(TEXT_PROPERTY, text, root)
1613+
return self.findViewWithAttribute(self.textProperty, text, root)
16101614

16111615
def findViewWithTextOrRaise(self, text, root="ROOT"):
16121616
'''

0 commit comments

Comments
 (0)