Android2.1--robotium<2>--AndroidCalculator
AndroidCalculator 这是一个官网上的例子
1--首先介绍一下robotium 我的简单理解就是,可以模拟人对android设备的操作,点击,向文本框中输入内容之类的... ...
使用robotium,就是通过调用测试类Solo提供的一些方法,对android进行测试的
2--安装robotium
安装robotium,其实就是导入包含Solo类的jar包
下载地址:http://code.google.com/p/robotium/downloads/list,到上面下载一个适用于你androidAPI级别的版本
我用的是android2.1 ,下载的是robotium-solo-2.1.jar
下载完成后,不用解压,等着什么时候用,引入这个jar就完了
3--建立一个android工程AndroidCalculator 一个简单的计算器,只实现一种运算--乘法,
layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="@dimen/padding_medium" tools:context=".AndroidCalculatorActivity" /> <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/txtFirstNum"/> <EditText android:inputType="numberDecimal" android:id="@+id/EditText01" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <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/txtSecondNum"/> <EditText android:inputType="numberDecimal" android:id="@+id/EditText02" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <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"/> </LinearLayout>
string.xml
<resources> <string name="app_name">AndroidCalculator</string> <string name="hello_world">Hello world!</string> <string name="menu_settings">Settings</string> <string name="title_activity_android_calculator">AndroidCalculatorActivity</string> <string name="txtSpace"></string> <string name="txtSecondNum">Enter Second Numbe</string> <string name="txtFirstNum">Enter First Numbe</string> </resources>
AndroidCalculatorActivity.java
package com.calculator; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.text.Editable; import android.widget.TextView; import android.widget.EditText; import android.widget.Button; import android.view.View; public class AndroidCalculatorActivity 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.activity_android_calculator); FirstValue = (EditText)findViewById(R.id.EditText01); SecondValue = (EditText)findViewById(R.id.EditText02); Result = (TextView)findViewById(R.id.TextView01); Result.setTag("0.00"); Calculate = (Button)findViewById(R.id.Button01); Calculate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub showResult(FirstValue.getText(),SecondValue.getText()); } }); } //showing 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)); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_android_calculator, menu); return true; } }
4--建立一个测试工程 AndroidCalculatorTest
创建一个测试工程才是重点,File --> New --> Project --> Android Project --> Android Test Project
将AndroidCalculator选为Select Test Target
在AndroidCalculatorTest-->右键-->build path-->Configure Build Path…
然后点击Add External JARs…按钮,导入本机中的robotium-solo-2.1.jar文件,最后点击Order and Export ,把所有的选项都勾上
添加一个TestMain.java
import android.test.ActivityInstrumentationTestCase2;
import com.jayway.android.robotium.solo.Solo;
package com.calculator.test; import java.util.ArrayList; import android.test.ActivityInstrumentationTestCase2; import android.widget.EditText; import android.widget.TextView; import com.calculator.R; import com.jayway.android.robotium.solo.Solo; import com.calculator.AndroidCalculatorActivity; public class TestMain extends ActivityInstrumentationTestCase2<AndroidCalculatorActivity> { private Solo solo; public TestMain() { super("com.calculator",AndroidCalculatorActivity.class); } @Override protected void setUp() throws Exception { super.setUp(); solo = new Solo(getInstrumentation(), getActivity()); } public void testDisplayBlackBox() { solo.enterText(0, "10"); solo.enterText(1, "20"); solo.clickOnButton("Multiply"); assertTrue(solo.searchText("200")); } public void testDispalyWhiteBox() { float firstNumber = 10; float secondNumber = 20; float result = firstNumber*secondNumber; 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)); solo.clickOnButton("Multiply"); assertTrue(solo.searchText(String.valueOf(result))); TextView outputField = (TextView)solo.getView(R.id.TextView01); ArrayList currentTextViews = solo.getCurrentTextViews(outputField); assertFalse(currentTextViews.isEmpty()); TextView output = (TextView) currentTextViews.get(0); //Assert to verify result with visible value assertEquals(String.valueOf(result), output.getText().toString()); } @Override protected void tearDown() throws Exception { try { solo.finalize(); } catch(Throwable e) { e.printStackTrace(); } getActivity().finish(); super.tearDown(); } }