Permalink
Browse files

Added list windows command

- Version 2.3.13
- Added ViewClient.list()
- dump.py: added window command line option
- list.py: list windows
  • Loading branch information...
1 parent e2462b5 commit 840a7a81d1cf4a4021fe68c2e8c79b6fa7914674 @dtmilano committed Apr 23, 2013
@@ -33,6 +33,7 @@
IGNORE_SECURE_DEVICE = 'ignore-secure-device'
FORCE_VIEW_SERVER_USE = 'force-view-server-use'
DO_NOT_START_VIEW_SERVER = 'do-not-start-view-server'
+WINDOW = 'window'
UNIQUE_ID = 'uniqueId'
POSITION = 'position'
CONTENT_DESCRIPTION = 'content-description'
@@ -43,22 +44,25 @@
'd':ViewClient.TRAVERSE_CITCD, CONTENT_DESCRIPTION:ViewClient.TRAVERSE_CITCD,
'c':ViewClient.TRAVERSE_CITC, CENTER:ViewClient.TRAVERSE_CITC,
}
-LONG_OPTS = [HELP, VERBOSE, IGNORE_SECURE_DEVICE, FORCE_VIEW_SERVER_USE, DO_NOT_START_VIEW_SERVER,
- UNIQUE_ID, POSITION, CONTENT_DESCRIPTION, CENTER]
+SHORT_OPTS = 'HVIFSw:ixdc'
+LONG_OPTS = [HELP, VERBOSE, IGNORE_SECURE_DEVICE, FORCE_VIEW_SERVER_USE,
+ DO_NOT_START_VIEW_SERVER, WINDOW + '=',
+ UNIQUE_ID, POSITION, CONTENT_DESCRIPTION, CENTER]
def usage(exitVal=1):
- print >> sys.stderr, 'usage: dump.py [-H|--%s] [-V|--%s] [-I|--%s] [-F|--%s] [-S|--%s] [-i|--%s] [-x|--%s] [-d|--%s] [-c|--%s] [serialno]' % \
+ print >> sys.stderr, 'usage: dump.py [-H|--%s] [-V|--%s] [-I|--%s] [-F|--%s] [-S|--%s] [-w|--%s=WINDOW] [-i|--%s] [-x|--%s] [-d|--%s] [-c|--%s] [serialno]' % \
tuple(LONG_OPTS)
sys.exit(exitVal)
try:
- opts, args = getopt.getopt(sys.argv[1:], 'HVIFSixdc', 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}
+kwargs2 = {'forceviewserveruse': False, 'startviewserver': True, 'autodump': False}
+options = {WINDOW: -1}
transform = ViewClient.TRAVERSE_CIT
for o, a in opts:
o = o.strip('-')
@@ -72,7 +76,11 @@ def usage(exitVal=1):
kwargs2['forceviewserveruse'] = True
elif o in ['S', DO_NOT_START_VIEW_SERVER]:
kwargs2['startviewserver'] = False
+ elif o in ['w', WINDOW]:
+ options[WINDOW] = a
else:
transform = MAP[o]
-ViewClient(*ViewClient.connectToDeviceOrExit(**kwargs1), **kwargs2).traverse(transform=transform)
+vc = ViewClient(*ViewClient.connectToDeviceOrExit(**kwargs1), **kwargs2)
+vc.dump(window=options[WINDOW])
+vc.traverse(transform=transform)
@@ -0,0 +1,66 @@
+#! /usr/bin/env monkeyrunner
+'''
+Copyright (C) 2012 Diego Torres Milano
+Created on Apr 23, 2013
+
+@author: diego
+'''
+
+
+import sys
+import os
+import getopt
+
+# This must be imported before MonkeyRunner and MonkeyDevice,
+# otherwise the import fails.
+# PyDev sets PYTHONPATH, use it
+try:
+ for p in os.environ['PYTHONPATH'].split(':'):
+ if not p in sys.path:
+ sys.path.append(p)
+except:
+ pass
+
+try:
+ sys.path.append(os.path.join(os.environ['ANDROID_VIEW_CLIENT_HOME'], 'src'))
+except:
+ pass
+
+from com.dtmilano.android.viewclient import ViewClient
+
+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'
+# -u,-s,-p,-v eaten by monkeyrunner
+SHORT_OPTS = 'HVIFS'
+LONG_OPTS = [HELP, VERBOSE, IGNORE_SECURE_DEVICE, FORCE_VIEW_SERVER_USE, DO_NOT_START_VIEW_SERVER]
+
+def usage(exitVal=1):
+ print >> sys.stderr, 'usage: list.py [-H|--%s] [-V|--%s] [-I|--%s] [-F|--%s] [-S|--%s] [serialno]' % \
+ tuple(LONG_OPTS)
+ sys.exit(exitVal)
+
+try:
+ 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, 'autodump': False}
+for o, a in opts:
+ o = o.strip('-')
+ if o in ['H', HELP]:
+ usage(0)
+ elif o in ['V', VERBOSE]:
+ kwargs1[VERBOSE] = True
+ elif o in ['I', IGNORE_SECURE_DEVICE]:
+ kwargs1['ignoresecuredevice'] = True
+ elif o in ['F', FORCE_VIEW_SERVER_USE]:
+ kwargs2['forceviewserveruse'] = True
+ elif o in ['S', DO_NOT_START_VIEW_SERVER]:
+ kwargs2['startviewserver'] = False
+
+print ViewClient(*ViewClient.connectToDeviceOrExit(**kwargs1), **kwargs2).list()
@@ -17,7 +17,7 @@
@author: diego
'''
-__version__ = '2.3.12'
+__version__ = '2.3.13'
import sys
import subprocess
@@ -1511,15 +1511,18 @@ def traverse(self, root="ROOT", indent="", transform=View.__str__, stream=sys.st
for ch in root.children:
self.traverse(ch, indent=indent+" ", transform=transform, stream=stream)
- def dump(self, windowId=-1, sleep=1):
+ def dump(self, window=-1, sleep=1):
'''
Dumps the window content.
Sleep is useful to wait some time before obtaining the new content when something in the
window has changed.
- @type windowId: int
- @param windowId: the window id of the window to dump or -1 to dump all windows
+ @type window: int or str
+ @param window: the window id or name of the window to dump.
+ The name is the package name or the window name (i.e. StatusBar) for
+ system windows.
+ Use -1 to dump all windows.
@type sleep: int
@param sleep: sleep in seconds before proceeding to dump the content
@@ -1556,19 +1559,32 @@ def dump(self, windowId=-1, sleep=1):
print >>sys.stderr
self.setViewsFromUiAutomatorDump(received)
else:
+ if type(window).__name__ == 'str':
+ self.list(sleep=0)
+ found = False
+ for wId in self.windows:
+ if window == self.windows[wId]:
+ window = wId
+ found = True
+ break
+ if not found:
+ raise RuntimeError("ERROR: Cannot find window '%s'" % window)
+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect((VIEW_SERVER_HOST, self.localPort))
except socket.error, ex:
raise RuntimeError("ERROR: Connecting to %s:%d: %s" % (VIEW_SERVER_HOST, self.localPort, ex))
- s.send('dump %d\r\n' % windowId)
+ cmd = 'dump %x\r\n' % window
+ if DEBUG:
+ print >>sys.stderr, "executing: '%s'" % cmd
+ s.send(cmd)
received = ""
doneRE = re.compile("DONE")
while True:
received += s.recv(1024)
if doneRE.search(received[-7:]):
break
-
s.close()
if DEBUG:
self.received = received
@@ -1584,6 +1600,58 @@ def dump(self, windowId=-1, sleep=1):
return self.views
+ def list(self, sleep=1):
+ '''
+ List the windows.
+
+ Sleep is useful to wait some time before obtaining the new content when something in the
+ window has changed.
+ This also sets L{self.windows} as the list of windows.
+
+ @type sleep: int
+ @param sleep: sleep in seconds before proceeding to dump the content
+
+ @return: the list of windows
+ '''
+
+ if sleep > 0:
+ MonkeyRunner.sleep(sleep)
+
+ if self.useUiAutomator:
+ raise Exception("Not implemented yet")
+ else:
+ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ try:
+ s.connect((VIEW_SERVER_HOST, self.localPort))
+ except socket.error, ex:
+ raise RuntimeError("ERROR: Connecting to %s:%d: %s" % (VIEW_SERVER_HOST, self.localPort, ex))
+ s.send('list\r\n')
+ received = ""
+ doneRE = re.compile("DONE")
+ while True:
+ received += s.recv(1024)
+ if doneRE.search(received[-7:]):
+ break
+ s.close()
+ if DEBUG:
+ self.received = received
+ if DEBUG_RECEIVED:
+ print >>sys.stderr, "received %d chars" % len(received)
+ print >>sys.stderr
+ print >>sys.stderr, received
+ print >>sys.stderr
+
+ self.windows = {}
+ for line in received.split('\n'):
+ if not line:
+ break
+ if doneRE.search(line):
+ break
+ (wid, package) = line.split()
+ self.windows[int('0x' + wid, 16)] = package
+ return self.windows
+
+
def findViewById(self, viewId, root="ROOT", viewFilter=None):
'''
Finds the View with the specified viewId.

0 comments on commit 840a7a8

Please sign in to comment.