Test Android Apk File With Robotium
Test Android Apk File With Robotium
Test Android Apk File With Robotium
To test an Android apk file using Robotium requires to Install apk file on device we will use AndroidCalculator apk file, click here to find apk Create Test Project using eclipse to test that apk Note: If android working environment is not set on your machine, follow Set up Android Working Environment
Important Steps:
1.If you know the certificate signature then use the same signature in test project 2. If you do not know the certificate signature then delete the certificate signature and use the same android debug key signature in both the application and the test project 3. If the application is unsigned then sign the application apk with the android debug key To un-signed a signed application download java program from http://www.troido.de/re-sign.jar double click on it to open it and drop apk file on it Or * Un-zip the apk file * Delete the META-INF folder * Re-zip the apk file It will zip file as "applicationName.apk.zip" , rename it and make it as "applicationName.apk" * In Dos prompt/Terminal write following commands > jarsigner -keystore ~/.android/debug.keystore -storepass android -keypass android applicationName.apk androiddebugkey > zipalign 4 applicationName.apk TempApplicationName.apk Then rename TempApplicationName.apk to applicationName.apk if you need. For more help visit: http://developer.android.com/guide/publishing/app-signing.html
Load Emulator
Load command prompt (windows user) or Terminal (Linux users) and write following command to run emulator emulator -avd <device name> here the Device Name is our AVD (Android Virtual Device) created in Set up Android Working Environment, it will load emulator in few seconds. If it shows some error most probably the Android SDK path in not set in PATH environment variable. One can set
up Android SDK path in next step, skip next step if emulator is working
adb install <path>/AndroidCalculator.apk the <path> showing the directory where AndroidCalculator.apk is located, in my case it was adb install /home/naveed/AndroidCalculator.apk as apk is at my home directory ( I am using ubuntu). You need to enter your own path where apk resides. It will successfully install apk file and show success Note: For sake of understanding Test Project app will be installed on emulator which will then call/load the AUT (already installed) and perform test scenarios on it. When we will run our Test project through Eclipse it will automatically install it on emulator (will do it in coming sections)
From New Android Project Window, enter Test Project Name as 'TestProject', and click on Next
On next window under 'Select TestTarget' section select 'This project' option & click 'Next'
On 'Next' window under 'Select Build Target' section select your desired SDK version, we will select Android 2.2 or one can select any of his choice & press Finish.
NOTE: A problem specially with novice, when they first time make new workspace and start working with Robotium If we create new workspace for this tutorial, on creating Test Project eclipse will show error 'java.lang.NullPointerException', because it is new workspace and eclipse is not able to get selected Android resources for first test project.
We can solve this issue by following any of the option, 1. Create another TestProject & eclipse will automatically get selected resource(for new one) & not show error for second created Test Project(we can use second one for our work) 2. First create an Android Project (we can also use Android Sample application bundled with Android SDK), run it, then create Test Project will not show error To manually create package right click on project select New > Package. On 'New Java Package' window fill package name against 'Name:'. Like to test AndroidCalculator which has com.calculator package name, TestProject should be com.testcalculator or com.calculator.test package name
Test can also be run using command prompt/terminal by following simple steps * Write following command to install AndroidCalculator apk on emulator > adb install <path>/AndroidCalculator.apk * Write following command to install AndroidCalculatorTest apk on emulator > adb install <path>/AndroidCalculatorTestApk.apk * Run the test cases: > adb shell am instrument -w com.testcalculator/android.test.InstrumentationTestRunner Note: Click here to download TestProject project source code