Android自动化测试之Robotium 用例分析

 如果大家配置好了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.51Ceshi.net/) 详细出处参考:http://www.51ceshi.cn/test/hangye/mobile/Android/5729.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值