博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android Espresso使用
阅读量:7217 次
发布时间:2019-06-29

本文共 2293 字,大约阅读时间需要 7 分钟。

自动化测试,创建项目时已导入相对应的包。

  1. 打开sdk(D:\java\androidstudio\sdk\tools)中的tools文件夹下的uiautomatorviewer.bat,点击下图中红色框中的内容捕获一帧画面。

    img_b54de810749ac03711be2da8fb10b0dc.png
    图1.png

  2. Run->点击Record Espresso Test,提示运行安装程序

    img_468343412f6d6834f07feb9150342b2f.png
    图2.png
  3. 点击确定后出现如下界面

    img_076c43ef1408b558e83c4ba9f766ed0e.png
    图3.png
  4. 点击上图中的Add Assertion按钮,配合uiautomatorviewer点击控件增加断言

    img_bdd89e5c05ca008d16deaeae6580507a.png
    图4.png
  5. 选择Save Assertion来保存断言并退出此窗口

    img_56ace05a1f307828a93b81190304a465.png
    图5.png
  6. 点击Complete Recording按钮,完成录制。此时需要填写一个测试的类名,然后点击Save。

    img_808f08f75b7c6ac95d9a5f09b4baa8ee.png
    图6.png
  7. 保存之后,就在androidTest目录的包下生成一个测试类.

    img_319aaf74bd3c4955c49efff51efe7f51.png
    图7.png

测试类代码如下:

@LargeTest@RunWith(AndroidJUnit4.class)public class MainActivityTest {    @Rule    public ActivityTestRule
mActivityTestRule = new ActivityTestRule<>(MainActivity.class); @Test public void mainActivityTest() { ViewInteraction appCompatButton = onView( allOf(withId(R.id.btn_open), withText("open"), withParent(allOf(withId(R.id.activity_main), withParent(withId(android.R.id.content)))), isDisplayed())); appCompatButton.perform(click()); ViewInteraction textView = onView( allOf(withText("登录成功"), childAtPosition( allOf(withId(R.id.activity_login), childAtPosition( withId(android.R.id.content), 0)), 0), isDisplayed())); textView.check(matches(withText("登录成功"))); } private static Matcher
childAtPosition( final Matcher
parentMatcher, final int position) { return new TypeSafeMatcher
() { @Override public void describeTo(Description description) { description.appendText("Child at position " + position + " in parent "); parentMatcher.describeTo(description); } @Override public boolean matchesSafely(View view) { ViewParent parent = view.getParent(); return parent instanceof ViewGroup && parentMatcher.matches(parent) && view.equals(((ViewGroup) parent).getChildAt(position)); } }; }}
  1. 这时,在刚刚生成的测试类右键,选择Run 'MainActivityTest' 点击运行,此时代码执行的过程即是刚刚操作的步骤。

    img_39c7b0156ec819be5d5af3af29f10ea2.png
    图8.png

转载地址:http://yhxym.baihongyu.com/

你可能感兴趣的文章
Linux 系统下双机HA的实现
查看>>
02_swarm mode key concepts
查看>>
Eclipse打包插件Fat Jar 解压打包
查看>>
Apache Shiro 使用手册
查看>>
CentOS mini 6.5 安装DB2 Express-C 问题处理记录
查看>>
DirectByteBuffer
查看>>
Docker Compose文件详解 V2
查看>>
Memcached的原理与应用(未完)
查看>>
基于 Confluence 6 数据中心的 SAML 单点登录设置你的身份提供者
查看>>
mysql总结
查看>>
Navicat for MySQL版本更新至v11.2.12,修复多项问题|附下载
查看>>
整理 JAVA中的IO流 (字符流和字节流两个大类)
查看>>
uefi与win8 (根据网络资料整理)
查看>>
Eclipse优化
查看>>
Log4j tutorial with Tomcat examples
查看>>
Kong 网关
查看>>
三层结构视频中的DBHelper.cs
查看>>
[转载] 信息系统项目管理师视频教程——18 项目沟通管理
查看>>
在Windows下建立QT开发环境
查看>>
Jedis、JedisPool、ShardedJedis和ShardedJedisPool,java对redis的基本操作
查看>>