前几天看了下 Espresso,看了作者了两篇视频介绍。觉得挺有意思,就拿了看一下。
官网地址:https://code.google.com/p/android-test-kit/wiki/Espresso
源码: git clone https://code.google.com/p/android-test-kit/
独立包路径是 GitHub\android-test-kit\bin\espresso-standalone\espresso-1.0-SNAPSHOT-bundled.jar
还是用的 robotium 的 notepad 的 demo 来尝试,一样的是继承的ActivityInstrumentationTestCase2
测试类。入口类是 espresso,大多数类被定义成 final 类型的。AndroidManifest 里面的 instrumentation 要改成框架里面的
GoogleInstrumentationTestRunner。
<instrumentation
android:name="com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
android:targetPackage="com.example.android.notepad" />
package com.example.android.notepad.test;
import com.example.android.notepad.NotesList;
import static com.google.android.apps.common.testing.ui.espresso.Espresso.onView;
import static com.google.android.apps.common.testing.ui.espresso.action.ViewActions.closeSoftKeyboard;
import static com.google.android.apps.common.testing.ui.espresso.action.ViewActions.pressMenuKey;
import static com.google.android.apps.common.testing.ui.espresso.action.ViewActions.click;
import static com.google.android.apps.common.testing.ui.espresso.action.ViewActions.typeText;
import static com.google.android.apps.common.testing.ui.espresso.matcher.ViewMatchers.isRoot;
import static com.google.android.apps.common.testing.ui.espresso.matcher.ViewMatchers.withText;
import static com.google.android.apps.common.testing.ui.espresso.matcher.ViewMatchers.withId;
import com.example.android.notepad.R;
import android.test.ActivityInstrumentationTestCase2;
public class NotePadTest extends ActivityInstrumentationTestCase2 {
@SuppressWarnings("unchecked")
public NotePadTest() {
super(NotesList.class);
}
@Override
protected void setUp() throws Exception {
getActivity();
super.setUp();
}
public void testAddNote() throws InterruptedException{
Thread.sleep(10000);
onView(isRoot()).perform(pressMenuKey());//点击菜单键
onView(withText("Add note")).perform(click());//点击Add note
onView(withId(R.id.note)).perform(typeText("Have a cup of Espresso."), closeSoftKeyboard()); //输入文本
onView(isRoot()).perform(pressMenuKey());//点击菜单键
onView(withText("Save")).perform(click());//点击Save
Thread.sleep(10000);
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
}
}
简单的写了点,和 robotium 使用上没有多少区别。里面更多的方法还没有细看,后续继续补充。