Anda di halaman 1dari 18

ROBOTIUM

Automated Testing for Android Application


Robotium is an open source automation testing framework that is used to write a robust
and powerful black box for Android applications. It fully supports testing for native and
hybrid applications.
The test cases written using Robotium can either be executed on the Android Emulator
(Android Virtual Device (AVD)) or on a real Android device.
Features of Robotium:

Robotium helps to write powerful test cases.

The Android platform is not modified by Robotium.

The Robotium test can also be executed using command prompt.

It's possible to run Robotium without a source code

Robotium can detect the messages that are shown on the screen (Toasts) it supports
Android features such as activities, menu, and context menu.

Both AUT and Project run on the same JVM, with the help of Dalvik Virtual Machine
(DVM) (DVM is the software that runs the Apps on Android devices and Emulators).

At first we need to have Android Environment to make application run before starting up
with Robotium framework.
Android Environment setup:

Checklist:
1. JDK - F:\Robotium\RobotiumSoftwares
2. JRE - F:\Robotium\RobotiumSoftwares
3. Eclipse

Integrated
with
F:\Robotium\RobotiumSoftwares\RobotiumSoft_Dump
4. SDK - F:\Robotium\RobotiumSoftwares\SDK

ADT

Setup Environmental Variables for Java: To make the java class accessible and
to run the java code.

For Windows 7
1. Select Computer from the Start menu.
2. Choose System Properties from the context menu.
3. Click Advanced system settings > Advanced tab.
4. Click on Environment Variables, under System Variables, find PATH, and click on it.
5. In the Edit windows, modify PATH by adding the location of the class to the value
for PATH. If you do not have the item PATH, you may select to add a new variable
and add PATH as the name and the location of the class as the value.
6. Reopen Command prompt window, and run your java code.
Windows XP
1. Start -> Control Panel -> System -> Advanced.
2. Click on Environment Variables, under System Variables, find PATH, and click on it.
3. In the Edit windows, modify PATH by adding the location of the class (C:\Program
Files\Java\jdk1.7\bin) to the value for PATH. If you do not have the item PATH, you
may select to add a new variable and add PATH as the name and the location of the
class as the value.
4. Close the window.
5. Reopen Command prompt window, and run your java code.
Note: Open Dos prompt and type below command to make sure Java is sworking in the machine

Download Eclipse: It is recommended to download ADT bundle, it includes Android


SDK and Eclipse IDE with built-in ADT (Android Developer Tools). Download Link:
http://developer.android.com/sdk/index.html

SDK Setup:

To make Android application work in the machine, SDK should be configured. There are two
ways to do Through Command Line and another through SDK.exe file available in the
bundle
To configure SDK through Command line:
1. Go to the path where SDK is installed
2. Copy the path till Platform-tolls and add to the PATH in System Variables
<Path>/SDK/tools.
3. To check its working fine, Open DOS Command and type below command
>Android: SDK Manager will open and it will automatically update latest APIs.

To configure SDK through SDK.exe:

1. Open the ADT Bundle after download.


2. Double click on SDK.exe file. SDK Manager will open and it will automatically update
latest APIs.

Note: It is best practice to set the System Variables

After Successful completion of the Android Setup, Now we have to configure ADB, ADB
serves as a communication channel between user and Android device or Emulator.

Setup Environmental Variables for ADB

1. Go to the path where SDK is installed


2. Copy the path till Platform-tools and add to the PATH in System Variables
<Path>/SDK/ platform-tools.
3. To check its working fine, Open DOS Prompt and type below command
4. >ADB

Getting Started with Robotium


At first we should have below Software to starting up robotium Framework:
1.
2.
3.
4.
5.

Eclipse - Yes
ADT - yes
SDK - yes
Junit(4.5.Jar) - Yes
Robotium(Solo-4.3.1.jar ) - Yes

By this time we should have Eclipse, ADT and SDK in the bundle which was downloaded.
UnZIP it to a safe location on your machine. Since bundle has built in ADT we need not
have to configure/Install ADT explicitly.
Adding the Android SDK location to Eclipse:
To add the Android SDK to Eclipse, follow below steps:
1. In Eclipse, under the Window menu, click on Preferences.
2. Check the Android tab (The presence of this tab clearly indicates that the Android ADT
plug in is successfully installed) and a window will appear, as shown in the following
screenshot.

