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

 MonekyRunner_Android UI Automation with Python_monkeyrunner

- 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
fromcom.android.monkeyrunner importMonkeyDevice, MonkeyRunner, MonkeyImage, MonkeyManager
device=MonkeyDevice
fori inrange(5):
device =MonkeyRunner.waitForConnection(8)
ifdevice !=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


http://blog.csdn.net/jiguanghoverli/article/details/8012666