MonkeyRunner_monkeyrunner: testing views properties

来源:互联网 发布:seo和sem之间的区别 编辑:程序博客网 时间:2024/05/14 08:43
There are several questions floating around, like this one instackoverflow, about how some view properties could be obtained from a monkeyrunner script or putting the question in more general terms:
how a test that verifies some properties can be created using monkeyrunner ?


This is a required feature if we are going to create tests in monkeyrunner otherwise our alternatives to verify some state in the views is limited to visual comparison. These cases were treated in previous articles like:


  • monkeyrunner: visual image comparison, and
  • Automated Android testing using Sikuli


but now we will be using a logical approach rather than visual comparison. To be able to do it we need a mechanism of obtaining view properties values.
Lastest versions of monkeyrunner provides an extension toMonkeyDevice called EasyMonkeyDevice and this class has methods to get some properties likeMonkeyDevice.getText().
This API is not documented so expect changes in the future.
TemperatureConverter, a sample application that has been used in other articles before, will be our application under test. The source code can be obtained fromgithub.


The idea behind this test is, using monkeyrunner to connect to  the device, enter123 in the Celsius field and expect to find 253.40 in the Fahrenheit field.

#! /usr/bin/env monkeyrunnerimport sysfrom com.android.monkeyrunner import MonkeyRunner, MonkeyDevicefrom com.android.monkeyrunner.easy import EasyMonkeyDevicefrom com.android.monkeyrunner.easy import By# connect to the devicedevice = MonkeyRunner.waitForConnection()# start TemperatureConverterdevice.startActivity(component="com.example.i2at.tc/.TemperatureConverterActivity")# use the EasyMonkey APIeasyDevice = EasyMonkeyDevice(device)celsiusId = By.id('id/celsius')if not celsiusId:   raise Exception("View with id/celsius not found")fahrenheitId = By.id('id/fahrenheit')if not fahrenheitId:   raise Exception("View with id/fahrenheit not found")MonkeyRunner.sleep(3)easyDevice.type(celsiusId, '123')MonkeyRunner.sleep(3)celsius = easyDevice.getText(celsiusId)fahrenheit = easyDevice.getText(fahrenheitId)expected = '253.40'if fahrenheit == expected:   print 'PASS'else:   print 'FAIL: expected %s, actual %s' % (expected, fahrenheit)

Unfortunately, it won't work in most of the cases. You are likely to receive this exception:


        java.lang.RuntimeException: java.lang.RuntimeException: No text property on node

I felt frustrated at first, but what the advantage of an Open Source project is other than going to the source code and find out why it's not working.
I dug into the Chimpchat, HierarchyView and ViewServer code and found out that for some reason EasyMonkeyDevice is looking for thetext:mText property when in most of the cases it should be only mText.
So here is the patch, that I will be uploading to android soon:

