Wednesday, November 26, 2014

Robotium - Android Application UI Testing Tool

Robotium is the android test automation framework for testing native and hybrid android applications. It provides simple API to write UI automation scripts. However, Number of tools are available for testing android applications, Robotium is the most commonly used Android testing tool.
Robotium can be used to write functional, system and user acceptance testcases. Robotium Testcases can be executed in Android emulator as well as the Android real device.

What is covered in this Robotium Tutorial:

  • Benefits
  • Prerequisites
  • Creating Robotium Project
  • Creating Robotium Class
  • Some Robotium Methods
  • Locating  Elements in Android application
  • Sample Code
  • Executing Robotium Project
Robotium tutorial

Benefits of Robotium

  • Easy to write
  • Simple API (All Methods are available only in Solo Class)
  • Automatic Delays & Timings
  • No need to write code , when navigating from one activity to another activity.
  • Test Android native apps as well as hybrid app.
  • Able to handle multiple android activities.
  • Less time to write tests as the API is simple.
  • Testcases are Robust , due to runtime binding to UI components
  • Fast testcase execution.
  • Integrates easily with Maven and ANT.

Prerequisites

1) Download and Install JAVA
– Download Java from this page.
– Add java libraries to the PATH and set JAVA_HOME to the root of java installation directory in your environment.
2) Download ADT Bundle
  • Download ADT bundle from this page.
  • Extract ADT bundle zip and put it in a folder.
  • Set ANDROID_HOME to the root of ADT bundle folder in your Environment.

Creating a Robotium Project

Only few steps required to create a project,
Step #1: Open eclipse contains your android application to be tested.
(Click on image to enlarge)
robotium tutorial-1
Step #2: Right click on the android project -> Android Tools and click on new Test project.
Step #3: Give name for the Test project and click Next button.
robotium tutorial-2
Step #4: Select The Application under test as the target and click finish button.
robotium tutorial-3
Step #5: Test project will be created in the eclipse workspace.
Step #6: Download Robotium solo jar from here.
Step #7: Right click on the Test project in the workspace Go to Build path and click Configure build path.
Step #8: Switch to libraries tab, click on “Add external jars” option and browse the downloaded Robotium jar file and add it to the libraries and click “OK”.
robotium tutorial-4
Step #9: Robotium Test project is created successfully. Now we can create classes under the project and start writing the test cases.

Creating Robotium Class

