滑動到某個對象
一、滑動到某個對象相關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"); } }
