Skip to content

Commit 9176b47

Browse files
committed
Standardized root parameter for all tree methods
- Added root with default value - Improved View.__str__() - Refactored tree traversal methods - Added TRAVERSE_CITPS
1 parent 579227f commit 9176b47

2 files changed

Lines changed: 259 additions & 31 deletions

File tree

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

Lines changed: 64 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -613,12 +613,15 @@ def __tinyStr__(self):
613613
def __str__(self):
614614
__str = "View["
615615
if "class" in self.map:
616-
__str += " class=" + self.map["class"] + " "
616+
__str += " class=" + self.map["class"].__str__() + " "
617617
for a in self.map:
618-
__str += a + "=" + self.map[a] + " "
618+
__str += a + "=" + self.map[a].__str__() + " "
619619
__str += "] parent="
620-
if self.parent and "class" in self.parent.map:
621-
__str += "%s" % self.parent.map["class"]
620+
if self.parent:
621+
if "class" in self.parent.map:
622+
__str += "%s" % self.parent.map["class"]
623+
else:
624+
__str += self.parent.getId().__str__()
622625
else:
623626
__str += "None"
624627

@@ -742,18 +745,20 @@ def connectToDeviceOrExit(timeout=60):
742745
return device, serialno
743746

744747
@staticmethod
745-
def traverseShowClassIdAndText(view):
748+
def traverseShowClassIdAndText(view, extraInfo=None):
746749
'''
747750
Shows the View class, id and text if available.
748751
This function can be used as a transform function to L{ViewClient.traverse()}
749752
750753
@type view: I{View}
751754
@param view: the View
755+
@type extraInfo: method
756+
@param extraInfo: the View method to add extra info
752757
@return: the string containing class, id, and text if available
753758
'''
754759

755760
try:
756-
return "%s %s %s" % (view.getClass(), view.getId(), view.getText())
761+
return "%s %s %s%s" % (view.getClass(), view.getId(), view.getText(), " " + extraInfo(view).__str__() if extraInfo != None else '')
757762
except Exception, e:
758763
return "Exception in view=%s: %s" % (view.__smallStr__(), e)
759764

@@ -768,16 +773,28 @@ def traverseShowClassIdTextAndCenter(view):
768773
@return: the string containing class, id, and text if available
769774
'''
770775

771-
try:
772-
return "%s %s %s %s" % (view.getClass(), view.getId(), view.getText(), view.getCenter())
773-
except Exception, e:
774-
return "Exception in view=%s: %s" % (view.__smallStr__(), e)
776+
return ViewClient.traverseShowClassIdAndText(view, View.getCenter)
777+
778+
@staticmethod
779+
def traverseShowClassIdTextPositionAndSize(view):
780+
'''
781+
Shows the View class, id and text if available.
782+
This function can be used as a transform function to L{ViewClient.traverse()}
783+
784+
@type view: I{View}
785+
@param view: the View
786+
@return: the string containing class, id, and text if available
787+
'''
788+
789+
return ViewClient.traverseShowClassIdAndText(view, View.getPositionAndSize)
775790

776791
# methods that can be used to transform ViewClient.traverse output
777792
TRAVERSE_CIT = traverseShowClassIdAndText
778793
''' An alias for L{traverseShowClassIdAndText(view)} '''
779794
TRAVERSE_CITC = traverseShowClassIdTextAndCenter
780795
''' An alias for L{traverseShowClassIdTextAndCenter(view)} '''
796+
TRAVERSE_CITPS = traverseShowClassIdTextPositionAndSize
797+
''' An alias for L{traverseShowClassIdTextPositionAndSize(view)} '''
781798

782799
def assertServiceResponse(self, response):
783800
'''
@@ -962,7 +979,7 @@ def getRoot(self):
962979
'''
963980
return self.root
964981

965-
def traverse(self, root="ROOT", indent="", transform=View.__str__):
982+
def traverse(self, root="ROOT", indent="", transform=View.__str__, stream=sys.stdout):
966983
'''
967984
Traverses the C{View} tree and prints its nodes.
968985
@@ -979,15 +996,15 @@ def traverse(self, root="ROOT", indent="", transform=View.__str__):
979996
if not root:
980997
return
981998

982-
if root == "ROOT":
999+
if type(root) == types.StringType and root == "ROOT":
9831000
root = self.root
9841001

9851002
s = transform(root)
9861003
if s:
987-
print "%s%s" % (indent, s)
1004+
print >>stream, "%s%s" % (indent, s)
9881005

9891006
for ch in root.children:
990-
self.traverse(ch, indent=indent+" ", transform=transform)
1007+
self.traverse(ch, indent=indent+" ", transform=transform, stream=stream)
9911008

