【Android自动化测试】03. Athrun之用例分析
来源:互联网 发布:金融软件融美科技 编辑:程序博客网 时间:2024/06/05 12:00
如果大家配置好了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);}}
- 【Android自动化测试】03. Athrun之用例分析
- 【Android自动化测试】02. Athrun之环境配置
- 自动化功能测试-Athrun—IOS学习之旅文档
- Android自动化测试之Robotium 用例分析
- Android自动化测试之Robotium<二>:用例分析
- 【Android自动化测试之Robotium】02. Robotium之用例分析
- 【Android自动化测试之Robotium】02. Robotium之用例分析
- 【Android自动化测试之Robotium】02. Robotium之用例分析
- 【Android自动化测试之Robotium】02. Robotium之用例分析
- 【Android自动化测试之Robotium】02. Robotium之用例分析
- Android自动化测试之Robotium登陆测试用例
- IOS测试框架之:athrun的InstrumentDriver源码阅读笔记
- android自动化测试Uiautomator源码分析之二
- android自动化测试Uiautomator源码分析之三
- android自动化测试Uiautomator源码分析之四
- android自动化测试Uiautomator源码分析之五
- android自动化测试Uiautomator API分析之二
- android自动化测试Monkeyrunner源码分析之二
- 面经-来自资深猎头的总结
- python学习第3章
- Weblogic和使用include标签的问题
- java代理请求http(实例)
- 转Linux电源管理
- 【Android自动化测试】03. Athrun之用例分析
- VS2010中的自动化测试(3)——数据驱动的单元测试
- 重定向输出结果
- 使用git clone出现SSL routines:SSL3_GET_SERVER_CERTIFICATE错误的一种解决办法
- 从页面上导出数据到Excel中
- QTP_QTP11过30天试用期如何恢复 [quickTest Professional]
- hdu 3535 涵盖了分组背包的各种情况 非常好的背包题目
- Android数据库相关代码解读(1)
- Linux中Apache+Tomcat+JK实现负载均衡和群集的完整过程(转载)