Create Android Sample Application
after setting up
Our sample application would be a simple calculator to multiply two integer/decimal values. It will take two inputs and on clicking 'Multiply' it will show their multiply result.
For simplicity steps are categorized as,
1. Create Project
Click on
From
Note:
Click on Next and it will load
2. Create Test Project
Check
Note: As our current focus is to design sample project so for now, we will just create test project and in next section we will be working on this new created project to test AndroidCalculator.
After successfully creating projects, our Project explorer screen should look like,
Now two projects are created, we will simply work on first project to design our
3. Understand Project Architecture
Expand the
We would not get into details, as its not in our scope so far.
4. Design Layout
Get the code for main.xml and string.xml from
5. Design Application Logic
In Main.java enter following code and save it.Our application is designed and its time to run it.Right click on project select Run As and then select Android Application & and wait for while.It will load Android simulator, you need to wait for some time, it will launch application itself.
Our simple multiply calculator is ready, enter some integer/decimal values and click on Multiply, it will show the result above Click button.
Next:
Test Android Sample App with Robotium
1.
To test an Android application using Robotium, we need to create a test project with in the package
We will move on to design our logic to test AndroidCaculator. We need to create test case class where we will write code to test AndroidCalculator's main class (Main.java).
2. Create Test Case
In test project from project explorer window right click on
On
A new test case by the name of TestMain will be created into com.calculator.test package of my test project (AndroidCaculatorTest).
3. Add Robotium Jar
We need to reference the Robotium jar to our project.
Right click on project select
You can download Robotium jar from
4. Write Test Case Code
In our create test case we will access the contents of AndroidCalculator and do followings,
a.
b. Enter values of our own choice
c. Access & Click on Multiply button
d. Put assert to verify their multiplication result into result field.
And add following code into
package com.calculator.test;
import java.util.ArrayList;
import android.app.Activity;
import android.test.ActivityInstrumentationT
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 ActivityInstrumentationT
private Solo solo;
public TestMain() {
super("com.calculator", Main.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
solo = new Solo(getInstrumentation(), getActivity());
}
@Override
protected void tearDown() throws Exception{
try {
solo.finalize();
} catch (Throwable e) {
e.printStackTrace();
}
getActivity().finish();
super.tearDown();
}
public void testDisplayBlackBox() {
//Enter any integer/decimal value, we will enter 10 in first editfield
solo.enterText(0, "10");
//Enter any integer/decimal value, we will enter 20 in second editfield
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 (editfiled) and putting firstNumber value in it
EditText FirsteditText = (EditText) solo.getView(R.id.EditText01);
solo.enterText(FirsteditText, String.valueOf(firstNumber));
//Access Second value (editfiled) 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);
ArrayList currentTextViews = this.solo.getCurrentTextViews(outputField);
assertFalse(currentTextViews.isEmpty());
TextView output = (TextView) currentTextViews.get(0);
//Assert to verify result with visible value
assertEquals(String.valueOf(resutl), output.getText().toString());
}
}
5. Run Test Case
Now as we are almost done so now its time to run our test case.
Right click on
Select the emulator or device to run the test (we will be using Android default emulator) , and wait for a while to see the magic of Robotium.
If things are going fine
1.
2.
3.
4.
Download
Design Data Driven Framework around Robotium
In my last tutorial we tested
There are many Data Driven Frameworks available, but to save time I have tried to develop me own data driven framework. It may not mature enough but we can make it more intelligent by extending it.
1. Data Driven Testing Architecture
I am going to define my test scenarios into a csv (comma separated version) file and my script file will
* Read input from TestData.csv file
* Enter each 'First Value' column data in first edit field
* Enter each 'Second Value' column data in second edit field
* Click on Multiply field each time
* Verify Resultant value
To create a data driven development we will go through following steps
* Create Test Data
* Push Test Data into Emulator
* Import jar file
* Create Test Script (read data and assign values to AUT(application under test)
* Execute Test scenarios
* View Test Results
We will apply Data Driven testing approach on Android Simple Calculator. In which we enter two values and on clicking Multiply button it will show their resultant value. We will use
Note:Test Android apk file with Robotium
2. Create Test Data
We can create any combination of First and Second Value to test our Simple Calculator on Android. First Value represents the values for First Editfield and Second Value represents the Second Editfield in Android Simple Calculator. Execute column will allow the script to execute specific scenarios only.
3. Push Test Data into Emulator
Robotium will run our test script into emulator, for proper implementation of Data Driven Testing we need to make Test Data file accessible for emulator.
Load emulator, from eclipse Load DDMS interface. From Project Explorer tab navigate to the AUT package (our Android Simple Calculators package is
From top right section click on push icon and browse the TestData file from your computer.
It will show the pushed test data file into
4. Import JXL jar file
Our TestData file is in CSV format so we need to import necessary jar file to read contents, there may be multiple API available on Internet, I will be using JXL, it is available on following
After successfully download import that jxl.jar file into project build path.
5. Create Test Script
From
6. Run Test Script
Now its time to run our test script, From eclipse right click on project, select Run As and then click on Android JUnit Test. Wait for some time it will show the progress in console bar like below
System will load Android Simple Calculator app into emulator and start to read TestData file and enter First and Second Values into calculator and asserts resultant values. I have put (System.out.println) code to print results, you need to load LogCat interface and results will be visible there in logCat window like below,
After completing execution eclipse will show successful script execution like below.
7. View Results
After successful execution test script will create and write results into txt file and that file will be visible into Project Explorer of DDMS.
Click on pull icon top right to put that file and view it. Results of all test scenarios will be visible into TestResultData file.
Note:
Download