9921009
def dump(self, windowId=-1, sleep=1):
9931010
'''
@@ -1063,20 +1080,34 @@ def findViewByIdOrRaise(self, viewId, root="ROOT"):
10631080
if view:
10641081
return view
10651082
else:
1066-
raise ViewNotFoundException("Couldn't find view with ID=%s" % viewId)
1083+
raise ViewNotFoundException("Couldn't find view with ID=%s in tree with root=%s" % (viewId, root))
10671084

1068-
def findViewByTag(self, tag):
1085+
def findViewByTag(self, tag, root="ROOT"):
10691086
'''
10701087
Finds the View with the specified tag
10711088
'''
10721089

1073-
return self.findViewWithAttribute('getTag()', tag)
1090+
return self.findViewWithAttribute('getTag()', tag, root)
1091+
1092+
def findViewByTagOrRaise(self, tag, root="ROOT"):
1093+
'''
1094+
Finds the View with the specified tag or raise a ViewNotFoundException
1095+
'''
1096+
1097+
view = self.findViewWithAttribute('getTag()', tag, root)
1098+
if view:
1099+
return view
1100+
else:
1101+
raise ViewNotFoundException("Couldn't find view with tag=%s in tree with root=%s" % (tag, root))
10741102

10751103
def __findViewWithAttributeInTree(self, attr, val, root):
10761104
if not self.root:
10771105
print >>sys.stderr, "ERROR: no root, did you forget to call dump()?"
10781106
return None
10791107

1108+
if type(root) == types.StringType and root == "ROOT":
1109+
root = self.root
1110+
10801111
if DEBUG: print >>sys.stderr, "__findViewWithAttributeInTree: checking if root=%s has attr=%s == %s" % (root.__smallStr__(), attr, val)
10811112

10821113
if root and attr in root.map and root.map[attr] == val:
@@ -1095,6 +1126,9 @@ def __findViewWithAttributeInTreeThatMatches(self, attr, regex, root, rlist=[]):
10951126
print >>sys.stderr, "ERROR: no root, did you forget to call dump()?"
10961127
return None
10971128

1129+
if type(root) == types.StringType and root == "ROOT":
1130+
root = self.root
1131+
10981132
if DEBUG: print >>sys.stderr, "__findViewWithAttributeInTreeThatMatches: checking if root=%s attr=%s matches %s" % (root.__smallStr__(), attr, regex)
10991133

11001134
if root and attr in root.map and regex.match(root.map[attr]):
@@ -1113,38 +1147,38 @@ def __findViewWithAttributeInTreeThatMatches(self, attr, regex, root, rlist=[]):
11131147
return None
11141148
#return rlist
11151149

1116-
def findViewWithAttribute(self, attr, val):
1150+
def findViewWithAttribute(self, attr, val, root="ROOT"):
11171151
'''
11181152
Finds the View with the specified attribute and value
11191153
'''
11201154

1121-
return self.__findViewWithAttributeInTree(attr, val, self.root)
1155+
return self.__findViewWithAttributeInTree(attr, val, root)
11221156

1123-
def findViewWithAttributeOrRaise(self, attr, val):
1157+
def findViewWithAttributeOrRaise(self, attr, val, root="ROOT"):
11241158
'''
11251159
Finds the View or raise a ViewNotFoundException.
11261160
11271161
@return: the View found
11281162
@raise ViewNotFoundException: raise the exception if View not found
11291163
'''
11301164

1131-
view = self.findViewWithAttribute(attr, val)
1165+
view = self.findViewWithAttribute(attr, val, root)
11321166
if view:
11331167
return view
11341168
else:
1135-
raise ViewNotFoundException("Couldn't find View with %s='%s'" % (attr, val))
1169+
raise ViewNotFoundException("Couldn't find View with %s='%s' in tree with root=%s" % (attr, val, root))
11361170

1137-
def findViewWithAttributeThatMatches(self, attr, regex):
1171+
def findViewWithAttributeThatMatches(self, attr, regex, root="ROOT"):
11381172
'''
11391173
Finds the list of Views with the specified attribute matching
11401174
regex
11411175
'''
11421176

1143-
return self.__findViewWithAttributeInTreeThatMatches(attr, regex, self.root)
1177+
return self.__findViewWithAttributeInTreeThatMatches(attr, regex, root)
11441178

1145-
def findViewWithText(self, text):
1179+
def findViewWithText(self, text, root="ROOT"):
11461180
if type(text).__name__ == 'PatternObject':
1147-
return self.findViewWithAttributeThatMatches(TEXT_PROPERTY, text)
1181+
return self.findViewWithAttributeThatMatches(TEXT_PROPERTY, text, root)
11481182
#l = self.findViewWithAttributeThatMatches(TEXT_PROPERTY, text)
11491183
#ll = len(l)
11501184
#if ll == 0:
@@ -1155,21 +1189,21 @@ def findViewWithText(self, text):
11551189
# print >>sys.stderr, "WARNING: findViewWithAttributeThatMatches invoked by findViewWithText returns %d items." % ll
11561190
# return l
11571191
else:
1158-
return self.findViewWithAttribute(TEXT_PROPERTY, text)
1192+
return self.findViewWithAttribute(TEXT_PROPERTY, text, root)
11591193

1160-
def findViewWithTextOrRaise(self, text):
1194+
def findViewWithTextOrRaise(self, text, root="ROOT"):
11611195
'''
11621196
Finds the View or raise a ViewNotFoundException.
11631197
11641198
@return: the View found
11651199
@raise ViewNotFoundException: raise the exception if View not found
11661200
'''
11671201

1168-
view = self.findViewWithText(text)
1202+
view = self.findViewWithText(text, root)
11691203
if view:
11701204
return view
11711205
else:
1172-
raise ViewNotFoundException("Coulnd't find View with text='%s'" % text)
1206+
raise ViewNotFoundException("Coulnd't find View with text='%s' in tree with root=%s" % (text, root))
11731207

11741208
def getViewIds(self):
11751209
'''

0 commit comments

Comments
 (0)