Permalink
Browse files

View tree created

- Support for different cases in temperature-converter-get-conversion.py
- Added View tree traversal to obtain View coordinates
  • Loading branch information...
dtmilano committed May 11, 2012
1 parent 54e27c7 commit f93204c439617e9f1798246e4635baf16d2e5da0
@@ -7,6 +7,5 @@
<pydev_pathproperty name="org.python.pydev.PROJECT_SOURCE_PATH">
<path>/AndroidViewClient/src</path>
<path>/AndroidViewClient/examples</path>
-<path>/AndroidViewClient/tests</path>
</pydev_pathproperty>
</pydev_project>
@@ -0,0 +1,49 @@
+#! /usr/bin/env monkeyrunner
+'''
+Copyright (C) 2012 Diego Torres Milano
+Created on May 5, 2012
+
+@author: diego
+'''
+
+import sys
+import os
+import time
+
+# this must be imported before MonkeyRunner and MonkeyDevice,
+# otherwise the import fails
+try:
+ ANDROID_VIEW_CLIENT_HOME = os.environ['ANDROID_VIEW_CLIENT_HOME']
+except KeyError:
+ print >>sys.stderr, "%s: ERROR: ANDROID_VIEW_CLIENT_HOME not set in environment" % __file__
+ sys.exit(1)
+sys.path.append(ANDROID_VIEW_CLIENT_HOME + '/src')
+from com.dtmilano.android.viewclient import ViewClient
+
+from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
+
+
+#package = 'com.example.i2at.tc'
+#activity = '.TemperatureConverterActivity'
+#componentName = package + "/" + activity
+device = MonkeyRunner.waitForConnection(60, "emulator-5554")
+if not device:
+ raise Exception('Cannot connect to device')
+
+#device.startActivity(component=componentName)
+MonkeyRunner.sleep(5)
+
+vc = ViewClient(device)
+vc.dump()
+
+for bt in [ '1', '2', '3', '4', '5', '6' ]:
+ b = vc.findViewWithAttribute('text:mText', bt)
+ if b:
+ (x, y) = b.getXY()
+ print >>sys.stderr, "clicking b%s @ (%d,%d) ..." % (bt, x, y)
+ b.touch()
+ else:
+ print >>sys.stderr, "b%s not found" % bt
+ time.sleep(7)
+
+print >>sys.stderr, "bye"
@@ -3,6 +3,10 @@
Copyright (C) 2012 Diego Torres Milano
Created on Feb 3, 2012
+This example starts the TemperatureConverter activity then type '123' into the 'Celsius' field.
+Then a ViewClient is created to obtain the view dump and the current values of the views with
+id/celsius and id/fahrenheith are obtained and the conversion printed to stdout.
+
@author: diego
'''
@@ -39,11 +43,41 @@
vc = ViewClient(device)
vc.dump()
+# obtain the views by id
celsius = vc.findViewById("id/celsius")
fahrenheit = vc.findViewById("id/fahrenheit")
+
-c = float(celsius.mText())
-f = float(fahrenheit.mText())
+# in android-15 this is text:mText while in previous versions it was just mText
+try:
+ c = float(celsius.text_mText())
+ f = float(fahrenheit.text_mText())
+
+ print "%.2f C => %.2f F" % (c, f)
+except:
+ try:
+ c = float(celsius.mText())
+ f = float(fahrenheit.mText())
+
+ print "%.2f C => %.2f F" % (c, f)
+ except:
+ print "Unexpected error:", sys.exc_info()[0]
+
+# obtain the views by tag
+celsius = vc.findViewByTag("celsius")
+fahrenheit = vc.findViewByTag("fahrenheit")
+
+# in android-15 this is text:mText while in previous versions it was just mText
+try:
+ c = float(celsius.text_mText())
+ f = float(fahrenheit.text_mText())
-print "%.2f C => %.2f F" % (c, f)
+ print "%.2f C => %.2f F" % (c, f)
+except:
+ try:
+ c = float(celsius.mText())
+ f = float(fahrenheit.mText())
+ print "%.2f C => %.2f F" % (c, f)
+ except:
+ print "Unexpected error:", sys.exc_info()[0]
Oops, something went wrong.

0 comments on commit f93204c

Please sign in to comment.