4、Android UI测试
为你的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测试的更多相关文章
- Android ui 测试课堂笔记
开始接触Android ui测试了,笔记如下 模拟器 Genemotion , the fastest android simulator in the world Android ui 测试工具 S ...
- Google+ 团队的 Android UI 测试
https://github.com/bboyfeiyu/android-tech-frontier/tree/master/android-blog/Google%2B%20%E5%9B%A2%E9 ...
- Appium 在 Android UI 测试中的应用
原文地址:https://blog.coding.net/blog/Appium-Android-UI Android 测试工具与 Appium 简介 Appium 是一个 C/S 架构的,支持 An ...
- TODO: Android UI测试 UIAutomator
前几天跑了一下UIAutomator的demo,忘记写下来了...真的日,有点忘了都. 待填坑
- 使用uiautomatorviewer和uiautomator来做android的UI测试
来自:http://university.utest.com 作者:Angelos Nakulas (All Authored Courses) 译者:Elaine00 目录 简介 什 ...
- 在Android Studio中进行单元测试和UI测试
本篇教程翻译自Google I/O 2015中关于测试的codelab,掌握科学上网的同学请点击这里阅读:Unit and UI Testing in Android Studio.能力有限,如有翻译 ...
- Android开源测试框架学习
近期因工作需要,分析了一些Android的测试框架,在这也分享下整理完的资料. Android测试大致分三大块: 代码层测试 用户操作模拟,功能测试 安装部署及稳定性测试 代码层测试 对于一般java ...
- 手机客户端UI测试常见的测试点
1.各种分辨率下,显示正常.现市场上主流的塞班V3系统手机为240*320.320*240.WM系统主要为240*320.320*480.Android系统主要为320*480,Iphone系统为32 ...
- 一起买beta版UI测试
一起买beta版UI测试 测试目的 保证代码质量,对各个单元进行测试,可以有效地保证代码的可靠性,让模块在与别的模块整合时出现更少的错误. UI测试 登录模块测试 登录模拟过程. 发帖模块测试 ...
随机推荐
- Python SMTP邮件发送
SMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本邮件.HTML邮件以及带附件的邮件. Python对SMTP支持有smtplib和email两个模块: email负责构造邮件 ...
- Linux学习之Centos(三)------系统文件目录及含义详解
Linux学习之Centos 之三------文件目录及含义 在了解了每个文件的相关种类与属性,以及了解了如何更改文件属性/权限的相关信息后,再来要了解的就是, 为什么每套Linux distribu ...
- IE下iframe跨域session和cookie失效问题的解决方案
http://blog.csdn.net/wauit/article/details/9875157
- Microsoft Visual Studio 2017 编译最新版 libuv 1.x
步骤很简单 1 下载最新版的 libuv(地址:https://github.com/libuv 2 安装Git,Python 2.7 ,cmake(这里使用的是 3.11.0-win64-x64 版 ...
- jQuery 捕获
jQuery 拥有可操作 HTML 元素和属性的强大方法. jQuery DOM 操作 jQuery 中非常重要的部分,就是操作 DOM 的能力. jQuery 提供一系列与 DOM 相关的方法,这使 ...
- Jmeter(二)_基础元件
测试计划(Test Plan) 它用来描述一个测试方案,包含与本次性能测试所有相关的功能.也就说本次测试的所有内容是于基于一个计划的. "函数测试模式"复选框,如果被选择,它会使J ...
- Java内存泄漏分析系列之一:使用jstack定位线程堆栈信息
原文地址:http://www.javatang.com 前一段时间上线的系统升级之后,出现了严重的高CPU的问题,于是开始了一系列的优化处理之中,现在将这个过程做成一个系列的文章. 基本概念 在对J ...
- Spring中@Transactional事务回滚(含实例详细讲解,附源码)
一.使用场景举例 在了解@Transactional怎么用之前我们必须要先知道@Transactional有什么用.下面举个栗子:比如一个部门里面有很多成员,这两者分别保存在部门表和成员表里面,在删除 ...
- Bootstrap3 表格-条纹状表格
通过 .table-striped 类可以给 之内的每一行增加斑马条纹样式. 跨浏览器兼容性 条纹状表格是依赖 :nth-child CSS 选择器实现的,而这一功能不被 Internet Explo ...
- android MultiDex multidex原理下超出方法数的限制问题(三)
android MultiDex 原理下超出方法数的限制问题(三) 插件化?自动化?multiDex?是不是觉得已经懵逼了?请先看这篇文章的内容,在下篇文章中将会详解具体的过程- 随着应用不断迭 ...