为你的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. hdu 5135(2014广州—状态dp)

    t题意:给你n条边,构造任意个三角形,一个三角形恰好只用3条边,每条边只能一次,求面积最大值 思路: 最开始想的是先排序从大到小取,但感觉并不怎么靠谱. 最多12条边,所以可以求出所有可能的三角形面积 ...

  2. 51Nod 1482 部落信号

    题目描述: 众所周知,如今的波兰在很久以前住着很多部落.他们的首都被n座山所环绕,形成一个圆圈.在每一座山上有一个哨兵,他日夜观察附近的山. 如果有任何危险,哨兵会在山上放一把火.如果在连接两座山的圆 ...

  3. TOJ 数据结构实验--静态顺序栈

    描述 创建一个顺序栈(静态),栈大小为5.能够完成栈的初始化.入栈.出栈.获取栈顶元素.销毁栈等操作. 顺序栈类型定义如下: typedef struct {  int data[Max];    i ...

  4. java(MyEclipse)创建webservice和测试webservice

    转载地址:http://blog.csdn.net/hsfy2012/article/details/46300921 创建并发布自己的Webservice的工具  1 安装MyEclipse  2 ...

  5. TensorFlow + Keras 实战 YOLO v3 目标检测图文并茂教程

    运行步骤 1.从 YOLO 官网下载 YOLOv3 权重 wget https://pjreddie.com/media/files/yolov3.weights 下载过程如图: 2.转换 Darkn ...

  6. Go 实现判断变量是否为合法数字 IsNumeric 算法

    [转] http://www.syyong.com/Go/Go-to-determine-whether-the-variable-is-a-legal-digital-algorithm.html ...

  7. 编程英语之KNN算法

    School of Computer Science The University of Adelaide   Artificial Intelligence Assignment 2   Semes ...

  8. delphi 线程教学第七节:在多个线程时空中,把各自的代码塞到一个指定的线程时空运行

    第七节:在多个线程时空中,把各自的代码塞到一个指定的线程时空运行     以 Ado 为例,常见的方法是拖一个 AdoConnection 在窗口上(或 DataModule 中), 再配合 AdoQ ...

  9. CSS中display:block属性的作用

    display:block可以理解为块,举个简单的例子!比如你做一个超链接,<li><a href="#">超链接</a></li> ...

  10. PHP 完整表单实例

    PHP - 在表单中确保输入值 在用户点击提交按钮后,为确保字段值是否输入正确,我们在HTML的input元素中插添加PHP脚本, 各字段名为: name, email, 和 website. 在评论 ...