zoukankan      html  css  js  c++  java
  • UI Watcher 解决不定时弹窗问题

    缘来是你:

      在基于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类的此方法。

                                                                                                                            

    使用方法:

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

     1 UiWatcher okCancelDialogWatcher = new UiWatcher() {
     2             @Override   //重写此方法
     3             public boolean checkForCondition() {
     4                 UiObject okCancelDialog = new UiObject(new UiSelector().textStartsWith("Lorem ipsum"));
     5                 if(okCancelDialog.exists()){
     6                     Log.w(LOG_TAG, "Found the example OK/Cancel dialog");
     7                     UiObject okButton = new UiObject(new UiSelector().className("android.widget.Button").text("OK"));
     8                     try {
     9                         okButton.click();
    10                     } catch (UiObjectNotFoundException e) {
    11                         // TODO Auto-generated catch block
    12                         e.printStackTrace();
    13                     }
    14                     
    15                     return (okCancelDialog.waitUntilGone(25000));
    16                 }
    17                 return false;
    18             }
    19         };

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

        

             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()运行时方才起作用。

      

     

          

  • 相关阅读:
    雨林木风操作系统有感
    Bitcoin P2P 虚拟货币原理详解
    SVN switch 用法详解
    Bitcoin P2P 货币:有史以来最危险的项目
    回旋线科普
    Mathematica 如何绘制双纵坐标轴的图像?
    c#数据库操作
    在自定义HttpHandler中无法使用Session
    VS2010安装因net framework4.0无法安装而失败的解决方法
    .net framework 4.0 0xc8000247错误解决
  • 原文地址:https://www.cnblogs.com/udld/p/4330848.html
Copyright © 2011-2022 走看看