Site Loader
Auckland, New Zealand
In this section we are going to discuss on automating the calculator application using Robotium, the calculator application we are going to automate is one we developed in our previous topic “Creating you first Calculator application in android” You can download the source code of the project from here Well we are all set with our AUT (Application Under Test), all we need is to start automating it. To automate our android application, please read the prerequisite article first Well, we have already seen how to automate application using Robotium (White-box testing), which you can check out from here When we talk about White-box testing, it’s like getting the property (controls identity) of AUT (Application Under Test) with its own source code. We can dive to the source code of actual application and see the Class R which is located under <project>\gen\<application package name>\R.java as shown For White-box testing we need to understand how android applications are actually developed and working. When it comes to black-box testing, not much coding is required, all the tester needs to do is to identify the object by its index and text value. You can see how to create Android test project and JUNIT test case class from the previous post Automating you Calculator application using Robotium(White-box Testing) Once you are done reading the previous post or you already know how to create a JUNIT test case class, let’s start working on creating black-box test for Calculator application. For doing black-box testing, check out the code below. I have written the code super simple. And since the scenario is very less. I am just trying to enter two text value in the number 1 and number 2 textbox verify the value. Then going to click the radiobutton and verify the textbox value. Here are few things I just wanted to let you all know. First, how to enter the text in the textbox using Index, here is how the code looks like         //Enter Number 1         solo.enterText(0, “12”); As you see above code, I have entered the value “12” to textbox whose index is 0. I have not passed the textbox by identifying its view Id or name, all I have did is passed its ID. Similarly I can enter the value for other textbox and get the value using getText() method as shown         //Get the result from its Index 2         String result = solo.getText(2).toString(); Thus the whole code looks like this
package com.example.calcapplication.test;

import com.example.calcapplication.MainActivity;
import com.jayway.android.robotium.solo.Solo;
import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;

/**********************************************************
 * 
 * @author Karthik_KK (www.executeautomation.com/blog)
 * @version 1.0
 * Description : CalcApplicationTest is the main class to launch Android tests.
 * 
 */
public class CalcApplicationTest extends
		ActivityInstrumentationTestCase2 {

	private String TAG = "CALCULATOR_TEST";
	private Solo solo;

	public CalcApplicationTest() throws ClassNotFoundException{
		super(MainActivity.class);
	}

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

	public void testDataEntry()
	{
		//Clear Text
		solo.clearEditText(0);
		//Enter Number 1
		solo.enterText(0, "12");

		//Clear Text
		solo.clearEditText(1);
		//Enter Number 2
		solo.enterText(1, "12");

		//check for the result
		Log.d(TAG, "Checking for the results");

		//Get the result from its Index 2
		String result = solo.getText(2).toString();
		if (result.equalsIgnoreCase("24"))
			Log.d(TAG,"ADD PASSED");
		else
			Log.d(TAG,"ADD FAILED");

		solo.clickOnRadioButton(1); //This will select Subtraction

		//Now get the result again
		result = solo.getText(2).toString();
		if (result.equalsIgnoreCase("24"))
			Log.d(TAG,"ADD PASSED");
		else
			Log.d(TAG,"ADD FAILED");
	}

}
I personally feel it’s good option to go with White-box based approach rather black-box, but to showcase the black-box testing option available with Robotium, I have included this post. Since there are lot of difficulties here in the above code as we operate fully on Index. You can download the working source code from here (Download) Please feel free to leave your comments and add new idea, which I will update in the post. Thanks, Karthik KK

Post Author: Karthik kk

6 Replies to “Automating your Calculator application using Robotium(Black-box Testing)”

  1. is there any way to get the id’s of the UI components wh9ile performing blackbox testing?? Its difficult to get the view of image button

Leave a Reply to Suman Cancel reply

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