Monday, June 04, 2012

monkeyrunner: Q&A

Q: 
Hi,
Thanks for the response.
To get more specific for what I was looking at is, to use getCallState()getDataActivity() etc.


[Reference: http://developer.android.com/reference/android/telephony/TelephonyManager.html] is Monkeyrunner scripts.


Please let me know if this is a possible idea,




(This question was posted as a comment to monkeyrunner:  visual image comparison)

A: 
Taking the right approach this is pretty simple and straightforward. If you have been followed the posts in this blog you may have noticed some time ago we introduced AndroidViewClient in monkeyrunner: interacting with the Views. Well, we can use exactly the same technique to invoke Android services and get the results, but in this case we will be invoking the phone service instead.

#! /usr/bin/env monkeyrunner
'''
Created on Jun 2, 2012

Take a look at:
    <android>/frameworks/base/telephony/java/com/android/internal/telephony/ITelephony.aidl
    <android>/out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/src/telephony/ja
va/com/android/internal/telephony/ITelephony.java
    <android>/java/android/telephony/TelephonyManager.java

@author: diego
'''

import sys
import re

from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice

DEBUG = True
android_os_IBinder_FIRST_CALL_TRANSACTION = 1
TRANSACTION_getCallState = android_os_IBinder_FIRST_CALL_TRANSACTION + 28
TRANSACTION_getDataActivity = android_os_IBinder_FIRST_CALL_TRANSACTION + 29;

def telephonyManager(device, transaction):
    return serviceResponse(device.shell('service call phone %d' % transaction))

def serviceResponse(response):
    m = re.match("Result: Parcel\((\d+) (\d+)   '........'\)\r\n", response)
    if m:
        return int(m.group(2))
    return -1

def main():
    device = MonkeyRunner.waitForConnection(60)

    print "call state: %d" % telephonyManager(device, TRANSACTION_getCallState)
    print "data activity: %d" % telephonyManager(device, TRANSACTION_getDataActivity)

if __name__ == '__main__':
    main()

Obviously, this can be extended to support other transactions or to interpret the results results in other ways.
Also, this same technique can be used to invoke other services.
Hope this helps.



13 comments:

Smitha said...

Thanks again, will try accordingly.

Smitha said...

The script was very useful.
On another note, one of my requirements needs enable and disable tethering.Problem is, once I enable tethering,the device loses the adb connection and is detected via rndis. So I am not able to get back to the script to disable tethering.

Can you please suggest a way out?

Thanks again.

Nilo said...

Olá Diego,
Parabéns pelo projeto PXES, muito bom.

Usava o PXES pra acesso via RDP a um servidor windows 2000, mas não funciona no Windows 2008 por causa do rdesktop, como faço pra atualizar o rdesktop?
Ou você tem um ISO com o Rdesktop atualizado?

Agradeço desde já a ajuda.

Jerry Chen said...

Hi, I was confused with monkeyrunner and instrumentationtestrunner.
they are two different ways to do autotest. but what's the difference between them? What scenario fit for which one?
Thanks

Diego Torres Milano said...

@Smitha,
Perhaps once you have your device tethered you can connect adb over IP (adb connect ).

Diego Torres Milano said...

@Jerry Chen,
1) monkeyrunner is a tool that provides an API for writing programs that control an Android device or emulator from outside of Android code.
2) InstrumentationTestRunner is an Instrumentation that runs various types of TestCases against an Android package (application).

Diego Torres Milano said...

@Nilo,
PXES project has been acquired by 2X Software a long time ago and thus it has not been updated. The rdesktop used by PXES is very outdated and it doesn't support latest Windows versions.
If you have a statically-linked rdesktop version that works in your environment you should be able to replace the version in the ISO image.

Nilo said...

Ola Diego Torres Milano.
Obrigado pela resposta,
Como atualizado o rdesktop? Como altero o ISO pra atualizar o rdesktop?
Seria perfeito o PXES com rdesktop atualizado.


Agradeço desde já a ajuda.

Nilo.

Smitha said...

Hi Milano,

