6

Is there any guide to do following UI automation like selecting an item, writing text, pressing buttons in Android. Please list the steps to integrated this UI automation of one of the above thing.

Thanks

blackfyre
  • 2,549
  • 5
  • 39
  • 58

4 Answers4

8

You should use it like python script. Example:

import sys
import os
import time
import shlex
import subprocess

from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
device = MonkeyRunner.waitForConnection(99, ANDROID_SERIAL_NUMBER)

def Click(device, left, top, duration = 0):
    if duration == 0:
        device.touch(left, top, MonkeyDevice.DOWN_AND_UP)
    else:
        device.touch(left, top, MonkeyDevice.DOWN)
        time.sleep(duration)
        device.touch(left, top, MonkeyDevice.UP)

def Drag_example():
    device.drag((100, 200), (1500, 150), 1, 10) 

def Settings_menu():
    package='com.android.settings'
    activity='.Settings'
    component_name=package + "/" + activity
    device.startActivity(component=component_name)
Settings_menu();

For run script use: monkeyrunner script_name

Here is: Click it's function for clicking on screen in x and y position;
Drag_example simple example of dragging
Settings_menu simple example of running activity

Don't forget to change ANDROID_SERIAL_NUMBER to your serial number.
You can get it nu,ber by adb devices command.

For more information you can read google documentation.

For using in Java read this post

Community
  • 1
  • 1
Laser
  • 6,652
  • 8
  • 54
  • 85
  • For a python script, can you please guide me, where to write the code i.e. the IDE e.g. right now I am using eclipse, now what IDE do I need for this script? After writing a script, how will I run it? :/ – blackfyre Sep 18 '12 at 11:37
  • You can write it even in notepad. Run it with command as i provide "monkeyrunner script_name". Monkeyrunner you can find in your "sdk_directory/tools/monkeyrunner" Monkeyrunner it's executable file. Probably you don't need in first 5 lines with import. – Laser Sep 18 '12 at 11:49
  • Thanks a lot! :) And what should be the extension of the notepad file? To run the script, should I go this directory "Android\android-sdk\tools" ? – blackfyre Sep 18 '12 at 11:55
  • Extansion is no matter, let it be .py as for python. You should run your script something like this way: "C:\sdk\tools\monkeyrunner.exe script.py". – Laser Sep 18 '12 at 12:01
  • Problem in running script file: android-sdk\tools>monkeyrunner test.py.txt Can't open specified script file Usage: monkeyrunner [options] SCRIPT_FILE -s MonkeyServer IP Address. -p MonkeyServer TCP Port. -v MonkeyServer Logging level (ALL, FINEST, FINER, FINE, CONFIG, INFO, WARNING, SEVERE, OFF) – blackfyre Sep 18 '12 at 12:20
  • I try to run it with emulator on windows. It' works perfect. Dont' forget to change ANDROID_SERIAL_NUMBER on your number. device = MonkeyRunner.waitForConnection(99, "emulator-5554") And run line should look like this: android-sdk-windows\tools>monkeyrunner.bat test.py.txt – Laser Sep 18 '12 at 13:29
5

Some useful link,

  1. Android: Sample Code To Demonstrate Monkeyrunner

  2. Android Guide: monkeyrunner Overview

  3. Android Guide: MonkeyDevice

  4. Android Guide: MonkeyRunner

  5. Monkeyrunner: interacting with the Views Last link but best...

JPM
  • 9,077
  • 13
  • 78
  • 137
RPB
  • 16,006
  • 16
  • 55
  • 79
1

Automate is an app that can do all jobs you mentioned and many more. You wouldn't need a laptop or ADB connection. Also, no coding is required since programming is in the form of blocks and mostly synchronous.

Play Store link.

Prateek Gupta
  • 2,422
  • 2
  • 16
  • 30
0

Here is the sample project which I developed to automate an Ebay App where all the above asked UI actions are implemented.

https://github.com/Sivateja0804/EbayAutomationUsingEclipse

go through the link for code repository