Espresso Espresso 试水贴

喜力 · 2013年11月11日 · 最后由 lqz7 回复于 2015年01月09日 · 1600 次阅读
本帖已被设为精华帖!

前几天看了下 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 使用上没有多少区别。里面更多的方法还没有细看,后续继续补充。

共收到 12 条回复 时间 点赞

欢迎喜力

改了下格式,爪机无力啊。。。

#2 楼 @lihuazhang 好晚。。= =

欢迎喜力~我正写你的网站介绍进入我的书呢~

#3 楼 @monkey 盐水没了,换药呢。。。

有没有什么方法可以直接在手机上运行测试 不使用 adb

#7 楼 @seveniruby 额。。instrumentation 通过 adb shell am instruments 来驱动,不过 Espresso 从这个尿性来看,如果可以,也应该是这样一个路数

#8 楼 @monkey adb shell 这种命令是电脑执行的, 不能在手机上执行..应该是需要 startInstrument 函数吧, 我这方面没验证, 不知道是否可以.
@lizhenghuan 你的代码是如何启动运行的那. apk 的入口函数如何写, 能给个例子演示下吗

喜力 #10 · 2013年11月12日 Author

#9 楼 @seveniruby 参考 sdk 自带工具 Dev tools

#10 楼 @lizhenghuan 我的意思是在 app 中可以自由的插桩任何 app, 所以不能在 manifest 中写死插桩配置. 采用 startinent startactivity 和 startinstrumentation 方式.
我翻了下 sdk 中的代码, 也没找到靠谱的例子, 我也没做过验证, 我怀疑 google 在这套技术背后隐藏了一个更底层的自动化体系.
求大牛科普

没有源码的情况下 Espresso 能测试 apk 吗@lizhenghuan

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册