Skip to content

Category: Robotium Android Automaton

ROBOTIUM

Table of Contents:

1. Robotium

2. What all we need before we get it started?

3. How to setup Robotium using Eclipse ADT with source code of Android Application?
3a. Basic Android Test Project with source code

4. How to setup Robotium using Eclipse ADT without source code of Android Application?
4a. Basic Android Test Project without source code

5. What is JUnit Test Suite and why we need it with Robotium?

6. How to Data-Drive Robotium Scripts?

7. Issues or challenges with Robotium

 

Please skip to second post If you are reading this post for the first time

 

ROBOTIUM 5.2.1 Released

 

New Features and bug Fix details:

 

unlockScreen() – Unlocks the screen lock.

getCurrentViews(Class<T> classToFilterBy, boolean includeSubclasses) – New parameter includeSubclasses set to true if also subclasses should be returned.

scrollToSide(Side side, float scrollPosition, int stepCount) – Added parameter stepCount to allow for users to decide the speed of the scroll.

scrollViewToSide(View view, Side side, float scrollPosition, int stepCount) – Added parameter stepCount to allow for users to decide the speed of the scroll.

Bugs fixed:

Issue-600 – ViewFetcher, getRecentDecorView fails on Samsung S4 with MultiWindow support

Issue-579 – Screenshots do not get saved

Issue-589 – waitForFragment on vers < 3.0

Issue-615 – ScrollToSide opens the navigation bar

I hope you will enjoy this release!

Link To Download: CLICK HERE

Robotium Recorder 2.0 is released!

From,
Renas Reda
Founder and CEO at Robotium Tech
We’re excited to announce the release of Robotium Recorder 2.0. Thanks to all the feedback that we’ve received, we’ve been able to make hundreds of improvements since it first launched. It’s now compatible with more apps and is optimized to generate even smarter tests than before.New in Robotium Recorder 2.0:– Improved click and gesture support.- Click to Assert™ has been further refined and is now performing optimally in a broad range of apps.- Record click and drag coordinates by selecting the option in “Settings”.- Full support for GL apps and games by enabling touch and drag coordinates.- After a test case has been saved it can now be executed directly from Robotium Recorder by clicking “Run Test”.

Robotium Recorder 2.0 comes with a new trial period, so even if you have consumed the trial of the old version you are now able to try this new version as well.

To update Robotium Recorder, in Eclipse go to Help –> “Check for updates”

Details on how to install explained in the Old posts below.

“““““““““““““““““““““““““ “““““““““““““““““““““““““ ` “““““““““““““““““““““““““ “

OLD POSTS

ROBOTIUM 5.0.1 Got Released !!!

Robotium Rec. Latest Update: 1.4.11 (23/01/14)

Quote:

Robotium Recorder updates are releasing almost daily.. The first update what I got was 1.2.95 and now we have the latest update as 1.4.5 (22/01/14). I will be updating this post as I find new updates…

Renas Reda
Founder and maintainer of Robotium

said,

Quote:

We’re happy to announce the release of Robotium 5.0.1! Included in this release is a new Config class, where Robotium Developers can change various things like default timeouts, screenshot location, screenshot type, scrolling, and web element click behaviour.

We’ve been working hard on improving the various click methods by testing numerous popular apps under sub-par performances to add further logic to the click methods and thus reduce click issues. In the last year we’ve gone through the whole code base and improved various areas. This is an update we recommend for everyone using older Robotium versions.

The new changes in 5.0.1 are as follows,

NOTE: The package name has been changed to com.robotium.solo. You’ll need to update the import line at the top of your current test classes from com.jayway.android.solo to com.robotium.solo.

New functionality in Robotium 5.0.1:

pressSoftKeyboardNextButton() – Presses the next button on the soft keyboard.

waitForEmptyActivityStack(int timeout) – Waits until the Activity stack is empty.

New Class com.robotium.solo.Solo.Config:

int timeout_large – The timeout length of the waitFor methods. Default length is 20 000 milliseconds.

int timeout_small – The timeout length of the get, is, set, assert, enter and click methods. Default length is 10 000 milliseconds.

String screenshotSavePath – The screenshot save path. Default save path is /sdcard/Robotium-Screenshots/.

ScreenshotFileType screenshotFileType – The screenshot file type, JPEG or PNG. Use ScreenshotFileType.JPEG or ScreenshotFileType.PNG. Default file type is JPEG.

boolean shouldScroll – Set to true if the get, is, set, enter, type and click methods should scroll. Default value is true.

boolean useJavaScriptToClickWebElements – Set to true if JavaScript should be used to click WebElements. Default value is false.


Improvements:

All the click methods. Improved behaviour in unfavorable situations.

The waitForDialogToOpen & waitForDialogToClose methods. Now recognises all dialogs.

The internal View handling class.

TakeScreenshot() now supports GLSurfaceViews with OpenGL ES 3.0.

See the Javadoc for an example on how to use the new Config class.

We hope you will enjoy this release!

https://code.google.com/p/robotium/downloads/list

Quote:

This will be a Full Tutorial on Robotium Android Automation Tool
I will be updating this thread as I get time. SO NO ETA’s.

Quote:

If anybody wants to use / copy this tutorial to their Website or Blog please feel free to contact me at my personal email id: edy.434@gmail.com
or at our official Website: www.rhbroms.com

Quote:

Please Click on Thanks button if this tutorial by me had helped you for some extent !!!

User Guide for Robotium Recorder ( Skip this guide if you are reading this post for the first time and move on with the second post..)

Link to Robotium Official website: Click Here

Prerequisites:

Install Robotium Recorder, if not already installed.

Connect a device (recommended) or start an emulator with external storage.

Start Robotium Recorder

Start Robotium Recorder from the toolbar: Eclipse > New > Other > Android – Robotium Recorder.

Robotium Recorder can also be started by right clicking an existing app or test project and from the opened menu select: Robotium Recorder > New Robotium Test.

Record test cases from a workspace project

From Robotium Recorder it’s possible to select either a binary (APK file) or a workspace project to record test cases from. In the screenshot below a workspace project has been selected. Enter a test project name and click “Next”.

Record test cases from a binary APK

To record test cases from an APK, click “select apk”. A file browser will be opened where the APK can be selected. Robotium Recorder will automatically sign the APK with the local debug certificate.

Once the signing process has finished the “Next” button will be enabled.

Start recording

Clicking Next on the previous screen will show the recording screen.

In the left bottom corner there is Setting menu with three options:

a. Use sleeps – choose if sleeps should be used to playback test cases in the same speed as they were recorded. Can be useful for slower apps like bandwidth intensive or hybrid apps.

b. Keep app data – choose if app data is to be kept when starting a new recording session.

c. Identify class over string – default View identifier is always the resource ID. In the event a resource ID is missing it’s possible to choose if a View class identifier is to be used over a string identifier (the text displayed by the View).

Click “New Robotium Test”.

If an APK is selected, Robotium Recorder will first install the re-signed APK on the device or emulator.

When installation of the workspace project or APK is done the recording will be started.

The app is executed on the device or emulator and recording of user actions starts.

The recorded steps are shown in the test step table (as seen below)

If a test step is selected in the table the “Delete step” and “Take screenshot” buttons will be enabled.

1. Select any step in the table and click on “Delete step” to delete it.

2. Click “Take screenshot” to add a screenshot step after the selected step.

To stop the recording click “Stop Recording”.

To save the test case click “Save”, enter a test case name, and click “Ok”.

If the test project already exists it will save the test case in that project. Otherwise it will first create the test project and then save the test case. Either start a new recording or close Robotium Recorder by clicking “Finish”.

To see the saved test case(s) expand the test project and look in the src folder. The whole test project or a specific test case can be executed by right clicking it and selecting Run As > Android JUnit Test.

1. Robotium

 

Robotium is an open-source test framework for writing automated Black-box test casesfor Android applications (Native/3rd party). With Robotium we can write function, systemand acceptance test scenarios.

Robotium can be used both for testing of apps where source code is available and appswhere only APK (Android Application Package) file is available and the implementation will be discussed in 4th chapter.

Robotium frame work released under Apache License 2.0. Its founder and main developeris Renas Reda. Latest Version of Robotium is 4.3.1(At the point of writing this book). The home page of Robotium is located under the following URL Robotium Home Page

Robotium extends the Android test framework to make it simpler to write tests. The main class for testing with Robotium is Solo.
Solo is initialized with the instrumentation of the test case and the first activity to test.

2. What all we need before we get it started?

We need the below:

1. Windows 7/8 OS
2. Java 6
3. Eclipse ADT
4. Android device running min android version 2.3.4*
5. Robotium Jar files (Robotium-solo-4.3.1.jar and Robotium-solo-4.3.1-javadoc.jar)
6. POI Jar files
7. Source code of the app to be tested*

*Are not mandatory.

DOWNLOADS:

SimpleCalc app sourcecode: SimpleCalc

Robotium jar files : file1 and file2

POI jar files: POI.zip

 

3. How to setup Robotium using Eclipse ADT with source code of Android Application?

 

1.Open Eclipse ADT.

2.Import the source code of the application (to be tested) to eclipse.

3.I have given a source code which is written by me for testing purpose, you can find it here. [To import the project go to File ->Import -> select General -> Archive file -> Click on finish button]

4. Remove all the Errors as well as Lint warnings that might be present in the project by right clicking on the project (SimpleCalc) -> properties -> Android Lint Preferences -> Click on Ignore all button.

5. Create new Android Test project (Go to File -> New -> Other.. -> Select Android Test Project and give the project name as SimpleCalc-test). Details shown from the belowscreen shots:

NOTE: Select the option “An existing Android project” and select SimpleCalc project and click on Next button, this means you are going to test SimpleCalc app which is given by me.

6. Now right click on the empty package file that you get (Project_Name -> src -> Package) and select new Junit test case as show in below screen shot.

7. Now you will see a window where in you have to specify the Name of Junit test case as shown below (give it as TestCase_1):

8. Now you have to create new folder with name Robotium

9. Inside the Robotium folder paste the downloaded Robotium jar files.

10. Now go to Java Build Path by right clicking on the project -> properties and add the Robotium jar file as shown below:

11. Now click on the dropdown list of the Robotium jar file and select javadoc option as shown below:

12. Click on edit button and use the Robotium-solo-4.3.1-javadoc file as shown in the belowscreen shot:

13. Now go to Order and Export tab and click on select all button as shown below and click ok button:

3a. Basic Android Test Project with Source Code

1. Now your project will somewhat look like below:

2. Now we have to do some editing work to make this test case work as shown below:

Quote:

package com.rhb.simplecalc.test;

import com.jayway.android.robotium.solo.Solo;
import com.rhb.simplecalc.MainActivity; // we have to import this class for testing our SimpleCalc application

import android.test.ActivityInstrumentationTestCase2; // You can run any test method on the UI thread

public class TestCase1 extends ActivityInstrumentationTestCase2<MainActivity> {

private Solo solo; // solo is the main class of Robotium

public TestCase1() {
super(MainActivity.class); // Activity name of the class that you want to test.
// TODO Auto-generated constructor stub
}

@override
public void setUp() throws Exception {
solo = new Solo(getInstrumentation(), getActivity());
}

public void test() throws Exception{

// check that we have the right activity
solo.assertCurrentActivity(“wrong activiy”, MainActivity.class);

// enter the value “12” into first edit text
solo.enterText(0, “12”);

// enter the value “2” into first edit text
solo.enterText(1, “2”);

// Click on the first button
solo.clickOnButton(0);

// searchForText has a timeout of 5 seconds
assertTrue(solo.waitForText(“24”));
}

@override
public void tearDown() throws Exception {
solo.finishOpenedActivities(); // finishes all opened activities
}

}

3. To Run the test just right click on the SimpleCalc-Test project -> Run As -> Android JUnit Test

4. Now you will see a tab of JUnit where in you will see your test results(Passed/Failed).

5. On the below Screen Shot I have shown you how the JUnit results are shown when the test is pass.

6. As you can see from the above screen shot clearly that our test is pass and the color indication is Green. If in case our test gets failed the color will be changed to Red as shown in below screen shot:

7. And the Fail reason will be shown at the Failure Trace as you can see from the above screen shot. Here I have purposely commented on the both edittext fields so that I can show you how the failed test case report will be.

8. Now the thing is how to take out the report? It’s easy, there are 3 ways of doing it they are:

8(a). By taking Logcat.

8(b). By Clicking on the Test Run History -> Export option you will get a xml file of the test report, and lastly.

8(c). Pushing the Test report to a text or excel file to the device.

9. Now we will see how to test the application when we don’t have source code with us in the next chapter.

4. How to setup Robotium using Eclipse ADT without source code of Android Application?

1. To get it started first let us see what is an apk file, why we need it? How to install it to a device?

2. Android application package file (APK) is the file format used to distribute and install application software and middleware onto Google’s Android operating system. APK files are ZIP file formatted packages based on the JAR file format, with .apk file extensions.

3. We need apk file to test our application without having the source code. Now the question is from where do I get the apk file? It’s Easy just go to workspace of eclipse -> open the folder called SimpleCalc -> bin -> SimpleCalc.apk (~279KB).

4. To install SimpleCalc.apk file we are going to use a simple adb (Android Debug Bridge) command. The syntax for install an apk file is as below:

Quote:

adb install [path of the apk file]

NOTE: Before Installing an apk file the important thing is to make sure that your device is in debugging mode and your device is connected to your Computer System. To enable debug mode in to your device go to Settings -> Developer options -> check mark Android debugging option.

4a. Basic Android Test Project without source code

1. Now as we are done with installing our apk file to device the next thing is very important, that is we have to create a new android test project so for this we have to keep at least one project open (It can be any project) and then only we can proceed else we will get an error saying which is show as below:

2. Now go to file -> new -> other -> Android Test Project -> give a name as SimpleCalcAPK-Test -> Select Test Target as “An existing Android project” -> Finish.

3. Now you will find a project with an empty package ready for creation of JUnit tests without source code.

4. Right click on the empty project -> new -> JUnit Test Case and give a name as ApkTestCase_1. Now you will see JUnit test case as below:

5. Now edit the above code as show below:

Quote:

package com.rhb.simplecalc.test.test;

import com.jayway.android.robotium.solo.Solo;

import android.test.ActivityInstrumentationTestCase2;

public class ApkTestCase_1 extends ActivityInstrumentationTestCase2 {

public ApkTestCase_1() {
super(MainActivity);
// TODO Auto-generated constructor stub
}

private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = “com.rhb.Simplecalc.MainActivity”;

private static Class MainActivity; // name of the activity class

static{
try {
MainActivity = Class
.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}

private Solo solo;

public void test() throws Exception{
solo.enterText(0, “12”);
solo.enterText(1, “2”);
solo.clickOnButton(0);
assertTrue(solo.waitForText(“24”));
}

protected void setUp() throws Exception {
super.setUp();
}

protected void tearDown() throws Exception {
super.tearDown();
}

}

6. As soon as you Run this test you will get an Error saying “Process crashed” as shown below:

7. Solution for this is we have to just change the below lines from AndroidMenifest.xml file :

HTML Code:
<instrumentation
        android:name="android.test.InstrumentationTestRunner"
        android:targetPackage="com.rhb.simplecalc.test" />

Change it to:

HTML Code:
 <instrumentation
        android:name="android.test.InstrumentationTestRunner"
        android:targetPackage="com.rhb.simplecalc" />

NOTE: The “targetPackage” will be the package called by the test which tests the functionality of the application in that called package.

8. Now copy paste the below code to test SimpleCalc application without source code:

Quote:

package com.rhb.simplecalc.test.test;

import com.jayway.android.robotium.solo.Solo;

import android.test.ActivityInstrumentationTestCase2;

public class ApkTestCase_1 extends ActivityInstrumentationTestCase2 {

public ApkTestCase_1() throws ClassNotFoundException {
super(MainActivity);
// TODO Auto-generated constructor stub
}

private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = “com.rhb.simplecalc.MainActivity”; // name of the package with activity name here com.rhb.simplecalc is the package name and MainActivity is the activity name to be executed.

private static Class MainActivity; // name of the activity class

static{
try {
MainActivity = Class
.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}

private Solo solo;

protected void setUp() throws Exception {
solo = new Solo(getInstrumentation(), getActivity());
}

public void test() throws Exception{

// solo.assertCurrentActivity(“wrong activiy”, MainActivity);
solo.enterText(0, “1”);
solo.enterText(1, “2”);
solo.clickOnButton(0);
assertTrue(solo.waitForText(“2”));
}

protected void tearDown() throws Exception {
super.tearDown();
}

}

10. Now Run the project by right clicking on the project -> RunAs -> Android Junit Test and see the test results in the Junit tab.

5. What is JUnit Test Suite and why we need it in Robotium?

1.JUnit Test Suite is a collection of multiple unit tests each of which shall test the functionality of a Robotium script.

2.Android sdk comes built in with android jUnit which is different from the normal jUnit however the structure of android jUnit resembles that of regular JUnit. This is the reason why we need JUnit suit to call all the Robotium scripts and thereby used in android automation.

3.One JUnit suite shall call multiple activities of the application and therefore for bigger applications. It is possible to have multiple JUnit suits and we can further have a collection of these suites under a suite of suits(collection of individual JUnit suits).

4.Now we will write a simple JUnit test for our application which is as below:

As I said before normal JUnit Suite is different and android JUnit is different we have to do some modifications on the coding part to make JUnit suite work on android, which is as below:

Quote:

package com.rhb.simplecalc.test.test;

import android.test.AndroidTestCase; // to make JUnitSuite work with android

public class JunitSuite extends AndroidTestCase {

@Override
public void setUp() throws Exception {
super.setUp();
}

// first JUnit Suite that will call ApkTestCase_1 class
public void Test() throws Exception {

ApkTestCase_1 a = new ApkTestCase_1();
a.setUp();
a.test();
a.tearDown();

}

@Override
public void tearDown() throws Exception {
super.tearDown();
}

}

5.Now I will show you how to create Main Suite which intern calls all the subsuites which are present in the test project.

Quote:

package com.rhb.simplecalc.test.test;

import junit.framework.Test;
import junit.framework.TestSuite;

public class MainSuite extends TestSuite {

public static Test suite () {
TestSuite suite = new TestSuite();

JunitSuite test = new JunitSuite();
test.setName(“Suite1”);
suite.addTest(test);

return suite;
}
}

6.Now as we are done with creating of JUnit Suite we will continue with creation of Data Driven testing in the next chapter.

6. How to Data-Drive Robotium Scripts?

1.We go for data driven testing when we have some modules that need to be tested for multiple values. For example in a application which has a login page with username and password field we have to test these edit text boxes for multiple inputs, it can be a number or it also can be a string or both together.

2.In our SimpleCalc application we have two edit text fields so we will see how to extract data from an Excel file and push the same to our application.

3.First we will create an excel file with some data present in it to test our application. (An Excel file for testing our app is attached here)

4.To extract data from Excel we use “FileInputStream” Class by which we can create/delete and modify a file.

5.Before we get it started some important things to be concentrated on are we have to keep the excel file inside the device and not on the PC because android Robotium doesn’t support the accessing of the files which are stored on to the computer system and not on the Device.

6.So first thing is to push the Excel file to the device. Just go to DDMS tab in eclipse and click on the push icon -> click on browse -> select the Test.xls file that I have provided. The screen shot for this is as shown below:

7.After pushing the Test.xls file to your device or Emulator the next thing what you have to do is add POI jar files to your build path. The link for these jar files can be found at first post.

8.Just create a new folder and name it as POI and add the Jar files to build path after adding them to build path it should look like below:

9.After successfully adding of the poi jar files its time to write program for accessing the data from excel file to our application.

10.The code for accessing data from excel file is as below:

Quote:

public void test() throws Exception, IOException,
FileNotFoundException,InvalidFormatException {

String cellval1;
String cellval2;

FileInputStream fis= new FileInputStream(Environment.getExternalStorageDire ctory() + “/Test.xls”);

Workbook wb = WorkbookFactory.create(fis); // creates object for workbook Test.xls

Sheet ws = wb.getSheet(“Sheet1”); // opens Sheet1 from workbook Test.xls

int rc = ws.getLastRowNum(); // counts the number of rows which are used

for(int i=1; i<=rc; i++)
{

Row r = ws.getRow(i); // ponts to the i’th ROW

for (int j=0;j==0;j++){

{
Cell c = r.getCell(j); // points to the j’th Column
cellval1=c.getStringCellValue(); // gets the data from J’th cell

Cell c1 = r.getCell(j+1); // ponts to the J+1’th column
cellval2=c1.getStringCellValue(); // gets the data from J+1’th cell

solo.clearEditText(0);
solo.enterText(0, cellval1); // pulls data from cellval1 and pushes it on to first editText box

solo.clearEditText(1);
solo.enterText(1, cellval2); // pulls data from cellval2 and pushes it on to second editText box

solo.clickOnButton(0);
}

}

}

}

11.Now when you run the code you will get the output as below:

12.Now as you can see from the above Screen Shot that the values from our excel sheet has been taken properly and after this the test should fail because we have given the second value as a special char and hence the test fails as shown below:

13.As we have finished with Data Driven Testing with Robotium we will now move with our last chapter where in we see what all issues I found while working with Robotium.

7. Issues with Robotium

1. Reporting capability is limited to JUnit Reports in XML format.

2. Writing into excel or text file is a bit clumsy at present.

3. Identifying Excel file for script parameters is manual in nature (Pushing Excel files to the device manually using DDMS).

4. Data whether text or number is taken as a string and fed to the script.

5. No Option of Record and Playback ( Now available 😛 ).

6. Robotium doesn’t work with TestNG suite and Eclipse Kepler at present.

7. Robotium is dependent on Android JUnit for execution control.

Leave a Comment