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

 

posted on 2015-06-09 18:09  发卡  阅读(1403)  评论(0编辑  收藏  举报