问题

在做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. 深度学习结合SLAM研究总结

    博客转载自:https://blog.csdn.net/u010821666/article/details/78793225 原文标题:深度学习结合SLAM的研究思路/成果整理之 1. 深度学习跟S ...

  2. hdu-4612(无向图缩点+树的直径)

    题意:给你n个点和m条边的无向图,问你如果多加一条边的话,那么这个图最少的桥是什么 解题思路:无向图缩点和树的直径,用并查集缩点: #include<iostream> #include& ...

  3. Nginx Http 过滤模块

    L69 执行顺序在content阶段后 log阶段前调用的 也就是处理完用户业务后 准备记录处理日志之前 我们可以到nginx http_model.c里查看 数组 执行顺序从下至上顺序执行 copy ...

  4. 接口测试(jmeter和postman 接口使用)

    接口测试基础知识 接口测试主要用于检测外部系统与系统之间以及内部各个子系统之间的交互点.把前端(client)和后端(server)联系起来,测试的重点是要检查数据的交换,传递和控制管理过程,以及系统 ...

  5. 树莓派3B+(一)

    第一步:安装raspbian系统 介绍:Raspbian是为树莓派设计,基于Debian的操作系统,由一个小团队开发.其不隶属于树莓派基金会,但被列为官方支持的操作系统. 下载地址:https://w ...

  6. leanote 信息栏显示笔记本和笔记类型

    本文解决如下两个问题: 1. 在列表视图下使用搜索时,不知道搜出来的笔记属于哪个笔记本.(摘要视图下是有显示的) 2. 增加显示笔记类型(markdown 或 富文本) 修改resources\app ...

  7. MySql自动备份shell

    MySql黑屏备份是每个运维工程师必备的技能,以下是MySQL自动备份脚本: #/bin/bash#This is mysql backup shell on 2019/4/28 BAKUP_DIR= ...

  8. CSS上下左右居中的几种方法

    1.absolute,margin: auto .container { position: relative; } .content { position: absolute; margin: au ...

  9. redisson整合spring

    转: redisson整合spring 转: 原文:http://blog.csdn.net/wang_keng/article/details/73549274 首先讲下什么是Redisson:Re ...

  10. html中设置锚点定位

    1.使用id定位: (这样的定位可以针对任何标签来定位. ) <a name="1F" href="#1F">锚点1</a> <d ...