Android Espresso基本使用

上面一篇文章Android Espresso UI自动测试的搭建 讲了如何搭建环境,下面说一些语法和使用方法。

找到View

使用ViewMatcher.class 里面的方法可以找到你想要的View,如你想找有Hello文字的View,你可以这样使用

onView(withText("Hello"));

相似的你也可以使用View的资源Id来找到该view

onView(withId(R.id.hello));

当有多个约束条件时,可以使用Matchers.class的allof()方法来组合,例子如下:

onView(allOf(withText("Hello") ,withId(R.id.hello)));

对View执行一些操作

对View操作的代码大概是这样: onView(...).perform();
在onView中找到这个View后,调用perform()方法进行操作,如点击该View:

onView(withId(R.id.hello)).perform(click());

也可以执行多个操作在一个perform中如

onView(withId(R.id.hello)).perform(click(),clearText());

检查View

使用check()方法来检查View是否符合我们的期望 onView(...).check();
如检查一个View里面是否有文字Hello:

onView(withId(R.id.hello)).check(matches(withText("Hello")));```

更详细的用法可参考下图:![SKODS.png](http://upload-images.jianshu.io/upload_images/1816215-ea7b7c4d6720f0bd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

google官方教程:https://developer.android.com/training/testing/ui-testing/espresso-testing.html
和youtube视频也可以参考https://www.youtube.com/watch?v=W8LJjfkTKik  ,需要梯子

列表用法看这里:[Android Espresso Recycle使用和一些坑](http://www.jianshu.com/p/9e4437615b40)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,002评论 25 709
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,603评论 2 45
  • Instrumentation介绍 Instrumentation是个什么东西? Instrumentation测...
    打不死的小强qz阅读 7,843评论 2 39
  • 一首歌/一种心情/一个故事 2017年4月5日 ☼ 《山丘》是李宗盛亲自创作的一首歌,其实这首歌的旋律李宗盛早在2...
    点暂阅读 1,612评论 7 11
  • 人做事前总要为自己找个道理,实在没道理了,就赖给冥冥之中自有天意。 我无法分辨,是真的有命运,还是这个位置上,非此...
    琐尾阅读 337评论 0 0