Android无线测试之—UiAutomator UiScrollable API介绍七

滑动到某个对象

一、滑动到某个对象相关API

返回值 API 描述
boolean scrollIntoView(UiSelector selector) 滑动到条件元素所在位置,并且尽量让其居于屏幕中央
boolean scrollIntoView(UiObject obj) 滑动到对象所在位置,并且尽量让其居于屏幕中央
boolean scrollTextIntoView(String text) 滑动到文本对象所在位置,并且尽量让其居于屏幕中央
boolean scrollDescriptionIntoView(String text) 滑动到文本描述对象所在位置,并且尽量让其居于屏幕中央
boolean scrollToBeginning(int maxSwipes) 自定义扫动次数,滑动到开始位置
boolean scrollToBeginning(int maxSwipes, int steps) 自定义扫动次数与步长,滑动到开始位置
boolean scrollToEnd(int maxSwipes) 自定义扫动次数,滑动到结束位置
boolean scrollToEnd(int maxSwipes, int steps) 自定义扫动次数与步长,滑动到结束位置

 

 

 

 

 

 

 

 

说明:所有函数都是先在当前界面查找是否有要找的对象,没找到则先向上滑动指定的扫动次数,然后再向下滑动的同时查找有没有这个对象,但也仅向下滑动指定的扫动次数,找不到则会报错。 

二、相关API应用举例

package com.testuiselector;

import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiScrollable;
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="demo2";
        testClass="com.testuiselector.Demo";
        testName="testScrollIntoView";
        androidId="1";
        new UiAutomatorHelper(jarName, testClass, testName, androidId);

    }
    
    public void testScrollIntoView() throws UiObjectNotFoundException{
        UiDevice.getInstance().pressHome();
        sleep(1000);
        
        UiObject people=new UiObject(new UiSelector().text("People"));
        people.click();
        sleep(2000);
        
        UiScrollable scroll=new UiScrollable(new UiSelector().className("android.widget.ListView"));
        
        scroll.scrollToBeginning(50);
        sleep(3000);
        
        UiSelector testview=new UiSelector().text("test");
        scroll.scrollIntoView(testview);
        sleep(3000);
        
        scroll.scrollToBeginning(50, 5);
        sleep(3000);
        
        UiObject test=new UiObject(testview);
        scroll.scrollIntoView(test);
        sleep(3000);
        
        scroll.scrollToEnd(50);
        sleep(3000);
        
        scroll.scrollTextIntoView("test");
        sleep(3000);
        
        scroll.scrollToEnd(50,5);
        sleep(3000);
        
        scroll.scrollDescriptionIntoView("Quick contact for test");
        
    }
    
}
Demo.java

 

posted on 2015-06-24 16:14  发卡  阅读(2808)  评论(0编辑  收藏  举报