问题

在做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信息的验证的更多相关文章

  1. 微软企业库5.0 学习之路——第五步、介绍EntLib.Validation模块信息、验证器的实现层级及内置的各种验证器的使用方法——下篇

    一.独立验证器 我上篇中我将AndCompositeValidator和OrCompositeValidator归为独立验证器,这2个验证器主要是为了第一类验证服务,可以进行多种验证组合在一起进行复杂 ...

  2. 『与善仁』Appium基础 — 29、获取toast信息

    目录 1.toast介绍 2.toast定位 3.示例 4.封装toast判断 1.toast介绍 Android中的toast是一种简易的消息提示框,toast提示框不能被用户点击,会根据所设置的显 ...

  3. app自动化toast信息获取

    移动端测试比较常遇到toast类型的提示信息,那么对于这类信息我们要怎么获取呢,让我细细道来: 首先,获取toast信息要注意以下几点: 必须是Uiautomator2框架,设备配置参数中设置 &qu ...

  4. UiAutomator2.0升级填坑记

    UiAutomator2.0升级填坑记 SkySeraph May. 28th 2017 Email:skyseraph00@163.com 更多精彩请直接访问SkySeraph个人站点:www.sk ...

  5. UiAutomator2.0 - 与AccessibilityService的关联

    目录 一.Android中的 Accessibility 二.UiAutomator2.0 与 AccessibilityService 三.验证与 AccessibilityService的关联 A ...

  6. UiAutomator2.0 - 控件实现点击操作原理

    目录 一.UiObject 二.UiObject2 穿梭各大技术博客网站,每天都能看到一些的新的技术.突然感觉UiAutomator 2.0相对于现在来说已经是个很久远的东西了ε=(´ο`*))).写 ...

  7. Uiautomator1.0与Uiautomator2.0测试项目搭建与运行原理

    Uiautomator是Android原生测试框架,可以用于白盒接口测试也可以用于UI自动化测试,Uiautomator分1.0版本与2.0版本,它们都是基于UiAutomation的测试框架,都是通 ...

  8. UiAutomator1.0 与 UiAutomator2.0

      在使用2.0之前,对android自动化框架也做过一些了解<Android 自动化测试框架>.使用UiAutomator2.0也有一段时间,这里将1.0与2.0进行一个对比总结. Ui ...

  9. Toast信息框

    Toast组件的功能和对话框有些相似,可是使用上更简单,使用Toast组件的目的仅仅有一个,就是在屏幕上弹出一个消息窗体告知用户某个信息,并且这个窗体没有不论什么button,经过几秒钟后就会消失.假 ...

随机推荐

  1. 使用FastJson进行对象和JSON转换属性命名规则为下划线和驼峰的问题

    public class AliPayParam { @JSONField(name="out_trade_no") private String outTradeNo; @JSO ...

  2. ubuntu 16.04上 mysql 5.7 安装笔记

    一 安装 ubuntu 采用APT安装方式,可参考: Ubuntu 安装mysql和简单操作 Ubuntu 16.04安装MySQL(5.7.18) A Quick Guide to Using th ...

  3. Windows Subsystem for Linux 环境变量

    WSL(Windows Subsystem for Linux )的环境变量是包含Linux子系统和Windows系统的,测试如下: wy@WY-PC:/mnt/c/Windows/System32$ ...

  4. Codeforces 1108E2 Array and Segments (Hard version)(差分+思维)

    题目链接:Array and Segments (Hard version) 题意:给定一个长度为n的序列,m个区间,从m个区间内选择一些区间内的数都减一,使得整个序列的最大值减最小值最大. 题解:利 ...

  5. practice01

    1. 组合数公式: C(n, k) =C(n-1, k) +C(n-1, k-1) 要求利用该公式写递归函数求组合数. #include <stdio.h> int C(int a,int ...

  6. CF1153D Pigeon d'Or

    Description 给一棵树,每个点是子节点的最大值或最小值,将叶子节点填上整数,使这棵树的根最大. Solution 明显的\(dp\)题,代码很短. 分类讨论如下: 1.如果是叶子节点,\(d ...

  7. MySQL巧建sum索引帮我们提高至少100%的效率

    有两个表,表a CREATE TABLE `a` ( `id` mediumint() unsigned NOT NULL AUTO_INCREMENT, `fid` ) unsigned ', `c ...

  8. Eclipse使用JDBC小案例

    JDBC(Java Database Connectivity:Java访问数据库的解决方案)定义一套标准接口,即访问数据库的通用API,不同数据库厂商根据各自数据的特点去实现这些接口. JDBC是J ...

  9. 树莓派3B+ 安装系统

    安装概要步骤: 官网下载系统->刷入TF卡->设置开启显示器和SSH->通电->进入系统 1. 进入官方网站下载系统镜像 下载页面:https://www.raspberryp ...

  10. windows系统安装gcc编译器----c/c++语言编译器

    1.安装MinGW编译管理安装软件 官方下载:https://osdn.net/projects/mingw/releases/ 作者百度云备份下载:https://pan.baidu.com/s/1 ...