s
用eclipse创建指定工程的测试工程,然后修改
1. 添加libs/framework.jar
2. 修改AndroidManifest.xml
原始:
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="待测程序的包名" />
改为:
<instrumentation
android:name="pl.polidea.instrumentation.PolideaInstrumentationTestRunner"
android:targetPackage="待测程序的包名" />
加上下面,用于写日志到sdcard
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
3. 附加内容:
删除日志:adb shell "rm /mnt/sdcard/Athrun/*.*"
导出日志:adb pull /mnt/sdcard/Athrun ./log
1. 创建
New-> JUnit Test Case
如MainActivityTest
org.athrun.android.framework.AthrunTestCase
加上如下import
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;
原始:
public MainActivityTest(String name) {
super(name);
}
改为:
public MainActivityTest() throws Exception {
super("org.athrun.android.app", "org.athrun.android.app.MainActivity");//MainActivity是指第一个打开的act,比如登录、注册页之类的
AthrunTestCase.setMaxTimeToFindView(10000);
}
0.疑问
closeCurrentActivity("ListViewActivity");
1. 当前截屏
captureScreenShot()
2. 查找元素
ViewGroupElement include = findElementById("include_checkbox", ViewGroupElement.class);
AbsListViewElement listView = findElementByIndex(0, AbsListView.class, AbsListViewElement.class);
TextViewElement findElementByText(String text)
3.获取当前activity,格式为:"org.athrun.android.app.TabOneActivity"
getCurrentActivityName();
4.获取设备。然后就可以操作设备,如下:
getDevice().pressMenu();//按下某个键
getDevice().setScreenOrientation(android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//设置屏幕方向
assertEquals("landscape", getDevice().getScreenOrientation());//获取屏幕方向return landscape or portrait
assertEquals(true, getDevice().isEmulator());//判断是否为模拟器
示例1:
findElementByText("ListView").doClick();//通过字本找到一个元素,并点击
AbsListViewElement listView = findElementById("my_listview",AbsListViewElement.class);//通过id找到一个listview
listView.scrollToNextScreen();//滚动这个listview到下一屏
ViewElement tmtsView = listView.getChildByIndex(35, ViewElement.class);//通过index找到第36个元素
tmtsView.doLongClick();//长按这个元素
findElementByText("Item One").doClick();//通过字本找到一个元素,并点击
assertEquals(true, waitForText("1 pressed!", 2000));//判断在2000毫秒内是否在当前页内显示出“1 pressed!”的文本
s
s
s