How to do UI automation in android?

blackfyre picture blackfyre · Sep 18, 2012 · Viewed 13.8k times · Source

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

Answer

Laser picture Laser · Sep 18, 2012

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