Monekyrunner_android UI automation with Python

Source: Internet
Author: User
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=
MonkeyDevicefor
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.