forked from dtmilano/AndroidViewClient
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripter.py
More file actions
executable file
·173 lines (142 loc) · 4.51 KB
/
scripter.py
File metadata and controls
executable file
·173 lines (142 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#! /usr/bin/env monkeyrunner
'''
Copyright (C) 2013 Diego Torres Milano
Created on Mar 28, 2013
Scripter helps you create AndroidViewClient scripts generating a working template that can be
modified to suit more specific needs.
@author: diego
'''
__version__ = '0.9.0'
import re
import sys
import os
import getopt
import warnings
from datetime import date
# 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
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'
# -u,-s,-p,-v eaten by monkeyrunner
LONG_OPTS = [HELP, VERBOSE, IGNORE_SECURE_DEVICE, FORCE_VIEW_SERVER_USE, DO_NOT_START_VIEW_SERVER,
FIND_VIEWS_BY_ID, FIND_VIEWS_WITH_TEXT]
ID_RE = re.compile('id/([^/]*)(/(\d+))?')
def variableNameFromId(id):
'''
Returns a suitable variable name from the id.
@type id: str
@param id: the I{id}
@return: the variable name from the id
'''
m = ID_RE.match(id)
if m:
var = m.group(1)
if m.group(3):
var += m.group(3)
if re.match('^\d', var):
var = 'id_' + var
return var
else:
raise Exception('Not an id: %s' % id)
def traverseAndPrintFindViewById(view):
'''
Traverses the View tree and prints the corresponding statement.
@type view: L{View}
@param view: the View
'''
id = view.getUniqueId()
var = variableNameFromId(id)
try:
print '# tag=%s' % view.getTag()
except:
pass
print '%s = vc.findViewByIdOrRaise("%s")' % (var, id)
def traverseAndPrintFindViewWithText(view):
'''
Traverses the View tree and prints the corresponding statement.
@type view: L{View}
@param view: the View
'''
id = view.getUniqueId()
text = view.getText()
if text:
var = variableNameFromId(id)
print '%s = vc.findViewWithTextOrRaise("%s")' % (var, text)
else:
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)
except getopt.GetoptError, e:
print >>sys.stderr, 'ERROR:', str(e)
usage()
kwargs1 = {VERBOSE: False, 'ignoresecuredevice': False}
kwargs2 = {'forceviewserveruse': False, 'startviewserver': True}
transform = traverseAndPrintFindViewById
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
elif o in ['i', FIND_VIEWS_BY_ID]:
transform = traverseAndPrintFindViewById
elif o in ['t', FIND_VIEWS_WITH_TEXT]:
transform = traverseAndPrintFindViewWithText
device, serialno = ViewClient.connectToDeviceOrExit(**kwargs1)
vc = ViewClient(device, serialno, **kwargs2)
print '''#! /usr/bin/env monkeyrunner
\'\'\'
Copyright (C) 2013 Diego Torres Milano
Created on %s by Scripter
@author: diego
\'\'\'
import re
import sys
import os
# 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
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
device, serialno = ViewClient.connectToDeviceOrExit()
vc = ViewClient(device, serialno)
''' % date.today()
vc.traverse(transform=transform)