#1: Right click on the package under src directory in the Test project, and create a new class.
#2: Import the Main Activity class the Robotium test project.
Syntax:
1
import com.sasi.attendanceproject.Home;
#3: New class will inherit properties from ActivityInstrumentationTestCase2 class
1
2
public class AttendanceTest extends
ActivityInstrumentationTestCase2 <home>
(Note: Here, Home is the activity to be tested in the android application)
#4: Create Instance for Solo class as below
1
private Solo solo;
#5: Create a Constructor for the Test class, as below
1
2
3
public AttendanceTest() {
    super(Home.class);
    // TODO Auto-generated constructor stub
#6: Create setUp and tearDown methods , as below
Setup method is used to Initiate the Instrumentation
1
2
3
4
public void setUp()throws Exception
{
   solo=new Solo(getInstrumentation(), getActivity());
}
Teardown method is used to close the activity after the test has been completed.
1
2
3
public void tearDown() throws Exception
{ solo.finishOpenedActivities();
}

Some Robotium Methods

#1. assertCurrentActivity (text,Activity)
This method verifies whether the current activity is the activity which is passed as the send parameter.
Syntax
1
solo.assertCurrentActivity("Current Activity", Home.class);
#2. clickOnButton(text)
This method will click on the button with the specified Text.
Syntax:
1
solo.clickOnButton("ADMIN");
#3. clickOnButton(int)
This method will click on the button with the specified index.
Syntax:
1
solo.clickOnButton(2);
#4. waitForText(text)
This method will wait until the text appearing on the activity.
Syntax:
1
solo.waitForText("Creating New Password");
#5. enterText(int, text)
This method will type the text passed as the second parameter to the specified index editbox.
Syntax:
1
solo.enterText(0,"test");
#6. clickOnCheckbox(int)
This Method will click on the checkbox with given index.
Syntax:
1
solo.clickOnCheckBox(0);
#7clickOnRadioButton(int)
This Method will click on the Radio button with the given index.
Syntax:
1
solo.clickOnRadioButton(1);
#8. clickOnImage(int)
This Method will click on the image with the given index.
Syntax:
1
solo.clickOnImage(1);
#9. clearEditText(int)
This Method will clear the text in the edit box with the given index.
Syntax:
1
solo.clearEditText(0);
#10. waitForText(text)
This Method will wait until the given text is appearing on the activity.
Syntax:
1
solo.waitForText(“Robotium”);

Example Program

Locating Elements in Android Application

Step – 1
Open Eclipse containing Android Application to be Tested.
Step – 2
Expand the project , Go to res ? Layout folder and double click on the activity XML file, you want to automate. It will open the designed activity on the eclipse editor.
Step – 3
Locating Button Element
Locating Element By ID
Click on the Element , you want to locate. On the right side properties panel, you can find the ID of that element. (ignore @+id/)
(Click on image to enlarge)
robotium tutorial-5
In the Above figure ID of the element is, btnadmin.
Corresponding Robotium code to locate this button is,
1
solo.clickOnButton(R.id.btnadmin);
Locating Element By Position
robotium tutorial-6
In case, if ID is not available then you can locate the element using the order. If the button is in the second position, Then you can locate the button by,
1
solo.clickOnButton(1);
Locating Element By Text
robotium tutorial-7
Even you can click on the button using the text displayed on the button.
1
Solo.clickOnButton(“ADMIN”);
Locating Text box Element 
Using Similar way click on the text box you want to enter data and find the position of the text box from the right side.
robotium tutorial-8
1
2
Solo.enterText(0,”Anitha”);
//Username textbox is in 1st position
1
2
Solo.enterText(1,”test”);  
//Password textbox is in 2nd position
Locating a Radio Button Element
Click on the Radio Button Element , you want to click and find out the position of the radio button.
robotium tutorial-9
1
2
Solo.clickOnRadioButton(1);
//It will location radio button in the second position.

Sample Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.sasi.attendanceproject.test;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.EditText;
 
import com.robotium.solo.By;
import com.robotium.solo.Solo;
import com.robotium.solo.WebElement;
import com.sasi.attendanceproject.Home;
 
public class AttendanceTest extends ActivityInstrumentationTestCase2<Home>{
    private Solo solo;
    public AttendanceTest() {
        super("com.sasi.attendanceproject.Home",Home.class);
        // TODO Auto-generated constructor stub
    }
 
    public void setUp()throws Exception{
        solo=new Solo(getInstrumentation(),getActivity());
    }
    public void testAttendance()throws Exception{
        //solo.assertCurrentActivity("Current Activity", Home.class);
        solo.waitForWebElement(By.id("btnadmin"));
        solo.clickOnButton("ADMIN");
        solo.clickOnButton(0);
        solo.waitForText("Creating New Password");
        solo.enterText(0, "test");
        solo.enterText(1, "test");
        solo.clickOnButton("Okay");
        solo.waitForText("Attendance Login");
        solo.enterText(0, "Anitha");
        solo.enterText(1, "test");
        solo.clickOnButton("Login");
        solo.waitForWebElement(By.id("btnaddnew"));
        solo.clickOnButton("Add New Details");
        solo.waitForText("Enter the Employee Details");
        solo.enterText(0, "Anitha");
        solo.enterText(1, "6");
        solo.enterText(2, "Testing Engineer");
        solo.clickOnRadioButton(1);
        solo.clickOnButton("Okay");
        solo.waitForWebElement(By.id("tvempID"));
        System.out.println(solo.getText(0));
 
    }
    public void tearDown()throws Exception{
        solo.finishOpenedActivities();}}

Executing Robotium Project

From Eclipse:

Right click on the project and select Run As -> Android Junit Test

From Command Prompt:

Step 1:
Cd to your Android Test Project Directory
Step 2:
Run the following command,
1
2
adb shell am instrument -w <<package name of your test
class>>/android.test.InstrumentationTestRunner
robotium tutorial-10

Conclusion

  • Robotium is the most commonly used Android Test automation tool.
  • Robotium Testcases can be executed on Android Emulator as well as the Real device, we don’t need to write any specific configuration code to run Robotium test cases on Real device.
  • Robotium Can be easily written in maven project also, and it can be run through continuous integration tools.
  • Thus, Robotium is very useful in writing easy/simple Android Test automation scripts.

No comments:

Post a Comment