[Android automated testing] 03. athrun Case Analysis

Source: Internet
Author: User

If you have configured the test environment of athrun, let's get started (only the source code is introduced here, and you can do it without the source code)

Tested application download: http://download.csdn.net/detail/wirelessqa/4487252

1. Import the tested application-create a test project-dependent on the tested application

Manifest. xml of the test project

<?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. Test code (only paste mainactivity, the use case is very simple and will not be commented out)

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; // inherit from athruntestcasepublic class mainactivitytest extends athruntestcase {Private Static final string log_tag = "mainactivitytest"; Public mainactivitytest () throws exception {// super (name of the tested package, started activity) Super ("org. athr Un. android. APP "," org. athrun. android. app. mainactivity "); // you can specify the maximum athruntestcase time for a view. setmaxtimetofindview (10000) ;}@ test // wait for 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 // The Public void testfindelementintree () throws exception {v Iewgroupelement 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 // directly find public void testfindviewbyiddirect () throws exception {findelementbyid ("my_checkbox "). doclick (); assertequals ("checkbox is checked! ", Findelementbyid (" textview ", textviewelement. class ). gettext () ;}@ test // long press 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 exc Eption {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 W Ith 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 ("Dao"); // 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);}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.