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测试 登录模块测试 登录模拟过程. 发帖模块测试 ...
随机推荐
- hdu5601 BestCoder Round #67 (div.2)
N*M bulbs Accepts: 94 Submissions: 717 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 655 ...
- hdu 5592 BestCoder Round #65(树状数组)
题意: ZYB有一个排列PP,但他只记得PP中每个前缀区间的逆序对数,现在他要求你还原这个排列. (i,j)(i < j)(i,j)(i<j)被称为一对逆序对当且仅当A_i>A_jA ...
- bzoj4034[HAOI2015]树上操作 树链剖分+线段树
4034: [HAOI2015]树上操作 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 6163 Solved: 2025[Submit][Stat ...
- UDA机器学习基础—异常值-安然数据处理
#!/usr/bin/python import pickle import sys import matplotlib.pyplot sys.path.append("../tools/& ...
- Linux下安装3.0以上的python
Linux下自带的python2.7是不建议删除的,很多系统软件依赖python2.7,但是现在我们学习python一般需要python3.0,下面介绍安装python3.0. 1.进入python官 ...
- Tensorflow 基于分层注意网络的文件分类器
After the exercise of building convolutional, RNN, sentence level attention RNN, finally I have come ...
- H5--Web Worker
Web Worker是H5的新特性. JS是单线程的,所以在消息队列中如果用户想进行一些阻塞操作,比如时延timeout和定时器interval,或者是数据量较大及处理过程非常长的场景,就很容易出现页 ...
- vue.js-路由
1:编写router.js import Router from "vue-router" import Vue from "vue" import rou ...
- 深入理解Oracle中的随机函数
--Oracle中取随机值的函数 .dbms_random包 dbms_random包提供了一系列的随机值获取函数以及相关存储过程.下面详细讲解常用的函数和过程. ()dbms_random.rand ...
- PTA中如何出Java题目?
PTA中如何出Java题目? 很多第一次出题的老师,不知道Java在PTA中是如何处理输入的.写一篇文章供大家参考.比如以下这样的一个题目: 从控制台读入两个数,然后将其相加输出. 对于该题可以有如下 ...