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测试 登录模块测试 登录模拟过程. 发帖模块测试 ...
随机推荐
- Mysql参数汇总
凡是需要耐心. 参数为静态参数则黄色字体标记. 参数为全局变量则粗体标记. 参数为全局.会话变量则不标记. auto_increment_increment auto_increment_offset ...
- Servlet中service()方法
在学习Servlet的过程中,我们大多时候编码都是直接继承HttpServlet这个类,并且重写doGet ,doPost,但是查看Api时我们会发现Servlet接口 ,GenericSevlet抽 ...
- JS中怎样判断undefined(比较不错的方法)
最近做项目碰到的问题.拿出来跟大家分享一下吧. 用servlet赋值给html页面文本框值后,用alert来弹出这个值.结果显示"undefined".所以我就自然的用这个值和字符 ...
- promise应用及原生实现promise模型
一.先看一个应用场景 发送一个请求获得用户id, 然后根据所获得的用户id去执行另外处理.当然这里我们完全可以使用回调,即在请求成功之后执行callback; 但是如果又添加需求呢?比如获得用户id之 ...
- javascript requestAnimationFarme
今天看到一篇很好的文章推荐一下:原文地址:http://www.zhangxinxu.com/wordpress/?p=3695 CSS3动画那么强,requestAnimationFrame还有毛线 ...
- VueJs(3)---V-指令
VueJs(3)---V-指令(1) 一.语法 v- 指令是带有v-的特殊属性 v-if 条件渲染 v-show v-else (必须在v-if/v-else-if/v-show指令后) v-else ...
- 2-学习GPRS_Air202(Air202开发板介绍和下载第一个程序)
http://www.cnblogs.com/yangfengwu/p/8887933.html 资料链接 链接:https://pan.baidu.com/s/1968t2QITuxoyXlE_Nz ...
- Python中and(逻辑与)计算法则
在程序设计中,and称为逻辑与运算,也称布尔运算:1.and是在布尔上下文中从左到右计算表达式的值:2.0.''.[].().{}.None.False在布尔上下文中为假:其它任何东西都为真:3.如果 ...
- python笔记三(list、tuple)
一.list list的增删改查 #增, classmates.append("nadech") #在末尾追加一个元素 classmates.insert(1,"agui ...
- ACM 最小公倍数
给定两个正整数,计算这两个数的最小公倍数. Input 输入包含多组测试数据,每组只有一行,包括两个不大于1000的正整数.Output对于每个测试用例,给出这两个数的最小公倍数,每个实例输出一行. ...