Android无线测试之—UiAutomator UiObject API介绍二

点击与长按

一、组件区域位置关系

  Rect 对象代表一个矩形区域 [Left,Top] [Right,Bottom]

技术分享

二、点击与长按API

返回值 API 描述
boolean click() 点击对象
boolean clickAndWaitForNewWindow(lang timeout) 点击对象,等待新窗口出现,参数为等待超长时间
boolean clickAndWaitForNewWindow() 点击对象,等待新窗口出现
boolean clickBottomRight() 点击对象的右下角
boolean clickTopLeft() 点击对象的左上角
boolean longClick() 长按对象,对对象执行长按操作
boolean longClickBottomRight() 长按对象右下角
boolean longClickTopLeft() 长按对象左上角

 

 

 

 

 

 

 

 

三、API应用举例

技术分享
package com.test.uiobject;

import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;

public class Demo extends UiAutomatorTestCase {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String jarName,testClass,testName,androidId;
        jarName="demo";
        testClass="com.test.uiobject.Demo";
        testName="testLongClick";
        androidId="1";
        new UiAutomatorHelper(jarName,testClass,testName,androidId);

    }
    
    public void testClick() throws UiObjectNotFoundException{
        UiDevice.getInstance().pressHome();
        sleep(1000);
        
        //click()
        UiObject clock=new UiObject(new UiSelector().resourceId("com.android.deskclock:id/analog_appwidget"));
        clock.click();
        sleep(2000);
        
        UiDevice.getInstance().pressBack();
        sleep(2000);
        
        //clickAndWaitForNewWindow()
        clock.clickAndWaitForNewWindow();
        
        UiDevice.getInstance().pressBack();
        sleep(2000);
        
        //clickAndWaitForNewWindow(timeout)
        clock.clickAndWaitForNewWindow(5000);
        
        UiDevice.getInstance().pressBack();
        sleep(2000);
        
        //clickBottomRight()
        clock.clickBottomRight();
        sleep(2000);
        
        UiDevice.getInstance().pressBack();
        sleep(2000);
        
        //clickTopLeft()
        clock.clickTopLeft();
        sleep(2000);
        UiDevice.getInstance().pressBack();
        
    }
    
    public void testLongClick() throws UiObjectNotFoundException{
        UiDevice.getInstance().pressHome();
        sleep(2000);
        
        //longClick()
        UiObject people=new UiObject(new UiSelector().text("People"));
        people.longClick();
        sleep(2000);
        
        //longClickBottomRight()
        people.longClickBottomRight();
        sleep(2000);
        
        //longClickTopLeft()
        people.longClickTopLeft();
    }

}
Demo.java

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。