Testing Android Apps with Robotium and JBehave

The purpose of the article is to implement a Behavior Driver Development (BDD) infrastructure for Android applications with the JBehave open source BDD  tool  and Robotium open source Android testing software. We will go through complete architectural understandings and try to set-up the framework.

Author: Naveed Saleem

Introduction

Behavior Driven Development is easiest way to ensure that all stakeholders are on same page. The software tester is sure about testing the application designed exactly as expected by the product owner, the business analyst and developer. To create a BDD architecture to test Android applications, we will use Motile as AUT(application under test) for this purpose.

Before moving on, it is necessary to have a quick presentation of the tools that will be associated to build BDD framework.

Robotium is an open source test automation framework for Android native and hybrid applications. One can write functional, system and acceptance test scenarios, spanning multiple Android activities. It can be used both for testing applications where the source code is available and applications where only the apk file is available.

Robotium Remote Control helps to run Robotium test cases on the PC. It has the following modules:

  1. The Messenger is a SAFS TCP Communication service on Android. It allows us to provide remote control capabilities even for AUT and TestRunners
  2. The TestRunner is an InstrumentationTestRunner for Robotium Remote Control. Because the actual AUT-specific test code is on the remote Controller and NOT in the TestRunner, the TestRunner receives its remote command and control data and reports its success, failure, and results data through the Messenger service.

JUnit is an open source unit testing framework for the Java programming language. Using JUnit, we will put asserts and annotations to design our test project around Robotium.

JBehave is Behavior Driver Development framework that helps to implement stories based on test scenarios easily understandable by the product owner, the business analysts, testers and software developers. We can easily use the “Given”, “When”, “Then” and “And” keywords to define the scenarios.

Maven can be used to manage project builds and reporting using POM (Project Object Model).

Motile is a sample Android application designed to use known Android SDK controls. This simple application can be uses to write test cases around Robotium.

Building up understanding

We use the Robotium API to write test cases bundled with JUnit annotations and assertions. They will instrument the Android application (Motile) to simulate the user actions explained in the test cases, to verify assertions and to come up with a report. An example of this implementation can be found as testMotile.

Testing Android Apps with Robotium and JBehave

To implement BDD around the above architecture we will use JBehave at its core. However, before moving on it is necessary to describe the core architecture. This will help to understand the environmental limitations and find the way out. Android apps are built using Java but run on the DVM (Davlik Virtual Machine) which is similar to the JVM (Java Virtual Machine) but for Android. Robotium instruments the AUT (Application Under Test) and runs it in the DVM. JBehave works on the JVM and converts the text based user stories into JUnit based test cases. Thus it is not possible to run the JBehave based test cases directly with Robotium. To implement BDD for Android apps, we will use Robotium Remote Control that is built to run Robotium on the JVM.

Testing Android Apps with Robotium and JBehave

How to implement

We will write text-based stories containing steps to cover all test case scenarios and map steps to Java based test classes using JBehave stories configurations. The next step is to pass these test cases to Robotium Remote Controler that will take the responsibility to launch the AUT, execute test cases and come up with the test result report. To keep the things simple, we will implement a simple story, which will cover loading of main screen and verify the activity name of the Motile app on Android Phone.

Story:

Narrative:
If AUT loaded successfully
verify activity name is MainActivity

Meta:
@author <author name>

Scenario Motile main screen loading verification
Given screen loaded
When activity name is MainActivity
Then success
When activity name is not MainActivity
Then fail

Map Teststeps

This class contains the Java methods that are mapped to above story steps

public class testMotile {
private Solo solo;
@Given("screen loaded")
public void LoadScreen(String vActivityName) {
solo = getActivity();
}
/*
* verify that specific screen is
* loaded successfully
*/
@When("activity name is MainActivity $vActivityName")
public void verifyActivity(String vActivityName) {
solo.assertCurrentActivity("Activity not loaded", vActivityName);
}
/*
* Success on
* activity verification
*/
@Then("success")
public void success(Boolean status) {
Assert.assertTrue("Error loading activity", status);
}
}

Configuration

A class needed to link textual stories with above Java class

public class config extends JUnitStory {
@Override
public Configuration configuration() {
//To find stories
return new MostUsefulConfiguration().useStoryLoader(new LoadFromClasspath(this.getClass())).useStoryReporterBuilder(new
//Results Reporting settings
StoryReporterBuilder().withDefaultFormats().withFormats(Format.CONSOLE, Format.TXT));
}
// Link the test case
@Override
public InjectableStepsFactory stepsFactory() {
return new InstanceStepsFactory(configuration(), new testMotile());
}
}

Finalizing

Follow the Robotium installation instructions and skip the third part “Verify (Install if needed) Apache Ant:” as we will run our project using maven. Put the above files into the remote control project, collect all remote control jars (this is explained in the above installation link). Create the maven POM.xml file with all the necessary info and run the following command from command line. Don’t forget to connect the Android device with your computer through the USB Cable.

mvn install

This command will run the project by downloading the necessary jars, installing apks on device, launch Motile, execute the test cases and come up with a report.

Conclusion

In this article we tried to cover the followings points
● Understanding Behavior Driver Development
● Introduction of different components used to implement BDD for Android applications
● Analyzing the architectural limitations and dealing with them
● Implementing BBD from scratch

References

http://jbehave.org/
https://code.google.com/p/robotium
http://developer.android.com/index.html
http://maven.apache.org/
http://maven.apache.org/
http://en.wikipedia.org/wiki/Android_(operating_system)
http://en.wikipedia.org/wiki/Behavior-driven_development
http://dannorth.net/whats-in-a-story/

About the Author

Naveed Saleem is a Software Test Engineer with more than 5 years of experience in designing and implementation of Test planning, Test Cases execution, Test automation and Bug Reporting. Worked on test automation framework implementation on multi environmental corporate products using Continuous Integration. Tries to cover Functional, Performance, Usability and Security areas during testing. Twitter @naveedsaleem

1 Comment on Testing Android Apps with Robotium and JBehave

  1. Hi.. Naveed, Thanks for your article. It is great. I installed Robotium RC and jBehave, and tested both of them. They are working fine. But the last bit is unclear for me. Could you explain more about how to join them? If you have any sample projects, it would be perfect. Looking forward to hearing.. Thanks in advance.

1 Trackbacks & Pingbacks

  1. Software Development Linkopedia May 2013

Comments are closed.