Android自动化测试之Robotium<二>:用例分析

发表于:2012-08-28来源:Csdn作者:wirelessqa点击数: 标签:Android
如果大家配置好了Robotium的测试环境,那么开始吧(这里只介绍有源码的情况,没有源码也是可以做的) 被测应用下载:http://download.csdn.net/detail/wirelessqa/4487252

  如果大家配置好了Robotium的测试环境,那么开始吧(这里只介绍有源码的情况,没有源码也是可以做的)

  被测应用下载:http://download.csdn.net/detail/wirelessqa/4487252

  测试代码 (只贴出MainActivity和GridView,用例很简单就不多作注释了)

  大家可以对照着Athrun的用例来看看两个框架的区别

  [java] view plaincopypackage org.onceler.robotium.test;

  import java.util.ArrayList;

  import org.athrun.android.app.MainActivity;

  import org.athrun.android.app.R;

  import org.athrun.android.app.ScrollActivity;

  import android.test.ActivityInstrumentationTestCase2;

  import android.test.suitebuilder.annotation.Smoke;

  import android.util.Log;

  import android.view.KeyEvent;

  import android.view.View;

  import android.widget.TextView;

  import com.jayway.android.robotium.solo.Solo;

  public class MainActivityTest extends ActivityInstrumentationTestCase2{

  private Solo solo;

  private TextView tv;

  private static final String TAG = "robotium_test";

  public MainActivityTest() {

  super("org.athrun.android.app", MainActivity.class);

  }

  public void setUp() throws Exception {

  solo = new Solo(getInstrumentation(), getActivity());

  }

  @Override

  public void tearDown() throws Exception {

  //Robotium will finish all the activities that have been opened

  solo.finishOpenedActivities();

  }

  @Smoke

  public void testWaitForActivity(){

  solo.assertCurrentActivity("assert activity err", MainActivity.class);

  solo.assertCurrentActivity("assert activity err", "MainActivity");

  solo.assertCurrentActivity("assert activity err", MainActivity.class, true);

  solo.assertCurrentActivity("assert activity err", "MainActivity", true);

  solo.clickOnButton("ScrollView");

  solo.assertCurrentActivity("assert activity err", ScrollActivity.class);

  solo.goBack();

  solo.clickOnButton(4);

  solo.assertCurrentActivity("assert activity err", "ScrollActivity");

  solo.goBackToActivity("MainActivity");

  solo.assertCurrentActivity("assert activity err", "MainActivity", true);

  }

  @Smoke

  public void testFindElementInTree(){

  ArrayList list = solo.getViews(solo.getView(R.id.include_checkbox));

  tv = (TextView)list.get(2);

  assertEquals("default text",tv.getText());

  solo.clickOnCheckBox(0);

  tv = (TextView)list.get(2);

  assertEquals("CheckBox is checked!",tv.getText());

  }

  public void testFindViewByIdDirect() {

  //通过ID得到view

  tv = (TextView)solo.getView(R.id.textview);

  assertEquals("default text",tv.getText());

  solo.clickOnCheckBox(0);

  tv = (TextView)solo.getView(R.id.textview);

  assertEquals("CheckBox is checked!",tv.getText());

  }

  public void testLongClick(){

  //ImageView iv = (ImageView)solo.getView(R.id.my_imageview);

  solo.clickLongOnView(solo.getView(R.id.my_imageview));

  //这个TOAST提示捕获不到,不知道哪里有问题,知道的朋友可以跟我说一下

  // assertEquals(true,solo.waitForText("LongClick"));

  }

  public void testPressMenu(){

  solo.clickOnMenuItem("Toast");

  //这个TOAST跟上面的一样,这个确是成功的。OH MY GOD,这是为何

  assertEquals(true,solo.waitForText("Hello World"));

  }

  public void testPressHome(){

  solo.sendKey(KeyEvent.KEYCODE_HOME);

  solo.sleep(2000);

  }

  public void testPressBack(){

  solo.clickOnButton("ScrollView");

  solo.clickOnButton("Bottom Button");

  solo.sendKey(KeyEvent.KEYCODE_BACK);

  solo.assertCurrentActivity("assert activity err", "MainActivity");

  }

  public void testFindViewByText(){

  solo.clickOnButton("ListView");

  solo.clickLongOnText("Item35");

  solo.clickOnText("Item One");

  assertTrue(solo.waitForText("1 pressed!"));

  }

  public void testScrollListInDialog(){

  solo.clickOnMenuItem("Dialog With List");

  solo.clickOnText("Item9");

原文转自:http://www.ltesting.net