UiAutomator2.0 - Toast信息的验证
问题:
在做UI自动化测试时,偶尔会碰到 Toast 这种提示信息(如图),通过Uiautomatorviewer 无法获该类控件的信息。所以无法验证,该条case不能实现。然后就没然后了...

思考:
在《UiAutomator2.0 - 与AccessibilityService的关联》实验后,发现Toast提示信息所属事件为 AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED,依据《UiAutomator2.0 - 控件实现点击操作原理》中的分析,那也可以模仿源码监听该事件啊!美滋滋~~
实现:
准备着手实现时,发现其他类的相关方法并没公开,只有UiAutomation这个类公开了setOnAccessibilityEventListener方法(通过该方法进行监听Toast)。突破口找到了,那么就从这个方法开始实现。
1、创建一个 VerifyToast类,代码如下:
package com.testtoast;
import android.app.Notification;
import android.app.UiAutomation;
import android.os.Parcelable;
import android.support.test.InstrumentationRegistry;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
/**
* @author zzw
* Toast Validation Helper
*/
public class VerifyToast {
private static final String TAG = TestCase_FM.class.getSimpleName();
private static VerifyToast verifyToast = new VerifyToast();
private boolean isPass;
private VerifyToast(){}
public static VerifyToast getVerifyToast(){
return verifyToast;
}
public boolean getIsPass(){
return isPass;
}
public VerifyToast setIsPass(boolean isPass){
this.isPass = isPass;
return this;
}
/**
* Listen for toast prompts
* @param pck The package name of toast
* @param msg Toast info
*/
public void monitoringToast(final String pck, final String msg){
UiAutomation uiAutomation = InstrumentationRegistry.getInstrumentation().getUiAutomation();
uiAutomation.setOnAccessibilityEventListener(new UiAutomation.OnAccessibilityEventListener() {
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
if(event.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED){
Log.d(TAG, "onAccessibilityEvent:"+
String.format("--pck: %s --msg: %s",getPackage(event),getMessage(event)));
isPass = pck.equals(getPackage(event)) && msg.equals(getMessage(event));
Log.d(TAG, "onAccessibilityEvent: isPass = "+ isPass);
}
}
});
}
// 获取监听的包名
private String getPackage(AccessibilityEvent event){
return (String) event.getPackageName();
}
// 获取 Toast 信息
private String getMessage(AccessibilityEvent event){
String message = null;
Parcelable parcelable = event.getParcelableData();
if (!(parcelable instanceof Notification)) {
message = (String) event.getText().get(0);
}
return message;
}
}
2、测试用例中的调用
package com.testtoast;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiSelector;
import android.util.Log;
import com.zzw.commonutils.UiApps;
import com.zzw.tools.ScreenCap;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import static junit.framework.Assert.assertTrue;
/**
* @author zzw
* Test for Toast
*/
@RunWith(AndroidJUnit4.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TestCase_FM {
private static final String TAG = TestCase_FM.class.getSimpleName();
private UiDevice device= UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
private String catchFail = "Catch_Pic";
@BeforeClass
public static void before(){
Log.d(TAG, "before: -----------------------------start-------------");
}
@Test
public void testCase_GetToast() throws Throwable {
VerifyToast verifyToast = VerifyToast.getVerifyToast();
String pck = "com.caf.fmradio";
String msg = "Please plug in a Headset to use FM Radio";
device.pressHome();
UiObject app= device.findObject(new UiSelector().text("FM Radio"));
Log.d(TAG, "testCase_GetToast: result == "+verifyToast.getIsPass());
verifyToast.setIsPass(false).monitoringToast(pck,msg);
try{
new UiApps().toOpenApp(app); //自己封装打开app的方法
assertTrue("Toast prompt error", verifyToast.getIsPass());
}catch (Throwable e){
e.printStackTrace();
// 自己封装的截图方法
ScreenCap.takeScreenshotToPicturesDirPath(catchFail);
throw e;
}
Log.d(TAG, "testCase_GetToast: result == "+verifyToast.getIsPass());
}
}
注:在 adb shell uiautomator --help 中有这么一句
events: prints out accessibility events until terminated
那么在控制台也直观的查看当前的Accessibility事件了,结果如图:

UiAutomator2.0 - Toast信息的验证的更多相关文章
- 微软企业库5.0 学习之路——第五步、介绍EntLib.Validation模块信息、验证器的实现层级及内置的各种验证器的使用方法——下篇
一.独立验证器 我上篇中我将AndCompositeValidator和OrCompositeValidator归为独立验证器,这2个验证器主要是为了第一类验证服务,可以进行多种验证组合在一起进行复杂 ...
- 『与善仁』Appium基础 — 29、获取toast信息
目录 1.toast介绍 2.toast定位 3.示例 4.封装toast判断 1.toast介绍 Android中的toast是一种简易的消息提示框,toast提示框不能被用户点击,会根据所设置的显 ...
- app自动化toast信息获取
移动端测试比较常遇到toast类型的提示信息,那么对于这类信息我们要怎么获取呢,让我细细道来: 首先,获取toast信息要注意以下几点: 必须是Uiautomator2框架,设备配置参数中设置 &qu ...
- UiAutomator2.0升级填坑记
UiAutomator2.0升级填坑记 SkySeraph May. 28th 2017 Email:skyseraph00@163.com 更多精彩请直接访问SkySeraph个人站点:www.sk ...
- UiAutomator2.0 - 与AccessibilityService的关联
目录 一.Android中的 Accessibility 二.UiAutomator2.0 与 AccessibilityService 三.验证与 AccessibilityService的关联 A ...
- UiAutomator2.0 - 控件实现点击操作原理
目录 一.UiObject 二.UiObject2 穿梭各大技术博客网站,每天都能看到一些的新的技术.突然感觉UiAutomator 2.0相对于现在来说已经是个很久远的东西了ε=(´ο`*))).写 ...
- Uiautomator1.0与Uiautomator2.0测试项目搭建与运行原理
Uiautomator是Android原生测试框架,可以用于白盒接口测试也可以用于UI自动化测试,Uiautomator分1.0版本与2.0版本,它们都是基于UiAutomation的测试框架,都是通 ...
- UiAutomator1.0 与 UiAutomator2.0
在使用2.0之前,对android自动化框架也做过一些了解<Android 自动化测试框架>.使用UiAutomator2.0也有一段时间,这里将1.0与2.0进行一个对比总结. Ui ...
- Toast信息框
Toast组件的功能和对话框有些相似,可是使用上更简单,使用Toast组件的目的仅仅有一个,就是在屏幕上弹出一个消息窗体告知用户某个信息,并且这个窗体没有不论什么button,经过几秒钟后就会消失.假 ...
随机推荐
- IntellIJ IDEA 配置 Maven 以及 修改 默认 Repository
转自:IntellIJ IDEA 配置 Maven 以及 修改 默认 Repository 今天将IntellIJ IDEA 关于Maven的配置总结一下,方便以后可参考. IDEA版本: Intel ...
- springboot启动关闭脚本
springboot项目jar包启动,application.properties.jar包.shell脚本.static目录(静态页面和jar包分离)在同一目录下 [start.sh] #!/bin ...
- C# call webservice方法
https://www.cnblogs.com/Fooo/p/5507153.html
- Git 操作命令
一.Git 基本配置 1.配置 命令:git config --global prop_name prop_value 如配置git用户名与邮箱: git config --global user. ...
- 【THUSC2017】【LOJ2981】如果奇迹有颜色 DP BM 打表 线性递推
题目大意 有一个 \(n\) 个点的环,你要用 \(m\) 中颜色染这 \(n\) 个点. 要求连续 \(m\) 个点的颜色不能是 $1 \sim m $ 的排列. 两种环相同当且仅当这两个环可以在旋 ...
- v-for 在 VSCode 中出现 Elements in iteration expect to have 'v-bind:key' directives.
在 VSCode 中编辑代码时,在有 v-for 的语句下面有一条红色波浪线,鼠标放上去有提示 Elements in iteration expect to have 'v-bind:key' di ...
- 配置webpack.config.js中的文件
webpack.config.js文件中,主要包括 entry:入口文件 output:出口文件 module:模块 plugins:插件 这几部分 1.基本配置 运行 webpack 这一命令可以将 ...
- ES 常用java api
java rest client 有两种: 1.Java Low Level REST Client :用于Elasticsearch的官方低层客户端.它允许通过http与Elasticsearch集 ...
- 【JS】JavaScript 指定日期增加天数
指定某个日期(字符串),增加n天后,输出日期字符串,格式:年-月-日: /** * [dateAddDays 从某个日期增加n天后的日期] * @param {[string]} dateStr [日 ...
- C语言面试题大汇总之华为面试题 Eddy整理
1.局部变量能否和全局变量重名? 答:能,局部会屏蔽全局.要用全局变量,需要使用"::" ;局部变量可以与全局变量同名,在函数内引用这个变量时,会用到同名的局部变量,而不会用到全局 ...