MonekyRunner_Android UI Automation with Python

來源:互聯網
上載者:User
文章目錄
  • 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 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?
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\\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

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.