您的位置:首页 > 移动开发 > Android开发

【Android自动化测试】03. Athrun之用例分析

2012-08-09 14:29 197 查看


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

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

1. 导入被测试应用-- 新建测试工程-- 依赖被测试应用

测试工程的Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.athrun.android.test"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <instrumentation
        android:name="pl.polidea.instrumentation.PolideaInstrumentationTestRunner"
        android:targetPackage="org.athrun.android.app" />

    <application
        android:icon="@drawable/testicon"
        android:label="@string/app_name" >
        <uses-library android:name="android.test.runner" />
    </application>

</manifest>


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

package org.athrun.android.test;

import org.athrun.android.app.R;
import org.athrun.android.framework.AthrunTestCase;
import org.athrun.android.framework.Test;
import org.athrun.android.framework.ViewOperation;
import org.athrun.android.framework.viewelement.AbsListViewElement;
import org.athrun.android.framework.viewelement.SlideableElement;
import org.athrun.android.framework.viewelement.TextViewElement;
import org.athrun.android.framework.viewelement.ViewElement;
import org.athrun.android.framework.viewelement.ViewGroupElement;

import android.util.Log;
import android.widget.AbsListView;

//继承AthrunTestCase
public class MainActivityTest extends AthrunTestCase {
	private static final String LOG_TAG = "MainActivityTest";

	public MainActivityTest() throws Exception {
		//super(被测试程序包名,启动的activity)
		super("org.athrun.android.app", "org.athrun.android.app.MainActivity");
		//设置查找VIEW最多用多少时间
		AthrunTestCase.setMaxTimeToFindView(10000);
	}

	@Test
	//等待activity
	public void testWaitForActivity() throws Exception {
		// log("This is a test for log() method");
		assertEquals(true, getDevice().waitForActivity("MainActivity", 5000));
		assertEquals(false, getDevice().waitForActivity("ScrollActivity", 5000));
		findElementByText("ScrollView").doClick();
		assertEquals(true, getDevice().waitForActivity("ScrollActivity", 5000));
		assertEquals(false, getDevice().waitForActivity("MainActivity", 5000));
		getDevice().pressBack();
		assertEquals(true, getDevice().waitForActivity("MainActivity", 5000));
		assertEquals(false, getDevice().waitForActivity("ScrollActivity", 5000));
	}

	@Test
	//通过ID一级一级的查找元素 
	public void testFindElementInTree() throws Exception {
		ViewGroupElement include = findElementById("include_checkbox",
				ViewGroupElement.class);
		include.findElementById("my_checkbox", ViewElement.class).doClick();
		TextViewElement tmtsTextView = include.findElementById("textview",
				TextViewElement.class);
		assertEquals("CheckBox is checked!", tmtsTextView.getText());
	}

	@Test
	//直接查找
	public void testFindViewByIdDirect() throws Exception {
		findElementById("my_checkbox").doClick();
		assertEquals("CheckBox is checked!",
				findElementById("textview", TextViewElement.class).getText());
	}

	@Test
	//长按
	public void testLongClick() throws Exception {
		findElementById("my_imageview", ViewElement.class).doLongClick();
		assertEquals(true, waitForText("LongClick", 2000));
	}

	@Test
	public void testPressMenu() throws Exception {
		getDevice().pressMenu();
		findElementByText("Toast").doClick();
		assertEquals(true, waitForText("Hello World", 2000));
	}

	@Test
	public void testPressHome() throws InterruptedException {
		getDevice().pressHome();
		Thread.sleep(2000);
	}

	@Test
	public void testPressBack() throws Exception {
		findElementById("btn_scrollview_activity").doClick();
		findElementByText("Bottom Button").doClick();
		getDevice().pressBack();
		assertEquals(true, getDevice().waitForActivity("MainActivity", 2000));
	}

	@Test
	public void testFindViewByText() throws Exception {
		findElementByText("ListView").doClick();
		AbsListViewElement listView = findElementById("my_listview",
				AbsListViewElement.class);
		listView.scrollToNextScreen();
		ViewElement tmtsView = listView.getChildByIndex(35, ViewElement.class);
		tmtsView.doLongClick();
		findElementByText("Item One").doClick();
		assertEquals(true, waitForText("1 pressed!", 2000));
	}

	@Test
	public void testScrollListInDialog() throws Exception {
		getDevice().pressMenu();
		findElementByText("Dialog With List").doClick();
		AbsListViewElement listView = findElementByIndex(0, AbsListView.class,
				AbsListViewElement.class);
		listView.scrollToLine(9);
		assertEquals(9, listView.getLastVisiblePosition());
		findElementByText("OK").doClick();
		assertEquals(true,
				waitForText("Botton OK in dialog with list is pressed!", 2000));
	}

