Wednesday, October 24, 2012

Running android automation script using batch file


Run android automation script and copy test result files on local machine (using batch file):

Here, I am taking example of retrieving screenshots taken by robotium. Robotium test script, in this example, runs on emulator/device and takes screenshots at regular intervals. Screenshots taken by robotium are stored in sdcard/Robotium-Screenshots/ folder. For demo purpose, I am running script on emulator but same script can be run on device also.

Note: You can retrieve any file like test results etc. in the same manner. You can even first push test data files on device/emulator and after execution pull the result files.


To run on actual device:
  1. Comment/Remove commands which launches emulator and other emulator related commands.
  2. Make sure device is attached to system and adb devices show the device.

Create a batch file and copy paste following code

@ echo OFF
REM Path of android sdk
SET SDK_DIR=c:\android-sdk

REM Path of folder where screenshots will be copied
SET SCREENSHOT_DIR=c:\temp\screenshot
SET TIMESTAMP=%date:~-4,4%%date:~-10,2%%date:~-7,2%%time:~-11,2%%time:~-8,2%
SET APK_PACKAGE=%1
SET APK_FILE=%2
SET EMULATOR=%3
SET EMULATOR_PORT=%4

IF [%1]==[] GOTO ErrLbl
IF [%2]==[] SET APK_FILE=*.apk
IF [%2]==[] SET EMULATOR=testEmu
IF [%3]==[] SET EMULATOR_PORT=5554

echo Setting path environment variable
SET PATH=%PATH%;%SDK_DIR%\tools;%SDK_DIR%\platform-tools

SET SCREENSHOT_DIR=%SCREENSHOT_DIR%\%EMULATOR%_%EMULATOR_PORT%_%TIMESTAMP%
echo Creating the screenshot folder %SCREENSHOT_DIR%
mkdir %SCREENSHOT_DIR%

echo Launching emulator %EMULATOR%
emulator @%EMULATOR% -port %EMULATOR_PORT% -scale 0.8 -no-boot-anim –no-window
wait 120

echo Waiting for device to come online
adb wait-for-device

echo Press any key only when emulator is completely online.
pause
adb shell input keyevent 82

echo Installing apk file: %APK_FILE%
adb install %APK_FILE%

echo Deleting existing screenshots from device
adb shell rm sdcard/Robotium-Screenshots/*
pause

echo Running test: %APK_PACKAGE%
adb shell am instrument -w
%APK_PACKAGE%/android.test.InstrumentationTestRunner

echo Copying Screeshots from sdcard to local Dir - %SCREENSHOT_DIR%
adb  pull
sdcard/Robotium-Screenshots/ %SCREENSHOT_DIR%\

echo Deleting screenshots from device
adb shell  rm
sdcard/Robotium-Screenshots/*
pause
echo Killing emulator
REM This command does not work on Windows OS but should work on other OS
REM adb -s emulator-%EMULATOR_PORT% emu kill
REM For Windows here is the workaround which is not good solution but does desired task
taskkill /IM emulator-arm.exe
REM Below command is required to avoid execution of ErrLbl content
exit /B

:ErrLbl
echo Package name is MISSING.

1 comment:

  1. Hello,
    The Article on Running android automation script using batch file is nice. It give detail information about it .Thanks for Sharing the information mobile application testing

    ReplyDelete