The Robotium framework provides us with testing automation for the android platform. It supports testings both for native and for hybrid applications. Using the Robotium framework we can easily write our own automatic black-box test cases.

The following is the code of the activity I test in the video clip.

package com.lifemichael.samplecalc;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		final EditText text1 = (EditText) findViewById(R.id.editText1);
		final EditText text2 = (EditText) findViewById(R.id.editText2);
		final EditText text3 = (EditText) findViewById(R.id.editText3);
		Button btPlus = (Button) findViewById(R.id.button1);
		Button btMinus = (Button) findViewById(R.id.button2);
		btPlus.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				double num1 = Double.parseDouble(text1.getText().toString());
				double num2 = Double.parseDouble(text2.getText().toString());
				text3.setText(String.valueOf(sum(num1, num2)));

			}
		});
		btMinus.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				double num1 = Double.parseDouble(text1.getText().toString());
				double num2 = Double.parseDouble(text2.getText().toString());
				text3.setText(String.valueOf(difference(num1, num2)));

			}
		});

	}

	protected double sum(double num1, double num2) {
		return num1 + num2;
	}

	protected double difference(double num1, double num2) {
		return num1 - num2;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

The following is the code that tests the activity we developed.

package com.lifemichael.samplecalc.test;

import com.lifemichael.samplecalc.MainActivity;
import com.jayway.android.robotium.solo.Solo;
import com.lifemichael.samplecalc.R.*;
import android.app.Activity;
import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;
import android.widget.EditText;

public class MainActivityTest extends
		ActivityInstrumentationTestCase2<MainActivity> {

	private Solo solo;

	public MainActivityTest() {
		super(MainActivity.class);
	}
  
	public void testPlusOperation() throws Exception {
		Log.i("mytest","inside testPlusOperation");
		EditText firstEditText = (EditText) solo.getView(com.lifemichael.samplecalc.R.id.editText1);
		solo.enterText(firstEditText, "3");
		EditText secondEditText = (EditText) solo.getView(com.lifemichael.samplecalc.R.id.editText2);
		solo.enterText(secondEditText, "4");
		solo.clickOnButton("plus");
		EditText thirdEditText = (EditText) solo.getView(com.lifemichael.samplecalc.R.id.editText3);
		String result = thirdEditText.getText().toString();
		Log.i("mytest","result="+result);
		assertEquals(result,"7.0");
		
	}

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

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

You can find the complete sample including all files as well as slides and video clips for learning how to use this testing framework in my new Robotium Basics free course at http://abelski.lifemichael.com.

Leave a Reply

Your email address will not be published. Required fields are marked *