为你的APP进行UI测试是为了确保不出现意料之外的结果,提升用户的体验。如果你需要验证你的APP UI的正确性,你需要养成创建UI测试的习惯。

Espresso测试框架是由Android Testing Support Library提供,包含了编写UI测试的API用于模拟用户在指定的APP界面上进行交互。Espresso测试可以运行在Android 2.2(API level 8)以上的设备。当主线程空闲时,Espresso可以侦测到,所以它可以在合适的时候运行你的测试指令,提升测试的可信度。

Espresso基于仪表测试。

配置Espresso

先看第一篇。

在build.gradle文件中添加依赖。

dependencies {
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
}

关闭测试机器的动画。

创建一个Espresso 测试类

为了创建一个Espresso测试,按照以下的方式创建一个Java类:

1、 通过调用onView()方法或者onData()在Activity中找到需要测试的UI组件。

2、 通过调用ViewInteraction.perform()或DataInteraction.perform()在UI组件上模拟特定的用户动作。

3、 需要的话重复如上动作。

4、 用ViewAssertions来检测UI。

代码如下:

nView(withId(R.id.my_view))
.perform(click())
.check(matches(isDisplayed()));

使用带ActivityTestRule的Espresso

下面将接受如何创建Junit 4风格的Espresso 测试,通过使用ActivityTestRule来减少不必要的代码。

package com.example.android.testing.espresso.BasicSample;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith; import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
... @RunWith(AndroidJUnit4.class)
@LargeTest
public class ChangeTextBehaviorTest { private String mStringToBetyped; @Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(
MainActivity.class); @Before
public void initValidString() {
// 声明一个String
mStringToBetyped = "Espresso";
} @Test
public void changeText_sameActivity() {
// 输入文字,点击按钮
onView(withId(R.id.editTextUserInput))
.perform(typeText(mStringToBetyped), closeSoftKeyboard());
onView(withId(R.id.changeTextBt)).perform(click()); // 检测文字改变
onView(withId(R.id.textToBeChanged))
.check(matches(withText(mStringToBetyped)));
}
}

使用带ActivityInstrumentationTestCase2的Espresso

代码如下:

import android.support.test.InstrumentationRegistry;

public class MyEspressoTest
extends ActivityInstrumentationTestCase2<MyActivity> { private MyActivity mActivity; public MyEspressoTest() {
super(MyActivity.class);
} @Before
public void setUp() throws Exception {
super.setUp();
injectInstrumentation(InstrumentationRegistry.getInstrumentation());
mActivity = getActivity();
}
}

访问UI组件

在测试下Espresso与你的app进行交互之前,你首先需要声明UI 组件或者view。

代码如下:

public void testChangeText_sameActivity() {
// 输入文字,点击按钮
onView(withId(R.id.editTextUserInput))
.perform(typeText(STRING_TO_BE_TYPED), closeSoftKeyboard());
onView(withId(R.id.changeTextButton)).perform(click());
}

声明一个ViewMatcher

可以通过如下方法声明一个view matcher:

1、 调用ViewMatchers类中的方法。如下:

onView(withText("登陆"));
onView(withId(R.id.button_signin));

使用id的时候需要注意,Android中资源id并不是唯一的,使用不当,Espresso可能会抛出AmbiguousViewMatcherException一场。

2、 使用Hamcrest Matchers类。你可以使用allof()方法来组合多个matchers。比如containsString()和instanceof()

onView(allOf(withId(R.id.button_signin), withText(“登陆”)));

onView(allOf(withId(R.id.button_signin), not(withText(“登出”))));


在AdapterView中定位View 代码如下:

onData(allOf(is(instanceOf(Map.class)),

hasEntry(equalTo(LongListActivity.ROW_TEXT), is(str))));


模拟动作

ViewActions.click():单击view

ViewActions.typeText():单机view并输入一个特定的string

ViewActions.scrollTo():滚动操作

ViewActions.pressKey();按键单机

ViewActions.clearText():清除text


验证结果 通过调用ViewInteraction.check()或者DataInteraction.check()方法来检测。
代码如下:

public void testChangeText_sameActivity() {

// 检测text更改

onView(withId(R.id.textToBeChanged))

.check(matches(withText(STRING_TO_BE_TYPED)));

}