	@Test
	public void testSetScreen() throws InterruptedException {
		getDevice().setScreenOrientation(
				android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		assertEquals("landscape", getDevice().getScreenOrientation());
	}

	@Test
	public void testGetStringById() throws Exception {
		findElementByText("ScrollView").doClick();
		assertEquals(true, getStringById("scroll_text").contains("道可道"));
		// log(getStringById("scroll_text"));
	}

	@Test
	public void testFindToastById() throws Exception {
		findElementById("my_imageview", ViewElement.class).doLongClick();
		assertEquals(true, waitForText("LongClick", 2000));
	}

	@Test
	public void testRequestFocus() throws Exception {
		findElementByText("ScrollView").requestFocus();
		getDevice().pressDown();
		getDevice().pressUp();
		getDevice().pressEnter();
		assertEquals(true, getDevice().waitForActivity("ScrollActivity", 2000));
	}

	@Test
	public void testPressEnter() throws Exception {
		findElementByText("ListView").requestFocus();
		getDevice().pressDown();
		getDevice().pressEnter();
		getDevice().pressEnter();
		Thread.sleep(2000);
	}

	@Test
	public void testFindViewByIntId() throws Exception {
		findElementById(R.id.btn_scrollview_activity, ViewElement.class)
				.doClick();
		assertEquals(true, getDevice().waitForActivity("ScrollActivity", 5000));
	}

	@Test
	public void testDevicePressKey() throws Exception {
		findElementByText("ScrollView").requestFocus();
		getDevice().pressEnter();
		assertEquals(true, getDevice().waitForActivity("ScrollActivity", 1000));
		getDevice().pressBack();
		assertEquals(true, getDevice().waitForActivity("MainActivity", 1000));
	}

	@Test
	public void testFindViewOutOfScreen() throws Exception {
		findElementById("btn_scrollview_activity").doClick();
		TextViewElement tb = findElementByText("Top Button");
		// log("x is " + tb.getLocation().getX());
		// log("y is " + tb.getLocation().getY());
		tb.doClick();
		Thread.sleep(3000);
		// log("x is " + tb.getLocation().getX());
		// log("y is " + tb.getLocation().getY());
		findElementByText("Top Button").doClick();
		Thread.sleep(3000);
	}

	@Test
	public void testFindViewOutOfScreen2() throws Exception {
		ViewGroupElement rootGroup = findElementById("mainroot",
				ViewGroupElement.class);
		rootGroup.findElementById("btn_scrollview_activity", ViewElement.class)
				.doClick();
	}

	@Test
	public void testFindViewOutOfScreen3() throws Exception {
		ViewGroupElement rootGroup = findElementById("mainroot",
				ViewGroupElement.class);
		rootGroup.getChildByIndex(4, ViewElement.class).doClick();
	}

	@Test
	public void testSlide() throws Exception {
		SlideableElement gallery = findElementById("my_gallery",
				SlideableElement.class);

		for (int i = 0; i < 10; i++) {
			gallery.slide(ViewOperation.Direction.LEFT);
		}
	}
	
	public void testGetChildCount() throws Exception {
		ViewGroupElement root = findElementById("mainroot", ViewGroupElement.class);
		Log.i(LOG_TAG, String.valueOf(root.getDirectChildCount()));
	}
}

GridView

package org.athrun.android.test;

import org.athrun.android.framework.AthrunTestCase;
import org.athrun.android.framework.Test;
import org.athrun.android.framework.viewelement.AbsListViewElement;
import org.athrun.android.framework.viewelement.TextViewElement;
import org.athrun.android.framework.viewelement.ViewGroupElement;

public class GridViewActivityTest extends AthrunTestCase {
	
	public GridViewActivityTest() throws Exception {
		super("org.athrun.android.app", "org.athrun.android.app.MainActivity");
	}

	@Test
	public void testGetChildByIndex() throws Exception {
		assertEquals(true, getDevice()
				.waitForActivity("MainActivity", 2000));
		findElementById("btn_gridview_activity").doClick();
		AbsListViewElement gridView = findElementById("my_gridview",
				AbsListViewElement.class);
		assertNotNull(gridView);
		ViewGroupElement item = gridView.getChildByIndex(0, ViewGroupElement.class);
		assertNotNull(item);
		TextViewElement view = item.getChildByIndex(1, TextViewElement.class);
		assertEquals("Item.0", view.getText());
		item.doClick();
		assertEquals(true, waitForText("Item 0 is clicked!", 2000));
		assertEquals(true,
				getDevice().waitForActivity("ListViewActivity", 2000));
		getDevice().pressBack();
		assertEquals(true,
				getDevice().waitForActivity("GridViewActivity", 2000));
	}

