How to automate Android settings

Inside a project of rollout of new mobile phone we had the need to automate some settings configuration to make android devices rollout quickly.

We used ADB and Monkeyrunner tools for accomplish this task. These tools are inside Android SDK: http://developer.android.com/sdk/index.html

Install Android SDK and from SDK Manager select both “Android SDK Tools” and “Android SDK Platform-tools”

Android SDK Manager

ADB can found on ..\Android\android-sdk\platform-tools

Before using ADB remember to enable Developer Menu and the USB Debug option: https://241931348f64b1d1.wordpress.com/2015/10/30/how-to-enabledisable-developer-menu-on-android/

With ADB you can perform various tasks against your Android device.
For example this script wait for a device and then install an app (MDM.apk) and remove some other unnecessary apps:

cd C:\Users\sysadmin\AppData\Local\Android\android-sdk\platform-tools
adb wait-for-device
adb install C:\Data\MDM.apk
adb shell pm uninstall com.vodafone.android.myweb.launcher
adb shell pm uninstall uk.amazon.mShop.android
adb shell pm uninstall com.ebay.mobile

To browse all the apps installed you can use this command:

adb shell pm list packages

More commands can be found here: Http://developer.androud.com/tools/help/shell.html

Then I used monkeyrunner for automate some configuration. Monkeyrunner can be found in this folder: ..\Android\android-sdk\tools
Then I downloaded two scripts to make using monkeyrunner easier: https://github.com/miracle2k/android-platform_sdk/tree/master/monkeyrunner/scripts
Copy monkeyrunner_recorder.py and monkeyrunner_playback.py into ..\Android\android-sdk\tools folder.

If you already connected your Android device via USB with USB_DEBUG enabled the run this command for record some actions:

monkeyrunner monkeyrunner_recorder.py

Using the mouse perform the actions on MonkeyRunner Recorder screen then save the script clicking on the menu “Export Actions”. Save the file with file extension “.mr”. For example C:\Data\test.mr

To perform the same action you recorded again type this command:

monkeyrunner monkeyrunner_playback.py C:\Data\test.mr

Remember to insert some sleep after each action you recorded because probably your device is not so quickly as commands are sent by script.

PRESS|{'name':'HOME','type':'downAndUp',}
WAIT|{'seconds':1.0,}
TOUCH|{'x':479,'y':822,'type':'downAndUp',}
WAIT|{'seconds':1.0,}
TOUCH|{'x':60,'y':822,'type':'downAndUp',}
WAIT|{'seconds':8.0,}
TOUCH|{'x':120,'y':800,'type':'downAndUp',}
WAIT|{'seconds':1.0,}
TOUCH|{'x':120,'y':700,'type':'downAndUp',}
WAIT|{'seconds':1.0,}
TYPE|{'message':'mdm-kion.telekom.ebf.de',}
WAIT|{'seconds':1.0,}
TOUCH|{'x':479,'y':600,'type':'downAndUp',}

Here a list of actions that you can type directly on your monkeyrunner script:

  • To press “HOME” Button:
     PRESS|{'name':'HOME','type':'downAndUp',} 
  • To type something:
     TYPE|{'message':'mdm-kion.telekom.ebf.de',} 
  • To Scroll Down:
     DRAG|{'start':(50,500),'end':(316,500),'duration':1.0,'steps':10,} 
  • To Scroll Left:
     DRAG|{'start':(216,768),'end':(216,153),'duration':1.0,'steps':10,} 
  • To wait 1 second:
     WAIT|{'seconds':1.0,} 

Following some ADB notes:

  • connect adb over Wi-Fi
     adb shell setprop service.adb.tcp.port 5555 && stop adbd && start adbd adb connect 
  • Unlock your Android screen
     adb shell input keyevent 82 
  • Lock your Android screen
     adb shell input keyevent 6 adb shell input keyevent 26 
  • Open default browser
     adb shell input keyevent 23 
  • Keep your android phone volume up(+)
     adb shell input keyevent 24 
  • Keep your android phone volume down(-)
     adb shell input keyevent 25 
  • Go to your Android Home screen
     adb shell input keyevent 3 
  • Take Screenshot from adb
     adb shell screenshot /sdcard/test.png 
  • start clock app
     adb shell am start com.google.android.deskclock 
  • stop clock app
     adb shell am force-stop com.google.android.deskclock 
  • start wifi settings manager
     adb shell am start -a android.intent.action.MAIN -n com.android.settings/.wifi.WifiSettings 

Via: https://software.intel.com/en-us/android/articles/automated-android-application-testing
http://testautomationdiary.blogspot.de/2014/05/testing-android-devices-using.html

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s