Document directory
- Setup environment for Android UI automation with Python
- Explanation
- Run Python code from Java
- 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
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 shoshould 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?
123456789101112 |
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-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.exe C (). 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 abve, 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
References
- Android UI automation | kgsepg
Android UI automation slide
- Monkeyimage | Android Developers
Image related APIs
- Monkeydevice | Android Developers
All device APIs reference
- Monkeyrunner | Android Developers
Base URL