diff --git a/chimpchat/src/com/android/chimpchat/hierarchyviewer/HierarchyViewer.java b/chimpchat/src/com/android/chimpchat/hierarchyviewer/HierarchyViewer.javaindex 6ad98ad..6c34d71 100644--- a/chimpchat/src/com/android/chimpchat/hierarchyviewer/HierarchyViewer.java+++ b/chimpchat/src/com/android/chimpchat/hierarchyviewer/HierarchyViewer.java@@ -170,7 +170,11 @@ public class HierarchyViewer {         }         ViewNode.Property textProperty = node.namedProperties.get("text:mText");         if (textProperty == null) {-            throw new RuntimeException("No text property on node");+            // dtmilano: give it another chance, ICS ViewServer returns mText+            textProperty = node.namedProperties.get("mText");+            if ( textProperty == null ) {+                throw new RuntimeException("No text property on node");+            }         }         return textProperty.value;     }
Once this patch is applied and you rebuild monkeyrunner or the entire SDK if you prefer, you will be presented with the expected result: PASS


UPDATE

This patch has been submitted toAndroid Open Source Project as https://android-review.googlesource.com/31850


UPDATE: July 2012

Android SDK Tools Rev 20 includes the aforementioned patch and now the previous monkeyrunner example works!


转自:There are several questions floating around, like this one instackoverflow, about how some view properties could be obtained from a monkeyrunner script or putting the question in more general terms:
how a test that verifies some properties can be created using monkeyrunner ?


This is a required feature if we are going to create tests in monkeyrunner otherwise our alternatives to verify some state in the views is limited to visual comparison. These cases were treated in previous articles like:


  • monkeyrunner: visual image comparison, and
  • Automated Android testing using Sikuli


but now we will be using a logical approach rather than visual comparison. To be able to do it we need a mechanism of obtaining view properties values.
Lastest versions of monkeyrunner provides an extension toMonkeyDevice called EasyMonkeyDevice and this class has methods to get some properties likeMonkeyDevice.getText().
This API is not documented so expect changes in the future.
TemperatureConverter, a sample application that has been used in other articles before, will be our application under test. The source code can be obtained fromgithub.


The idea behind this test is, using monkeyrunner to connect to  the device, enter123 in the Celsius field and expect to find 253.40 in the Fahrenheit field.

#! /usr/bin/env monkeyrunnerimport sysfrom com.android.monkeyrunner import MonkeyRunner, MonkeyDevicefrom com.android.monkeyrunner.easy import EasyMonkeyDevicefrom com.android.monkeyrunner.easy import By# connect to the devicedevice = MonkeyRunner.waitForConnection()# start TemperatureConverterdevice.startActivity(component="com.example.i2at.tc/.TemperatureConverterActivity")# use the EasyMonkey APIeasyDevice = EasyMonkeyDevice(device)celsiusId = By.id('id/celsius')if not celsiusId:   raise Exception("View with id/celsius not found")fahrenheitId = By.id('id/fahrenheit')if not fahrenheitId:   raise Exception("View with id/fahrenheit not found")MonkeyRunner.sleep(3)easyDevice.type(celsiusId, '123')MonkeyRunner.sleep(3)celsius = easyDevice.getText(celsiusId)fahrenheit = easyDevice.getText(fahrenheitId)expected = '253.40'if fahrenheit == expected:   print 'PASS'else:   print 'FAIL: expected %s, actual %s' % (expected, fahrenheit)

Unfortunately, it won't work in most of the cases. You are likely to receive this exception:


        java.lang.RuntimeException: java.lang.RuntimeException: No text property on node

I felt frustrated at first, but what the advantage of an Open Source project is other than going to the source code and find out why it's not working.
I dug into the Chimpchat, HierarchyView and ViewServer code and found out that for some reason EasyMonkeyDevice is looking for thetext:mText property when in most of the cases it should be only mText.
So here is the patch, that I will be uploading to android soon:

diff --git a/chimpchat/src/com/android/chimpchat/hierarchyviewer/HierarchyViewer.java b/chimpchat/src/com/android/chimpchat/hierarchyviewer/HierarchyViewer.javaindex 6ad98ad..6c34d71 100644--- a/chimpchat/src/com/android/chimpchat/hierarchyviewer/HierarchyViewer.java+++ b/chimpchat/src/com/android/chimpchat/hierarchyviewer/HierarchyViewer.java@@ -170,7 +170,11 @@ public class HierarchyViewer {         }         ViewNode.Property textProperty = node.namedProperties.get("text:mText");         if (textProperty == null) {-            throw new RuntimeException("No text property on node");+            // dtmilano: give it another chance, ICS ViewServer returns mText+            textProperty = node.namedProperties.get("mText");+            if ( textProperty == null ) {+                throw new RuntimeException("No text property on node");+            }         }         return textProperty.value;     }
Once this patch is applied and you rebuild monkeyrunner or the entire SDK if you prefer, you will be presented with the expected result: PASS


UPDATE

This patch has been submitted toAndroid Open Source Project as https://android-review.googlesource.com/31850


UPDATE: July 2012
Android SDK Tools Rev 20 includes the aforementioned patch and now the previous monkeyrunner example works!


原创粉丝点击