Friday, August 17, 2012

monkeyrunner: detecting the OS

Sometimes you monkeyrunner script should know the Operating System it is running on. In the big majority of the cases you don't have to worry if you are running on Linux or Mac OS X, but things are not so smooth on Windows.

I'll give you an example. I've received some bug reports about AndroidViewClient not being able to find adb. AndroidViewClient tries to be clever and not to invoke adb if it's going to fail because it's not found or it's not executable. To determine this, it is using:

        if not os.access(adb, os.X_OK):
            raise Exception('adb="%s" is not executable' % adb)

the trick here is that for Windows platforms adb should include the trailing .exe.
Then the problem is to determine the OS the script is running on.

There are several ways of determining the OS in python and jython. Let's see what are the results using monkeyrunner

Command Linux Mac OS X Windows
os.getenv('os') None None Windows_NT java java java
platform.system() Java Java Java
sys.platform java1.6.0_26 java1.6.0_33 java1.7.0_05
java.lang.System.getProperty('') Linux Mac OS X Windows XP

From the previous table we can determine that the best way of obtaining the OS from a monkeyrunner script is


I hope this helps you


BruceZhang said...

Hello Diego,

Thanks for your sharing. I have a question about MonkeyRunner.waitForConnection().
I can create the connection between MonkeyRunner and Emulator, but failed between MonkeyRunner and real device..
The phenomenon is that..'waitForConnection()'always is waiting..

I tried two days but cannot find the reason..

BruceZhang said...

Could you give me some instructions about this?

previously I could use MonkeyRunner on real device successfully.
I don't know why it is always waiting now...

BruceZhang said...

Add more info..
Adb devices----I can get the real device listed.
Android SDK 4.2.

then enter monkeyrunner in cmd.
from import MonkeyRunner
device=MonkeyRunner.waitForConnection()... then it always wait here..

Diego Torres Milano said...

it's a [known] MonkeyRunner bug. That's why AndroidViewClient uses:

osName = java.lang.System.getProperty('')
if osName.startswith('Windows'):
# alarm is not implemented in Windows
setAlarm = False
if setAlarm:
device = MonkeyRunner.waitForConnection(timeout, serialno)
if setAlarm:

to avoid this problem.
Killing adb server and probably disconnecting and connecting the device should help.

BruceZhang said...

Hello Diego,
Thank you very much. I will try the above codes now.
I have killed/started Adb server many times before, also disconnecting/connecting the device, but it always failed to connnect the device.

BruceZhang said...

Hello Diego,
I have added the above scripts, osName.startswith('Windows') is correct, import MonkeyRunner successfully.

but device = MonkeyRunner.waitForConnection() is still waiting there while connecting to real device....

is it possible that it has something to do with the PC environment? because I can connect real device to MonkeyRunner before I upgrade my device's version..
I cannot perform all my scripts if this cannot be fixed...

BruceZhang said...

Hello Diego,

I have fixed this issue...actually it's one of my mistakes, I have added wrong enviroment variables.

srikanth said...

your providing such a valuable information about studying..and also have some good key points to every student.

IBM MQ online training