“`

本文作者:宋志辉

个人微博:点击进入

4、Android UI测试的更多相关文章

  1. Android ui 测试课堂笔记

    开始接触Android ui测试了,笔记如下 模拟器 Genemotion , the fastest android simulator in the world Android ui 测试工具 S ...

  2. Google+ 团队的 Android UI 测试

    https://github.com/bboyfeiyu/android-tech-frontier/tree/master/android-blog/Google%2B%20%E5%9B%A2%E9 ...

  3. Appium 在 Android UI 测试中的应用

    原文地址:https://blog.coding.net/blog/Appium-Android-UI Android 测试工具与 Appium 简介 Appium 是一个 C/S 架构的,支持 An ...

  4. TODO: Android UI测试 UIAutomator

    前几天跑了一下UIAutomator的demo,忘记写下来了...真的日,有点忘了都. 待填坑

  5. 使用uiautomatorviewer和uiautomator来做android的UI测试

    来自:http://university.utest.com    作者:Angelos Nakulas (All Authored Courses)      译者:Elaine00 目录 简介 什 ...

  6. 在Android Studio中进行单元测试和UI测试

    本篇教程翻译自Google I/O 2015中关于测试的codelab,掌握科学上网的同学请点击这里阅读:Unit and UI Testing in Android Studio.能力有限,如有翻译 ...

  7. Android开源测试框架学习

    近期因工作需要,分析了一些Android的测试框架,在这也分享下整理完的资料. Android测试大致分三大块: 代码层测试 用户操作模拟,功能测试 安装部署及稳定性测试 代码层测试 对于一般java ...

  8. 手机客户端UI测试常见的测试点

    1.各种分辨率下,显示正常.现市场上主流的塞班V3系统手机为240*320.320*240.WM系统主要为240*320.320*480.Android系统主要为320*480,Iphone系统为32 ...

  9. 一起买beta版UI测试

    一起买beta版UI测试 测试目的 保证代码质量,对各个单元进行测试,可以有效地保证代码的可靠性,让模块在与别的模块整合时出现更少的错误. UI测试 登录模块测试 ​ 登录模拟过程. 发帖模块测试 ​ ...

随机推荐

  1. 关于java线程中stop interrupt daemon wait notify

    一.关于终止线程stop与interrupt 一般来说,线程执行结束后就变成消亡状态,乍看之下我们并不需要人为进行干预(人为停止线程),不过凡事都有例外吧,在服务器或者其他应用场景下,线程为了提供服务 ...

  2. 华科机考:IP地址

    时间限制:1秒  空间限制:32768K 题目描述 输入一个ip地址串,判断是否合法. 输入描述: 输入的第一行包括一个整数n(1<=n<=500),代表下面会出现的IP地址的个数. 接下 ...

  3. TensorFlow 聊天机器人开源项目评测第一期:DeepQA

    聊天机器人开源项目评测第一期:DeepQA https://github.com/Conchylicultor/DeepQA 用 i5 的笔记本早上运行到下午,跑了 3 轮的结果,最后效果并不理想.官 ...

  4. 如何成为快手尬舞王?HUAWEI HiAI了解一下!

    左手!右手!抱一抱!扭一扭! 快手短视频,红遍东西南北中, 给大家的生活增添了不少乐趣. 有了人体姿态识别的魔法表情, 不会跳舞的也都可以跟着跳一跳. 从村口朴实的阿姨,到写字楼里端庄的白领, 在人体 ...

  5. 取list的值

    list.get(0):之类的我就不写了 我就写一个我老忘记的 Iterator it = list.iterator(); while(it.hasNext()){ Student stu = it ...

  6. 操作系统内存管理之 内部碎片vs外部碎片

    外部碎片:因为行程持续地被载入与置换,使得可用的记忆体空间被分割成许多不连续的区块.虽然记忆体所剩空间总和足够让新行程执行,却因为空间不连续,导致程式无法载入执行.内部碎片:发生在以固定长度分割区来进 ...

  7. Echarts 中国地图各个省市自治区自定义颜色

    前言 最近接了一个外包的项目,其中有个需求是这样的, 需要展示一个中国的统计地图,要求每个省市区都是不一样的颜色,必须特别区分开.以及隐藏南海部分. 看了Echats相关文档,发现有类似的demo,但 ...

  8. 643. Maximum Average Subarray

    Given an array consisting of \(n\) integers, find the contiguous subarray of given length \(k\) that ...

  9. 让互联网更快,Server Push 特性及开启方式详解

    过去 Nginx 并不支持 HTTP/2 的 Server Push 特性,幸运的是 Nginx 1.13.9 已支持该特性,详情介绍请移步 Nginx 官方博客. Server Push 这个特性是 ...

  10. Oracle中case用法总结

    --case语句的种类: .简单case语句 语法: case exp when comexp then returnvalue ... when comexp then returnvalue el ...