Permalink
Browse files

Improved scripter.py

- Added command line options to scripter.py
- Added verbose message to viewclient.py
- Added error message when regexp not found
  • Loading branch information...
1 parent 3e46c9f commit 759d89b414d3d87cd77c6a6d83133d8c38af4447 @dtmilano committed Apr 2, 2013
Showing with 69 additions and 18 deletions.
  1. +7 −1 AndroidViewClient/src/com/dtmilano/android/viewclient.py
  2. +62 −17 AndroidViewClient/tools/scripter.py
@@ -1165,6 +1165,8 @@ def connectToDeviceOrExit(timeout=60, verbose=False, ignoresecuredevice=False, s
if re.search("[.*()+]", serialno) and not re.search("(\d{1,3}\.){3}\d{1,3}", serialno):
# if a regex was used we have to determine the serialno used
serialno = ViewClient.__obtainDeviceSerialNumber(device)
+ if verbose:
+ print >> sys.stderr, 'Actual device serialno=%s' % serialno
return device, serialno
@staticmethod
@@ -1738,7 +1740,11 @@ def findViewWithTextOrRaise(self, text, root="ROOT"):
if view:
return view
else:
- raise ViewNotFoundException("Coulnd't find View with text='%s' in tree with root=%s" % (text, root))
+ if type(text).__name__ == 'PatternObject':
+ msg = "Couldn't find View with text that matches '%s' in tree with root=%s" % (text.pattern, root)
+ else:
+ msg = "Couldn't find View with text='%s' in tree with root=%s" % (text, root)
+ raise ViewNotFoundException(msg)
def findViewWithContentDescription(self, contentdescription, root="ROOT"):
'''
@@ -1,4 +1,5 @@
#! /usr/bin/env monkeyrunner
+# -*- coding: utf-8 -*-
'''
Copyright (C) 2013 Diego Torres Milano
Created on Mar 28, 2013
@@ -9,7 +10,7 @@
@author: diego
'''
-__version__ = '0.9.0'
+__version__ = '0.9.2'
import re
import sys
@@ -36,19 +37,60 @@
from com.dtmilano.android.viewclient import ViewClient
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
-
+
HELP = 'help'
VERBOSE = 'verbose'
IGNORE_SECURE_DEVICE = 'ignore-secure-device'
FORCE_VIEW_SERVER_USE = 'force-view-server-use'
DO_NOT_START_VIEW_SERVER = 'do-not-start-view-server'
FIND_VIEWS_BY_ID = 'find-views-by-id'
FIND_VIEWS_WITH_TEXT = 'find-views-with-text'
+USE_REGEXPS = 'use-regexps'
+VERBOSE_COMMENTS = 'verbose-comments'
# -u,-s,-p,-v eaten by monkeyrunner
+SHORT_OPTS = 'HVIFSitrC'
LONG_OPTS = [HELP, VERBOSE, IGNORE_SECURE_DEVICE, FORCE_VIEW_SERVER_USE, DO_NOT_START_VIEW_SERVER,
- FIND_VIEWS_BY_ID, FIND_VIEWS_WITH_TEXT]
+ FIND_VIEWS_BY_ID, FIND_VIEWS_WITH_TEXT, USE_REGEXPS, VERBOSE_COMMENTS]
ID_RE = re.compile('id/([^/]*)(/(\d+))?')
+def shortAndLongOptions():
+ '''
+ @return: the list of corresponding (short-option, long-option) tuples
+ '''
+
+ if len(SHORT_OPTS) != len(LONG_OPTS):
+ raise Exception('There is a mismatch between short and long options')
+ t = tuple(SHORT_OPTS) + tuple(LONG_OPTS)
+ l2 = len(t)/2
+ sl = []
+ for i in range(l2):
+ sl.append((t[i], t[i+l2]))
+ return sl
+
+def usage(exitVal=1):
+ print >> sys.stderr, 'usage: scripter.py ',
+ for so, lo in shortAndLongOptions():
+ print >> sys.stderr, '[-%c|--%s]' % (so, lo),
+ print >> sys.stderr, '[serialno]'
+ sys.exit(exitVal)
+
+def verboseComments(view):
+ if options[VERBOSE_COMMENTS]:
+ print '\n# class=%s' % view.getClass(),
+ try:
+ text = view.getText()
+ if text:
+ print 'text="%s"' % text,
+ except:
+ pass
+ try:
+ tag = view.getTag()
+ if tab != 'null':
+ print 'tag=%s' % tag
+ except:
+ pass
+ print
+
def variableNameFromId(id):
'''
Returns a suitable variable name from the id.
@@ -79,10 +121,7 @@ def traverseAndPrintFindViewById(view):
id = view.getUniqueId()
var = variableNameFromId(id)
- try:
- print '# tag=%s' % view.getTag()
- except:
- pass
+ verboseComments(view)
print '%s = vc.findViewByIdOrRaise("%s")' % (var, id)
def traverseAndPrintFindViewWithText(view):
@@ -96,24 +135,25 @@ def traverseAndPrintFindViewWithText(view):
id = view.getUniqueId()
text = view.getText()
if text:
+ verboseComments(view)
var = variableNameFromId(id)
- print '%s = vc.findViewWithTextOrRaise("%s")' % (var, text)
- else:
+ if options[USE_REGEXPS]:
+ text = "re.compile('%s')" % text
+ else:
+ text = "'%s'" % text
+ print '%s = vc.findViewWithTextOrRaise(%s)' % (var, text)
+ elif kwargs1[VERBOSE]:
warnings.warn('View with id=%s has no text' % id)
-def usage(exitVal=1):
- print >> sys.stderr, 'usage: scripter.py [-H|--%s] [-V|--%s] [-I|--%s] [-F|--%s] [-S|--%s] [-i|--%s] [-t|--%s] [serialno]' % \
- tuple(LONG_OPTS)
- sys.exit(exitVal)
-
try:
- opts, args = getopt.getopt(sys.argv[1:], 'HVIFSit', LONG_OPTS)
+ opts, args = getopt.getopt(sys.argv[1:], SHORT_OPTS, LONG_OPTS)
except getopt.GetoptError, e:
print >>sys.stderr, 'ERROR:', str(e)
usage()
kwargs1 = {VERBOSE: False, 'ignoresecuredevice': False}
kwargs2 = {'forceviewserveruse': False, 'startviewserver': True}
+options = {USE_REGEXPS: False, VERBOSE_COMMENTS: False}
transform = traverseAndPrintFindViewById
for o, a in opts:
o = o.strip('-')
@@ -131,13 +171,18 @@ def usage(exitVal=1):
transform = traverseAndPrintFindViewById
elif o in ['t', FIND_VIEWS_WITH_TEXT]:
transform = traverseAndPrintFindViewWithText
+ elif o in ['r', USE_REGEXPS]:
+ options[USE_REGEXPS] = True
+ elif o in ['C', VERBOSE_COMMENTS]:
+ options[VERBOSE_COMMENTS] = True
device, serialno = ViewClient.connectToDeviceOrExit(**kwargs1)
vc = ViewClient(device, serialno, **kwargs2)
print '''#! /usr/bin/env monkeyrunner
+# -*- coding: utf-8 -*-
\'\'\'
Copyright (C) 2013 Diego Torres Milano
-Created on %s by Scripter
+Created on %s by Scripter v%s
@author: diego
\'\'\'
@@ -168,6 +213,6 @@ def usage(exitVal=1):
device, serialno = ViewClient.connectToDeviceOrExit()
vc = ViewClient(device, serialno)
-''' % date.today()
+''' % (date.today(), __version__)
vc.traverse(transform=transform)

0 comments on commit 759d89b

Please sign in to comment.