码迷,mamicode.com
首页 > 其他好文 > 详细

UiAutomator2.0 - Toast信息的验证

时间:2018-12-20 14:28:52      阅读:296      评论:0      收藏:0      [点我收藏+]

标签:tac   tps   .text   tin   src   listen   event   adb shell   art   

目录

问题:在做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信息的验证

标签:tac   tps   .text   tin   src   listen   event   adb shell   art   

原文地址:https://www.cnblogs.com/zeo-to-one/p/10143370.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!