	@Test
	public void testGetChildByIndexMixed() throws Exception {
		findElementById("btn_gridview_activity").doClick();
		AbsListViewElement gridView = findElementById("my_gridview",
				AbsListViewElement.class);
		ViewGroupElement item = gridView.getChildByIndex(19, ViewGroupElement.class);
		assertNotNull(item);
		assertEquals(2, item.getChildCount());
		assertEquals("Item.19", item.getChildByIndex(1, TextViewElement.class)
				.getText());
		item.doClick();
		assertEquals(true, waitForText("Item 19 is clicked!", 2000));
		AbsListViewElement listView = findElementById("my_listview",
				AbsListViewElement.class);
		assertNotNull(listView);
		ViewGroupElement group = listView.getChildByIndex(20, ViewGroupElement.class);
		assertNotNull(group);
		assertEquals("Item20Thank you!", group.fetchText());
		TextViewElement groupChild = group.getChildByIndex(1, TextViewElement.class);
		assertEquals("Item20", groupChild.getText());
		group.doLongClick();
		findElementByText("Item One").doClick();
		assertEquals(true, waitForText("1 pressed!", 2000));
	}

	@Test
	public void testFindMixed() throws Exception {
		findElementByText("ListView").doClick();
		AbsListViewElement listView = findElementById("my_listview",
				AbsListViewElement.class);
		ViewGroupElement group = listView.getChildByIndex(20, ViewGroupElement.class);
		TextViewElement textView = group.getChildByIndex(1, TextViewElement.class);
		assertEquals("Item20", textView.getText());
		TextViewElement textView2 = findElementByText("Item20");
		assertEquals("Item20", textView2.getText());
		group.doLongClick();
		findElementByText("Item One").doClick();
		assertEquals(true, waitForText("1 pressed!", 2000));
	}
	
	@Test
	public void testScrollListInDialog() throws Exception {
		getDevice().pressMenu();
		findElementByText("Dialog With List").doClick();
		AbsListViewElement listView = findElementById("my_listview", AbsListViewElement.class);
		listView.scrollToLine(9);
		assertEquals(9, listView.getLastVisiblePosition());
		findElementByText("OK").doClick();
		assertEquals(true,
				waitForText("Botton OK in dialog with list is pressed!", 2000));
	}

}


ScrollView

package org.athrun.android.test;

import org.athrun.android.framework.AthrunTestCase;
import org.athrun.android.framework.Test;
import org.athrun.android.framework.viewelement.ScrollViewElement;

import android.view.View;
import android.widget.ScrollView;

/**
 * TestCases for ScrollViewElement
 */
public class ScrollViewActivityTest extends AthrunTestCase {
	private static final String LOG_TAG = "ScrollViewActivityTest";

	public ScrollViewActivityTest() throws Exception {
		super("org.athrun.android.app", "org.athrun.android.app.MainActivity");
	}

	@Test
	public void testFullScroll() throws Exception {
		findElementById("btn_scrollview_activity").doClick();
		ScrollViewElement tmtsScrollView = findElementById("ScrollView",
				ScrollViewElement.class);
		tmtsScrollView.fullScroll(View.FOCUS_DOWN);
	}

	@Test
	public void testScrollTo() throws Exception {
		findElementById("btn_scrollview_activity").doClick();
		ScrollViewElement tmtsScrollView = findElementById("ScrollView",
				ScrollViewElement.class);
		tmtsScrollView.scrollTo(480, 400);
	}

	@Test
	public void testScrollBy() throws Exception {
		findElementById("btn_scrollview_activity").doClick();
		ScrollViewElement tmtsScrollView = findElementById("ScrollView",
				ScrollViewElement.class);
		tmtsScrollView.scrollBy(480, 400);
	}

	@Test
	public void testFullScrollUp() throws Exception {
		findElementById("btn_scrollview_activity").doClick();
		findElementById("scroll_button2").doClick();
		Thread.sleep(10000);
	}

	@Test
	public void testFindScrollViewByIndex() throws Exception {
		findElementByText("ScrollView").doClick();
		findElementByIndex(0, ScrollView.class, ScrollViewElement.class)
				.scrollBy(0, 400);
		Thread.sleep(3000);
	}

	@Test
	public void testScrollToNextScreen() throws Exception {
		findElementById("btn_scrollview_activity").doClick();
		findElementById("ScrollView", ScrollViewElement.class)
				.scrollToNextScreen();
		Thread.sleep(5000);
	}
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: