【Android自动化测试之Robotium】02. Robotium之用例分析


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

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


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

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


package 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<MainActivity>{
	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<View> 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");
		assertTrue(solo.waitForText("Item 9 clicked!"));
		solo.clickOnButton("OK");
		assertTrue(solo.waitForText("Botton OK in dialog with list is pressed!"));
	}
	
	public void testSetScreen() {
		solo.setActivityOrientation( Solo.LANDSCAPE);
		solo.setActivityOrientation(Solo.PORTRAIT);
		solo.sleep(2000);		
	}
	
	public void testGetStringById(){
		solo.clickOnButton("ScrollView");
		assertTrue(solo.getString(R.string.scroll_text).toString().contains("道可道"));
	}
	
	
	public void testRequestFocus(){		
		//好像没有直接获得焦点的方法,所以。。慢慢移动吧	
	}
	
	public void testSlide(){
		//gallery的操作方法没有
		//左右滑动用 solo.scrollToSide(Solo.LEFT);		
	}

	public void testGetChildCount(){
		Log.i(TAG, String.valueOf(solo.getViews().size())); //所有的VIEW数量
		Log.i(TAG, String.valueOf(solo.getViews(solo.getView(R.id.mainroot)).size()));//LinearLayout下的VIEW数量
		Log.i(TAG, String.valueOf(solo.getViews(solo.getView(R.id.my_gallery)).size()));//GALLERY下的VIEW数量
	}

}

GridView


package org.onceler.robotium.test;

import org.athrun.android.app.GridViewActivity;
import org.athrun.android.app.ListViewActivity;

import android.test.ActivityInstrumentationTestCase2;

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

public class GridViewActivityTest extends ActivityInstrumentationTestCase2<GridViewActivity>{
	private Solo solo;
	private static final String TAG = "robotium_test";
	public GridViewActivityTest() {
		super("org.athrun.android.app", GridViewActivity.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();
	}
	
	public void testGetChildByIndex(){
//		solo.clickOnText("Item.0");
		solo.clickOnImage(0);
		assertTrue(solo.waitForText("Item 0 is clicked!"));
		solo.assertCurrentActivity("current activity is not ListViewActivity", ListViewActivity.class);
	}
	
	public void testGetChildByIndexMixed(){
		solo.clickOnText("Item.19");
		assertTrue(solo.waitForText("Item 19 is clicked!"));
		solo.sleep(1000);
		//接下来没有办法直接获得哪一行的TEXT,ATHRUN就可以
		//所以只能通过INDEX来查找
		solo.getText(40); //这个有问题,被测应用的问题
		//移动到第20行可以像下面这样OH MY GOD,好慢
//		for(int i = 0;i<21;i++){
//			solo.sendKey(Solo.DOWN);
//		}
			
	}

}

【Android自动化测试之Robotium】02. Robotium之用例分析_第1张图片


你可能感兴趣的:(exception,android,ListView,测试,dialog)