首页 Android自动化测试

Android自动化测试

举报
开通vip

Android自动化测试 达内广州软件培训 http:// tarena3g.com.cn/ Android自动化测试 如果大家配置好了athrun的测试环境,那么开始吧(这里只介绍有源码的情况,没有源码也是可以做的) 1. 导入被测试应用-- 新建测试工程-- 依赖被测试应用 测试工程的Manifest.xml                                               2. 测试代码 (只贴出MainActivity,用例很简单就不多作注释了) package org.athrun.andro...

Android自动化测试
达内广州软件培训 http:// tarena3g.com.cn/ Android自动化测试 如果大家配置好了athrun的测试环境,那么开始吧(这里只介绍有源码的情况,没有源码也是可以做的) 1. 导入被测试应用-- 新建测试工程-- 依赖被测试应用 测试工程的Manifest.xml                                               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);      }  }  广州天河北校区 广州达内岗顶中心 广州达内江南中心天河区天寿路105号天寿大厦4层 天河龙口东路5号龙晖大厦403-405室 海珠区礼岗路10号2楼 电话:020-38097500 联系电话:020-85262372 联系电话:020-34430387
本文档为【Android自动化测试】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_699045
暂无简介~
格式:doc
大小:79KB
软件:Word
页数:9
分类:互联网
上传时间:2013-04-19
浏览量:41