Back to you for inputs. I tried adb connect.
Now that the device is connected over IP, i tried running the Monkeyrunner script. I get the following error:
120720 12:24:20.247:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] Scri
pt terminated due to an exception
120720 12:24:20.247:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]Trace
back (most recent call last):
File "C:\Users\smgs\Desktop\MonkeyRunner_Bangalore\Khushboo\MO_Call_final.py",
line 22, in
device.touch(42,814,"UP")
at com.android.monkeyrunner.MonkeyDevice.touch(MonkeyDevice.java:136)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)

java.lang.NullPointerException: java.lang.NullPointerException

120720 12:24:20.247:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.core.Py.JavaError(Py.java:455)
120720 12:24:20.247:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.core.Py.JavaError(Py.java:448)
120720 12:24:20.247:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:177)
120720 12:24:20.247:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.core.PyObject.__call__(PyObject.java:355)
120720 12:24:20.247:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.core.PyMethod.__call__(PyMethod.java:215)
120720 12:24:20.247:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.core.PyMethod.instancemethod___call__(PyMethod.java:221)
120720 12:24:20.247:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.core.PyMethod.__call__(PyMethod.java:206)
120720 12:24:20.247:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.core.PyObject.__call__(PyObject.java:432)
120720 12:24:20.247:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.core.PyObject.__call__(PyObject.java:436)
120720 12:24:20.247:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.pycode._pyx0.f$0(C:\Users\smgs\Desktop\MonkeyRunner_Bangalore\Khus
hboo\MO_Call_final.py:14)
120720 12:24:20.247:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.pycode._pyx0.call_function(C:\Users\smgs\Desktop\MonkeyRunner_Bang
alore\Khushboo\MO_Call_final.py)
120720 12:24:20.247:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.core.PyTableCode.call(PyTableCode.java:165)
120720 12:24:20.247:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.core.PyCode.call(PyCode.java:18)
120720 12:24:20.247:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.core.Py.runCode(Py.java:1197)
120720 12:24:20.247:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.core.__builtin__.execfile_flags(__builtin__.java:538)
120720 12:24:20.247:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.util.PythonInterpreter.execfile(PythonInterpreter.java:156)
120720 12:24:20.247:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at com.android.monkeyrunner.ScriptRunner.run(ScriptRunner.java:116)
120720 12:24:20.247:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at com.android.monkeyrunner.MonkeyRunnerStarter.run(MonkeyRunnerStarter.java:77)

120720 12:24:20.247:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at com.android.monkeyrunner.MonkeyRunnerStarter.main(MonkeyRunnerStarter.java:18
9)

Stuck up again,

Diego Torres Milano said...

@Smitha,
Have you used MonkeyDevice.UP or just "UP"?

Nilo said...

Ola Diego Torres Milano.
Obrigado pela resposta,
Como atualizado o rdesktop? Como altero o ISO pra atualizar o rdesktop?
Seria perfeito o PXES com rdesktop atualizado.


Agradeço desde já a ajuda.

Nilo.

Wilson said...

Hi there Diego, first let me say that I really appreciate your blog, it's been very helpful as a reference for testing and Android. I have a question about monkey runner, is there a way to tap on one of the locale settings from the locale picker? Here's what I have to get there:

device = MonkeyRunner.waitForConnection()
easyDevice = EasyMonkeyDevice(device)
easyDevice.startActivity(component="com.android.settings/.LocalePicker")

I don't want to have to tap on the coordinates, is there a way to use the mtext to figure out what button to tap? All of the ids are 'id/text1' as far as I can tell. Any feedback would be appreciated. Thanks for your time and thanks again for such a great resource!

--W Bull

jitendraa said...

Knowlarity provides a suite of hosted voice applications for all types of call tracking, call notifications, call forwarding, call automation and interactive voice response (IVR) and
ip telephony
.These technologies help businesses manage, measure and automate voice communications. Knowlarity make Business Communication on Phone virtually accessible for companies to manage. Business Intelligence and reporting on phone calls now on your FingerTips.