数据驱动测试 Robotium

Create Android Sample Application

after setting up Android working environment we will start designing our own sample application, which we will test using Robotium in next section.

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 File menu, select New and click on the Others, from New window, Drag down to Android option, expand it, and select Android Project and Click on Next.

数据驱动测试 <wbr>Robotium 
From New Android Project Window, enter Project Name as 'AndroidCalculator', FromContents section select 'Create New Project in workspace', Check Android 2.2 fromBuild Target section, Application Name 'AndroidCalculator', Package Name'com.calculator', Create Activity 'Main' and '8' as Min SDK Version 

Note: you can enter any other options best suits to your need.

Click on Next and it will load New Android Project window eclipse offer you to create project to test your project, at this time we will avail it to test 'AndroidCalculator' we made in last step. We can skip this part by un-checking Create a Test Project option, if you want to manually create test project later, we will create test project right away.

2. Create Test Project
Check Create a Test Project and it will automatically fill rest of fields based on the last made project (AndroidCalculator), and click on Finish.

数据驱动测试 <wbr>Robotium 
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,

数据驱动测试 <wbr>Robotium

Now two projects are created, we will simply work on first project to design our sample calculator application.

3. Understand Project Architecture
Expand the src directory then expand com.calculator directory, Main.java file contains application logic. In rec directory we can define application's UI interface. In Main.xmlwe can put controls on application interface and in string.xml we can define their string values,which would be visible on UI.

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 Android Calculator Code

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.

数据驱动测试 <wbr>Robotium

Next: In next step we will work on AndroidCaculatorTest project to test that simple calculator. Download Android Calculator Code

Test Android Sample App with Robotium

1. Create Test Project
To test an Android application using Robotium, we need to create a test project with in the package (com.calculator) of specific project. We have already created our test project in last section, hope you got viewing below screen,

数据驱动测试 <wbr>Robotium

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 com.calculator.test selectNew then others. On New window expand Java and then expand Junit category and select Junit Test Case and click on Next.

数据驱动测试 <wbr>Robotium 
On New Junit Test Case screen, most of the options will be automatically filled as we have already created test project (AndroidCalculatorTest) with project (AndroidCalculator). We need to enter the Name of Test case, which I will enter TestMain, as I am going to test (main.java) of AndroidCalculator project. On next section check Setup(), tearDown() & Constructor options and click on Finish.

数据驱动测试 <wbr>Robotium 
A new test case by the name of TestMain will be created into com.calculator.test package of my test project (AndroidCaculatorTest).

数据驱动测试 <wbr>Robotium 
3. Add Robotium Jar
We need to reference the Robotium jar to our project.

Right click on project select Build Path, and then click on Configure Build Path option. On Properties window click on Libraries tab and add Robotium jar into project.

You can download Robotium jar from http://code.google.com/p/robotium/downloads/list

数据驱动测试 <wbr>Robotium 
4. Write Test Case Code
In our create test case we will access the contents of AndroidCalculator and do followings,

a. Call/Access first & second input controls (EditFields)
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 TestMain.java class and save it.

 

package com.calculator.test;

import java.util.ArrayList;

import android.app.Activity;

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 {

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 TestMain.java file select Run As option and then click on Android JunitTest. It will start running Junit test.

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. Emulator will load, Unlock it.

2. AndroidCalculator application will load

3. It will automatically enter first & second values in First and Second EditField, and click on Multiply button (you can see all this happening as record & play scripts)

4. After successfully execution it will show green bar showing the successful execution and all results are passed.

Download AndroidCalculatorTest project code. In next section we will test android apk file with Robotium.

Design Data Driven Framework around Robotium

In my last tutorial we tested Test Android apk file with Robotium. Simple Android Calculator’s apk was used with some specific input values. Now in this tutorial we are extending testing to Data Driven Testing approach under Robotium. Many automate testers may have worked on Data Driven Testing Framework for web sites or Desktop applications.

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

数据驱动测试 <wbr>Robotium


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
 Test Android apk file with Robotium project for Data Driven Testing implementation.

Note:Test Android apk file with Robotium
 tutorial first.

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.

数据驱动测试 <wbr>Robotium

 

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 com.calculator) and expand it to files category. It will be empty, we will push our Test Data file into filesdirectory and then our script will be able to read TestData contents with emulator.

数据驱动测试 <wbr>Robotium


From top right section click on push icon and browse the TestData file from your computer.

数据驱动测试 <wbr>Robotium

It will show the pushed test data file into files directory like below

数据驱动测试 <wbr>Robotium

 

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 link.

After successfully download import that jxl.jar file into project build path.

数据驱动测试 <wbr>Robotium

 

5. Create Test Script

From Test Android apk file with Robotium project open the TestCalculatorApk.java file and replace the existing code with one (same file name) in shared project code.

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

数据驱动测试 <wbr>Robotium 
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,

数据驱动测试 <wbr>Robotium After completing execution eclipse will show successful script execution like below.

数据驱动测试 <wbr>Robotium

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.

数据驱动测试 <wbr>Robotium 
Click on pull icon top right to put that file and view it. Results of all test scenarios will be visible into TestResultData file.

数据驱动测试 <wbr>Robotium


Note:
 This is the basic Data Driven Testing framework around Robotium. There are many things which can be implemented in better way. I have tried to develop it in simplest way. Its up to the test engineer to use it in his/her own way. We can make it as much intelligent as we need.

Download sample project code with TestData file

转载地址:http://blog.sina.com.cn/s/blog_7277cc970100qmgc.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值