缘来是你:

  在基于UI谷歌库的测试系统对第三方APK测试例,存在不定时弹窗问题,对测试例的健壮性和稳定性产生了很大影响。

  为了解决这个问题,谷歌开源了UIwatcher 类来解决此问题。

  附谷歌官网类解析地址:http://developer-android.ir/tools/help/uiautomator/UiWatcher.html#checkForCondition%28%29

                                                                                                                        

问问你是谁:

Public Methods
abstract boolean checkForCondition()

The testing framework calls this handler method automatically when the framework is unable to find a match using the UiSelector.

即: 在UiSelector 方法找不到匹配的时候,便调用uiwatcher类的此方法。

                                                                                                                        

使用方法:

首先要定义一个类对象:示例如下    

 UiWatcher okCancelDialogWatcher = new UiWatcher() {
@Override //重写此方法
public boolean checkForCondition() {
UiObject okCancelDialog = new UiObject(new UiSelector().textStartsWith("Lorem ipsum"));
if(okCancelDialog.exists()){
Log.w(LOG_TAG, "Found the example OK/Cancel dialog");
UiObject okButton = new UiObject(new UiSelector().className("android.widget.Button").text("OK"));
try {
okButton.click();
} catch (UiObjectNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} return (okCancelDialog.waitUntilGone(25000));
}
return false;
}
};

其次若想其发挥作用,必须先要注册然后在应用之前调用,其相关函数:

    

public void registerWatcher (String name, UiWatcher watcher)

Registers a UiWatcher to run automatically when the testing framework is unable to find a match using a UiSelector. See runWatchers()

Parameters    name       to register the UIWatcher

          watcher        uiwatcher

         public void removeWatcher (String name) 

   Removes a previously registered UiWatcher. See registerWatcher(String, UiWatcher)

     Parameters     name                used to register the UIWatcher
     public void resetWatcherTriggers ()
     Resets a UiWatcher that has been triggered. If a UiWatcher runs and its checkForCondition() call returned true, then the UiWatcher is considered triggered. See                   egisterWatcher(String, UiWatcher)
 
 
   public void runWatchers ()
     This method forces all registered watchers to run. See registerWatcher(String, UiWatcher)
 

public boolean hasAnyWatcherTriggered ()

     Checks if any registered UiWatcher have triggered. See registerWatcher(String, UiWatcher) See hasWatcherTriggered(String)
 
 
     public boolean hasWatcherTriggered (String watcherName)

   Checks if a specific registered UiWatcher has triggered. See registerWatcher(String, UiWatcher). If a UiWatcher runs and its checkForCondition() call returned true, then the           UiWatcher is considered triggered. This is helpful if a watcher is detecting errors from ANR or crash dialogs and the test needs to know if a UiWatcher has been triggered.

     Returns          true if triggered else false

                                                   

实际应用Demo:

测试代码:

public void SendMessage(UiDevice dut, Bundle bundle) throws UiObjectNotFoundException, RemoteException, InterruptedException,UIAException{
int x=dut.getDisplayWidth();
int y=dut.getDisplayHeight();
Thread.sleep(3000);
UiObject SetNum = new UiObject(new UiSelector().packageName("com.p1.chompsms").resourceId("com.p1.chompsms:id/recipients_field")); //此为控件1号,刚开始是找不见的。
Thread.sleep(10000);
String phone=bundle.getString("phoneNum");
if(SetNum.exists())
SetNum.setText(phone); Thread.sleep(1000);

uiwatcher代码:

public void Game_uiwatcher(UiDevice dut, Bundle bundle) throws UiObjectNotFoundException, UIAException, InterruptedException
{
//定义
UiWatcher okCancelDialogWatcher = new UiWatcher() {
@Override
public boolean checkForCondition() { System.out.println("uiwatcher run ing....."); UiObject SetTend= new UiObject(new UiSelector().className("oandroid.widget.LinearLayout").index(1).childSelector(new UiSelector().className("android.widget.TextView").index(0))); System.out.println("uiwatcher run ing.....");
if(SetTend.exists()){ try {
SetTend.click();
} catch (UiObjectNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} return (SetTend.waitUntilGone(25000));
}
return false;
}
};
//注册
UiDevice.getInstance().registerWatcher(MYOKCANCELDIALOGWATCHER_STRING, okCancelDialogWatcher);
 
 }

uiwatcher需要在调用前注册并启用过程:

    Game_uiwatcher()方法注册并调用

      然后测试函数SendMessage()运行时方才起作用。

  

 

      

UI Watcher 解决不定时弹窗问题的更多相关文章

  1. JavaScript的案例(数据校验,js轮播图,页面定时弹窗)

    1.数据校验            步骤            1.确定事件(onsubmit)并绑定一个函数            2.书写这个函数,获取数据,并绑定id            3. ...

  2. 使用 Microsoft.UI.Xaml 解决 UWP 控件和对老版本 Windows 10 的兼容性问题

    原文 使用 Microsoft.UI.Xaml 解决 UWP 控件和对老版本 Windows 10 的兼容性问题 虽然微软宣称 Windows 10 将是最后一个 Windows 版本,但由于年代跨越 ...

  3. ubuntu下定时弹窗记录工作日志

    背景 记录工作日志,是一个很好的习惯,但不容易坚持,本来打算每天记录,但经常拖延,拖着拖着,有一些事情就忘记了. 等到写周报或月报的时候,才会开始翻邮件,聊天记录,各个仓库的提交log等,回忆都干了些 ...

  4. android4.0以上访问网络不能在主线程中进行以及在线程中操作UI的解决方法

    MONO 调用一个线程操作UI 然后报Only the original thread that created a view hierarchy can touch its views.错误 goo ...

  5. Android 之UI自适应解决方式

    1.概况 作为Android开发者,最头疼的莫过于让自己开发的程序在不同终端上面的显示效果看起来尽量一致(当然.假设要充分利用大屏幕的优势另当别论).在全球范围内来讲.android有着数以亿计的设备 ...

  6. Selenium UI自动化解决iframe定位问题

      更多原创测试技术文章同步更新到微信公众号 :三国测,敬请扫码关注个人的微信号,感谢! 原文链接:http://www.cnblogs.com/zishi/p/6735116.html 一个阴雨霏霏 ...

  7. 自动化测试系列:Selenium UI自动化解决iframe定位问题

      更多原创测试技术文章同步更新到微信公众号 :三国测,敬请扫码关注个人的微信号,感谢! 原文链接:http://www.cnblogs.com/zishi/p/6735116.html 一个阴雨霏霏 ...

  8. selenium UI自动化解决验证码的五种方法

    TesseractPy3.py #coding=utf-8 import os import subprocess import traceback import logging from PIL i ...

  9. webapi+Quartz.NET解决若干定时程序同时运行的问题

    项目现状: 有若干定时程序需要自启动运行,为了简便程序部署等问题,采取这种办法把定时程序集中管理到webapi中跟随api发布 代码架构介绍: 新建一个类库,类库引用Quartz(Quartz.2.3 ...

随机推荐

  1. Optimizing subroutine calls based on architecture level of called subroutine

    A technique is provided for generating stubs. A processing circuit receives a call to a called funct ...

  2. CiteSeer统计的计算机领域的期刊和会议的影响因子(2005)

    产生自CiterSeer 2005数据库,实际的影响因子可能更高.仅供参考使用.真实的IF还需去官网查看 . OSDI: 3.31 (top 0.08%) . USENIX Symposium on ...

  3. Filecoin:一种去中心化的存储网络(二)

    开始初步了解学习Filecoin,如下是看白皮书的内容整理. 参考: 白皮书中文版 http://chainx.org/paper/index/index/id/13.html 白皮书英文版 http ...

  4. docker基于本地模版导入创建镜像

    /* 因为直接去网站拿会下载的慢,所以直接到网站里,对着此包--〉右键--〉复制链接地址 网站地址:https://openvz.org/Download/template/precreated */ ...

  5. 高德地图web 端智能围栏

    最近有个项目,需要在web上批量给设备设置智能围栏,设备超出范围报警,查看高德地图webJS API,web端操作案例如,后台判断没有提供源码 <!-- 重点参数:iconStyle --> ...

  6. hive学习(二) hive操作

    hive   ddl 操作官方手册https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL hive  dml 操作官方手 ...

  7. git更新远程仓库代码到本地

    1 使用命令查看连接的远程的仓库 git remote -v 2 远程获取代码 git fetch origin master 如果出现 Already up-to-date 说明代码更新好了 出现 ...

  8. UVA 548.Tree-fgets()函数读入字符串+二叉树(中序+后序遍历还原二叉树)+DFS or BFS(二叉树路径最小值并且相同路径值叶子节点权值最小)

    Tree UVA - 548 题意就是多次读入两个序列,第一个是中序遍历的,第二个是后序遍历的.还原二叉树,然后从根节点走到叶子节点,找路径权值和最小的,如果有相同权值的就找叶子节点权值最小的. 最后 ...

  9. ubuntu下基于Anaconda使用Tensorflow

    为了在ubuntu下利用Anaconda使用tensorflow,但在利用conda安装tensorflow,不能在终端,spyder和notebook中直接使用,需要我们进行一定的配置. 1.安装A ...

  10. 洛谷P2224 [HNOI2001] 产品加工 [DP补完计划,背包]

    题目传送门 产品加工 题目描述 某加工厂有A.B两台机器,来加工的产品可以由其中任何一台机器完成,或者两台机器共同完成.由于受到机器性能和产品特性的限制,不同的机器加工同一产品所需的时间会不同,若同时 ...