MonekyRunner_Android UI Automation with Python

Read first

In Continuation to my previous post "Ui Automation on Android Basics"

Setup Environment for Android UI Automation with Python

- Download Eclipse from http://www.eclipse.org/downloads/

- Install Jython from: http://www.jython.org/downloads.html

- Install Python from: http://www.python.org/download/releases/2.7.2/

- Install pydev from: http://pydev.org/download.html, and install eclipse plugin for pydev from: http://pydev.org/updates

- Open Eclipse, Click on Window Menu => Preferences

- You should have Pydev on left side of panel, if you have installed pydev

- Expand, pydev from left panel. Click on "Interpreter - Jython"

- Click on New, give path to jython.jar, which you have installed. Click OK. It will populate the below list for libraries list.

- In Below box, click on new jar => give path to monkeyrunner.jar from AndroidSdkPath/tools/lib/monkeyrunner.jar

- Click on "Interpreter - Python" from left panel. Similarly, give path to python.exe

- Click OK, and close Preferences panel.

Create a New Pydev Project


- Create new pydev project from Eclipse.

- Use defaults, click on OK to create project

- Add a python source file in src folder of project

Sample code

?
1
2
3
4
5
6
7
8
9
10
11
12
from com.android.monkeyrunner import MonkeyDevice, MonkeyRunner, MonkeyImage, MonkeyManager
 
device = MonkeyDevice
for i in range ( 5 ):
     device = MonkeyRunner.waitForConnection( 8 )
     if device ! = None :
         print "Device found..."
         break ;
 
device.press( "KEYCODE_NOTIFICATION" , "DOWN_AND_UP" )
time.sleep( 1 )
device.press( "KEYCODE_BACK" , "DOWN_AND_UP" )

Running the code

- Open shell prompt (cmd.exe, or terminal)

- Type absolute path of monkeyrunner followed by path of python script

example: "c:\\Program Files\\Android\\android-sdk\\tools\\monkeyrunner.bat" "C:\\sample.py"

Explanation

The sample code does following:

- Wait for a Android device or emulator

- Slide down Notification bar

- sleep for 1 sec

- Press hardware button back.

Note, if you know a little python. You can do much more error checks. See references section for API usage, and more details

Run Python code from Java

This is just the extension of python code. It can give you the flexibility to extend code base to java.

You need to develop a class CommandExecutor, which can actually run a command line, and give result. I advice not to use Java's RunTime.exec(). Use Apache Exec library: http://commons.apache.org/exec/

Now, you need to wrap each UI function in a java function. Lets see above example in java form:

- Write python commands in a file in exact way as we use to write python. Take care of tabs. Lets assume filename written is "C:\\sample2.py"

- Now, execute the command like we executed above, from Java, using apache exec functions

- Wait to see the results

Note: Its very important to note here, that you actually don't need to have Python, or jython installed on Desktop. It is just required to write and compile scripts, from eclipse or any other IDE. All you need is just Android SDK



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值