如果大家配置好了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;//繼承AthrunTestCasepublic 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//等待activitypublic 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));}@Testpublic void testPressMenu() throws Exception {getDevice().pressMenu();findElementByText("Toast").doClick();assertEquals(true, waitForText("Hello World", 2000));}@Testpublic void testPressHome() throws InterruptedException {getDevice().pressHome();Thread.sleep(2000);}@Testpublic void testPressBack() throws Exception {findElementById("btn_scrollview_activity").doClick();findElementByText("Bottom Button").doClick();getDevice().pressBack();assertEquals(true, getDevice().waitForActivity("MainActivity", 2000));}@Testpublic 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));}@Testpublic 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));}@Testpublic void testSetScreen() throws InterruptedException {getDevice().setScreenOrientation(android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);assertEquals("landscape", getDevice().getScreenOrientation());}@Testpublic void testGetStringById() throws Exception {findElementByText("ScrollView").doClick();assertEquals(true, getStringById("scroll_text").contains("道可道"));// log(getStringById("scroll_text"));}@Testpublic void testFindToastById() throws Exception {findElementById("my_imageview", ViewElement.class).doLongClick();assertEquals(true, waitForText("LongClick", 2000));}@Testpublic void testRequestFocus() throws Exception {findElementByText("ScrollView").requestFocus();getDevice().pressDown();getDevice().pressUp();getDevice().pressEnter();assertEquals(true, getDevice().waitForActivity("ScrollActivity", 2000));}@Testpublic void testPressEnter() throws Exception {findElementByText("ListView").requestFocus();getDevice().pressDown();getDevice().pressEnter();getDevice().pressEnter();Thread.sleep(2000);}@Testpublic void testFindViewByIntId() throws Exception {findElementById(R.id.btn_scrollview_activity, ViewElement.class).doClick();assertEquals(true, getDevice().waitForActivity("ScrollActivity", 5000));}@Testpublic void testDevicePressKey() throws Exception {findElementByText("ScrollView").requestFocus();getDevice().pressEnter();assertEquals(true, getDevice().waitForActivity("ScrollActivity", 1000));getDevice().pressBack();assertEquals(true, getDevice().waitForActivity("MainActivity", 1000));}@Testpublic 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);}@Testpublic void testFindViewOutOfScreen2() throws Exception {ViewGroupElement rootGroup = findElementById("mainroot",ViewGroupElement.class);rootGroup.findElementById("btn_scrollview_activity", ViewElement.class).doClick();}@Testpublic void testFindViewOutOfScreen3() throws Exception {ViewGroupElement rootGroup = findElementById("mainroot",ViewGroupElement.class);rootGroup.getChildByIndex(4, ViewElement.class).doClick();}@Testpublic 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");}@Testpublic 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));}@Testpublic 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));}@Testpublic 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));}@Testpublic 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");}@Testpublic void testFullScroll() throws Exception {findElementById("btn_scrollview_activity").doClick();ScrollViewElement tmtsScrollView = findElementById("ScrollView",ScrollViewElement.class);tmtsScrollView.fullScroll(View.FOCUS_DOWN);}@Testpublic void testScrollTo() throws Exception {findElementById("btn_scrollview_activity").doClick();ScrollViewElement tmtsScrollView = findElementById("ScrollView",ScrollViewElement.class);tmtsScrollView.scrollTo(480, 400);}@Testpublic void testScrollBy() throws Exception {findElementById("btn_scrollview_activity").doClick();ScrollViewElement tmtsScrollView = findElementById("ScrollView",ScrollViewElement.class);tmtsScrollView.scrollBy(480, 400);}@Testpublic void testFullScrollUp() throws Exception {findElementById("btn_scrollview_activity").doClick();findElementById("scroll_button2").doClick();Thread.sleep(10000);}@Testpublic void testFindScrollViewByIndex() throws Exception {findElementByText("ScrollView").doClick();findElementByIndex(0, ScrollView.class, ScrollViewElement.class).scrollBy(0, 400);Thread.sleep(3000);}@Testpublic void testScrollToNextScreen() throws Exception {findElementById("btn_scrollview_activity").doClick();findElementById("ScrollView", ScrollViewElement.class).scrollToNextScreen();Thread.sleep(5000);}}