Post Reply

[DEV] [TUTORIAL] Auto Testing of Apps

OP EatHeat

24th May 2013, 06:53 PM   |  #1  
EatHeat's Avatar
OP Recognized Developer
Flag Calcutta
Thanks Meter: 1,419
 
913 posts
Join Date:Joined: Jan 2013
More
Disclaimer
  • I am not the developer of this tool. I am just sharing how to test your apps' functionality via Robotium.
  • This tutorial assumes that you have the SDK and Eclipse all set up perfectly. Setting these up will not be covered in this tutorial.


---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


Setting up a sample project:


Although source codes are available, it is recommended to do this manually.


  1. Create a new android project by going to File> New> Android Application Project.
  2. Fill in the following information until you have the project set up.









So your project is now set up. Its a simple calculator just for testing.


Type this in your main.xml;


Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />

<TextView 
	android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/txtSpace"
/>

<TextView 
	android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/txtFirstNumber"
/>

<EditText 
	android:inputType="numberDecimal"
	android:id="@+id/EditText01" 
	android:layout_width="fill_parent" 
	android:layout_height="wrap_content">
</EditText>

<TextView 
	android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/txtSpace"
/>

<TextView 
	android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/txtSecondNumber"
/>

<EditText 
	android:inputType="numberDecimal"
	android:id="@+id/EditText02" 
	android:layout_width="fill_parent" 
	android:layout_height="wrap_content">
</EditText>

<TextView 
	android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/txtSpace"
/>

<TextView 
	android:id="@+id/TextView01" 
	android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
/>

<TextView 
	android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/txtSpace"
/>

<Button 
	android:text="Multiply"
	android:id="@+id/Button01" 
	android:layout_width="fill_parent" 
	android:layout_height="wrap_content">
</Button>

</LinearLayout>


This in your strings.xml:




Code:
<?xml version="1.0" encoding="utf-8"?>

<resources>

    <string name="hello">Enter two values and click on Calculate to multiply them.</string>
    <string name="app_name">AndroidCalculator</string>
    <string name="txtFirstNumber">Enter First Number</string>
    <string name="txtSecondNumber">Enter Second Number</string>
    <string name="txtSpace"></string>
</resources>

This in your Main.java :


Code:
package com.calculator;

import com.calculator.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.text.Editable;

public class Main extends Activity {
	EditText FirstValue;
	EditText SecondValue;
	TextView Result;
	Button Calculate;
	float num1 , num2;	
    
     @override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        FirstValue = (EditText) findViewById(R.id.EditText01);
        
        SecondValue = (EditText) findViewById(R.id.EditText02);
        
        Result = (TextView) findViewById(R.id.TextView01);
        Result.setText("0.00");
        
        Calculate = (Button) findViewById(R.id.Button01);

        //Adding listener to button
        Calculate.setOnClickListener(new View.OnClickListener() {

    	public void onClick(View v) {
    		//Getting first & second values and passing to show result
    		showResult(FirstValue.getText(), SecondValue.getText());
    	}
   	});
 }

 //Showing multiply results
 protected void showResult(Editable first, Editable second) 
 {
	 float num1 = Float.parseFloat(first.toString());
	 float num2 = Float.parseFloat(second.toString());
	 float result = num1 * num2;
	 Result.setText(String.valueOf(result));
 }
 
}
Your basic calculator app is now ready. This is just for testing purposes so we will not be focusing on the layout too much.

Launch your app in emulator. It will look like this :




Now we will create a test android project.

Go to File> New> Project and follow like its given below :







This is also done.

Now right click on the Test project and go to New> JUnit Test Case and setup the following:



A new java file will now bee created.

Now right click on the Test Project again and go to Build Path> Configure Build Path and setup the following again:



Here Click Add External Jars and browse to the Robotium jar file. You can download it here. Choose the latest one to download.

Note: Not javadoc.jar Only Jar file.




Go to Order and Export and check the jar file and click OK.



Now type this in the TestMain.java :

Code:
package com.calculator.test;

import android.test.ActivityInstrumentationTestCase2;
import android.widget.EditText;
import android.widget.TextView;
import com.calculator.Main;
import com.calculator.R;
import com.jayway.android.robotium.solo.Solo;

public class TestMain extends ActivityInstrumentationTestCase2<Main> {
	private Solo solo;
	
	public TestMain() {
		super(Main.class);
	}
	
  @override
	protected void setUp() throws Exception {
		super.setUp();
		solo = new Solo(getInstrumentation(), getActivity());
	}
	
	public void testDisplayBlackBox() {

		//Enter 10 in first edit-field
		solo.enterText(0, "10");

		//Enter 20 in first edit-field
		solo.enterText(1, "20");

		//Click on Multiply button
		solo.clickOnButton("Multiply");

		//Verify that resultant of 10 x 20 
		assertTrue(solo.searchText("200"));

		}
	