3. Browse and give the SDK location (Location where the ADT bundle is Un-Zip) the SDK
location informs Eclipse where the Android SDK is located in the system.
4. The list of SDK targets will be reloaded only when the proper SDK location is provided
and the Apply or OK button is clicked on.
Note: It is best practice run the SDK manager once before providing the location so that all the latest APIs are
downloaded.

Setting up the AVD


An AVD /Emulator is used to run Android applications in place of physical device.
1. Click on icon to start AVD available on Eclipse menu bar

2. Click on Device Definition to configure the AVD. We can create customize AVD, by
clicking new Devices.

3. Click on Create AVD and observe Android Virtual device Tab, AVD definition will be
visible in it as shown below.

4. Click on Start and launch the AVD.AVD thus created will appear as shown below.

By this point, we should have a working setup of the Android and Robotium Environment.
Now we have to start with Robotiums test case writing and execution.
As discussed earlier Robotium automation can be executed for both White Box
Testing(where the Sourcecode is avaliable) and Black Box Testing(Where only .Apk file is
avalaibale)this very feature of Robotium makes it more relaibale and robust framework.

Executing Black Box Testing Using Robotium:

Checklist
1. Install AUT (Application Under Test). Apk file
2. Package Name
3. Main Activity Name
Step1:Create Test Project:
1. Click on file->New->Project->select Android Test Project(as shown below)

2. Provide Project name and click on next button.

3. Select this project(Note: when we start Eclipse for the very first time this window will
be blank)

4. Select the API for the AUT to be tested which was configured earlier (This
configuration will design the AVD later on). Click on finish.

5. On clicking Finish button a n Android Test Project will be created with no package, but
this will throw an error as shown below.

Note: 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 option:
Create another Test Project & 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).

Step2:Create Package for Test Project:


1. Click on SRC ->Right Click->New->Package
Note: Package name must be the name of the .apk

2. Once the Package is created Package Explorer looks as below. Add the Test case to
the package.
3. On creating the Package, a Package and a class are created with their corresponding
names.

4. Once the Package is created we have to import Robotium jar files to import Robotium
solo class into our test suit as shown below.
a. Right click on the project->Build Path->Configure Build Path

b. In properties Test Project window, Unfold Resource and select Java Build
Path.
c. In Libraries tab click on Add External JARs and browse the robotium Solo
jar file.

d. In Order and Export tab select the check the Robotium solo jar file and
click on OK button.

Code Test Class:

1. Copy below code inside Testcase calss()


package <Package Name>;
import android.test.ActivityInstrumentationTestCase2;
import com.jayway.android.robotium.solo.Solo;
@SuppressWarnings("unchecked")
public class TestCase extends ActivityInstrumentationTestCase2
{
private static final String TARGET_PACKAGE_ID = "<Package Name as of the AUT>";
private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "<Full Launcher
activity name>"
private static Class<?> launcherActivityClass;
static{
try
{
launcherActivityClass=Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
}catch(ClassNotFoundException e){
throw new RuntimeException(e);
}
}
@SuppressWarnings("unchecked")
public TestCase() throws ClassNotFoundException
{
super(TARGET_PACKAGE_ID,launcherActivityClass);
}
private Solo solo;
@Override protected void setUp()throws Exception
{
super.setUp();
solo = new Solo(getInstrumentation(),getActivity());
}
public void testCanOpenSettings(){

<Main Login for the Test case


eg: solo.clearEditText(1);
solo.enterText(1, "20");>
}
@Override
public void tearDown() throws Exception
{
try
{
solo.finalize();
}
catch(Throwable e)
{
e.printStackTrace();
}
getActivity().finish();
super.tearDown();
}
}
2. Make necessary changes in the code as shown below.

3. Open Manifest.xml file from the Project tree, Copy the below code in it and make
changes in - <instrumentationandroid:targetPackage="<Package Name>" and save
it
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sampleproject.test.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="18" />
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="<PackaheName>" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<uses-library android:name="android.test.runner" />
</application>
</manifest>

Run the Test:


1. Right click on project and Run As Android JUnit Test. Wait for some time, ADT will
automatically load AVD as below:
2. Load the .apk file into the AVD.
Open DOS Prompt and enter the following:

>adb devices
>adb install <path>/<filename>.apk

3. Right Click on Project -> Run As->Android JUnit Test. It will automatically run the test
case. Logs can be observed in Console window

Anda mungkin juga menyukai