Skip to content

Commit f93204c

Browse files
committed
View tree created
- Support for different cases in temperature-converter-get-conversion.py - Added View tree traversal to obtain View coordinates
1 parent 54e27c7 commit f93204c

6 files changed

Lines changed: 346 additions & 57 deletions

File tree

AndroidViewClient/.pydevproject

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,5 @@
77
<pydev_pathproperty name="org.python.pydev.PROJECT_SOURCE_PATH">
88
<path>/AndroidViewClient/src</path>
99
<path>/AndroidViewClient/examples</path>
10-
<path>/AndroidViewClient/tests</path>
1110
</pydev_pathproperty>
1211
</pydev_project>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#! /usr/bin/env monkeyrunner
2+
'''
3+
Copyright (C) 2012 Diego Torres Milano
4+
Created on May 5, 2012
5+
6+
@author: diego
7+
'''
8+
9+
import sys
10+
import os
11+
import time
12+
13+
# this must be imported before MonkeyRunner and MonkeyDevice,
14+
# otherwise the import fails
15+
try:
16+
ANDROID_VIEW_CLIENT_HOME = os.environ['ANDROID_VIEW_CLIENT_HOME']
17+
except KeyError:
18+
print >>sys.stderr, "%s: ERROR: ANDROID_VIEW_CLIENT_HOME not set in environment" % __file__
19+
sys.exit(1)
20+
sys.path.append(ANDROID_VIEW_CLIENT_HOME + '/src')
21+
from com.dtmilano.android.viewclient import ViewClient
22+
23+
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
24+
25+
26+
#package = 'com.example.i2at.tc'
27+
#activity = '.TemperatureConverterActivity'
28+
#componentName = package + "/" + activity
29+
device = MonkeyRunner.waitForConnection(60, "emulator-5554")
30+
if not device:
31+
raise Exception('Cannot connect to device')
32+
33+
#device.startActivity(component=componentName)
34+
MonkeyRunner.sleep(5)
35+
36+
vc = ViewClient(device)
37+
vc.dump()
38+
39+
for bt in [ '1', '2', '3', '4', '5', '6' ]:
40+
b = vc.findViewWithAttribute('text:mText', bt)
41+
if b:
42+
(x, y) = b.getXY()
43+
print >>sys.stderr, "clicking b%s @ (%d,%d) ..." % (bt, x, y)
44+
b.touch()
45+
else:
46+
print >>sys.stderr, "b%s not found" % bt
47+
time.sleep(7)
48+
49+
print >>sys.stderr, "bye"

AndroidViewClient/examples/temperature-converter-get-conversion.py

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
Copyright (C) 2012 Diego Torres Milano
44
Created on Feb 3, 2012
55
6+
This example starts the TemperatureConverter activity then type '123' into the 'Celsius' field.
7+
Then a ViewClient is created to obtain the view dump and the current values of the views with
8+
id/celsius and id/fahrenheith are obtained and the conversion printed to stdout.
9+
610
@author: diego
711
'''
812

@@ -39,11 +43,41 @@
3943
vc = ViewClient(device)
4044
vc.dump()
4145

46+
# obtain the views by id
4247
celsius = vc.findViewById("id/celsius")
4348
fahrenheit = vc.findViewById("id/fahrenheit")
49+
4450

45-
c = float(celsius.mText())
46-
f = float(fahrenheit.mText())
51+
# in android-15 this is text:mText while in previous versions it was just mText
52+
try:
53+
c = float(celsius.text_mText())
54+
f = float(fahrenheit.text_mText())
55+
56+
print "%.2f C => %.2f F" % (c, f)
57+
except:
58+
try:
59+
c = float(celsius.mText())
60+
f = float(fahrenheit.mText())
61+
62+
print "%.2f C => %.2f F" % (c, f)
63+
except:
64+
print "Unexpected error:", sys.exc_info()[0]
65+
66+
# obtain the views by tag
67+
celsius = vc.findViewByTag("celsius")
68+
fahrenheit = vc.findViewByTag("fahrenheit")
69+
70+
# in android-15 this is text:mText while in previous versions it was just mText
71+
try:
72+
c = float(celsius.text_mText())
73+
f = float(fahrenheit.text_mText())
4774

48-
print "%.2f C => %.2f F" % (c, f)
75+
print "%.2f C => %.2f F" % (c, f)
76+
except:
77+
try:
78+
c = float(celsius.mText())
79+
f = float(fahrenheit.mText())
4980

81+
print "%.2f C => %.2f F" % (c, f)
82+
except:
83+
print "Unexpected error:", sys.exc_info()[0]

0 commit comments

Comments
 (0)