	public void testDisplayWhiteBox() {

		//Defining our own values to multiply
		float firstNumber = 10;
		float secondNumber = 20;
		float resutl = firstNumber * secondNumber ;
		
		//Access First value (edit-filed) and putting firstNumber value in it
		EditText FirsteditText = (EditText) solo.getView(R.id.EditText01);
		solo.enterText(FirsteditText, String.valueOf(firstNumber));
		
		//Access Second value (edit-filed) and putting SecondNumber value in it
		EditText SecondeditText = (EditText) solo.getView(R.id.EditText02);
		solo.enterText(SecondeditText, String.valueOf(secondNumber));
		
		//Click on Multiply button
		solo.clickOnButton("Multiply");
		
		assertTrue(solo.searchText(String.valueOf(resutl)));				
		TextView outputField = (TextView) solo.getView(R.id.TextView01);		
		//Assert to verify result with visible value
		assertEquals(String.valueOf(resutl), outputField.getText().toString());
	}
	
  @override
	protected void tearDown() throws Exception{

			solo.finishOpenedActivities();
	}
}
That's it. Your test project is also done.

Now right click on the TestMain.java in Eclipse and run it as an Android JUnit Test Case:



Project will now load up in the emulator and auto testing will begin as provided in TestMain.java:



Finally your results will be shown in the JUnit panel. Which shouldn't have any errors of course.




That's it! You have just tested your app without even really testing it!!
Last edited by EatHeat; 25th May 2013 at 02:24 AM.
The Following 15 Users Say Thank You to EatHeat For This Useful Post: [ View ]
24th May 2013, 06:54 PM   |  #2  
EatHeat's Avatar
OP Recognized Developer
Flag Calcutta
Thanks Meter: 1,419
 
913 posts
Join Date:Joined: Jan 2013
More

Using Robotium to test apk file

This tutorial is for testing an apk file if you dont have the source.

Follow this first if app is not yours and is signed by someone else.

1. Open the apk file with 7zip or any other alternative.

2. Delete the Meta-inf folder.

3. Resign the apk again in debug mode.

If the app is yours.

Export the app in any location signed in debug mode.

Install the apk onto the emulator.



Now again create a test project like in the 1st post.



Make sure you click the radio button for ''This Project''.



From now, its similar to the first post.

First right click on the package and go to New> Package and give the package as com.testcalculator .

Create a new Class by right clicking on the Test Project and name it as TestApk.

Again add the Robotium jar file that you downloaded earlier. Follow first post on how to do it.

Finally right-click the project and run it as Android JUnit Test.

That's it. Emulator will load it and test it the same way as in Post 1.

Note:

1. Make sure your Manifest is correct because most probably Eclipse will make the package as com.calculator.test . You can keep it the same way but to differentiate it from the 1st post, we will keep it as com.testcalculator .

2. Whatever your package name is, make sure to keep it correct in the java class in src folder.

3. You can keep the java file same as in post 1 keeping in mind the above point.
Last edited by EatHeat; 26th May 2013 at 03:31 PM.
The Following 8 Users Say Thank You to EatHeat For This Useful Post: [ View ]
24th May 2013, 06:55 PM   |  #3  
EatHeat's Avatar
OP Recognized Developer
Flag Calcutta
Thanks Meter: 1,419
 
913 posts
Join Date:Joined: Jan 2013
More
Featured on the Portal here:

http://www.xda-developers.com/androi...-with-robotium

Again here:

http://www.xda-developers.com/androi...without-source

And on XDA TV here:

http://www.xda-developers.com/androi...a-developer-tv
Last edited by EatHeat; 29th May 2013 at 10:52 AM.
The Following 3 Users Say Thank You to EatHeat For This Useful Post: [ View ]
24th May 2013, 06:55 PM   |  #4  
EatHeat's Avatar
OP Recognized Developer
Flag Calcutta
Thanks Meter: 1,419
 
913 posts
Join Date:Joined: Jan 2013
More
Robotium

Quote:

Robotium is an Android test automation framework
that has full support for native and hybrid
applications. Robotium makes it easy to write
powerful and robust automatic black-box test
cases. With the support of Robotium, test case
developers can write function, system and acceptance test scenarios, spanning multiple
Android activities.

Sources And Links

Robotium Project Home.

Download Page.

Tutorials Page.

Sources

The source code of the sample apps can be downloaded through the Tutorials link given.
Last edited by EatHeat; 25th May 2013 at 06:24 AM.
The Following 3 Users Say Thank You to EatHeat For This Useful Post: [ View ]
Post Reply Subscribe to Thread
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes