引用:极客学院 Android UiAutomator UiScrollable
一. UiScrollable类说明
参考官方文档:UiScrollable
中国大陆正常是无法访问的,可参考Android官网访问设置:通过Host设置访问Android官网
二. 环境说明
IDE: Android Studio 2.3.1
JAVA: JDK1.8.0_121
测试框架:Junit4
Android:Android SDK
三. 快速滚动
1. 滚动相关概念
1) 步长:控制扫动速度
2) 扫动次数
实例:
// flingBackward(), flingForward(),
//flingToBeginning(int maxSwipes), flingToEnd(int maxSwipes)
@Test
public void testFling throws UiObjectNotFoundException {
UiScrollable scroll=new UiScrollable(new UiSelector().className("android.widget.ListView"));
//scroll.flingBackward();
//scroll.flingForward();
//scroll.flingToBegin(20);
//scroll.flingToEnd(20);
}
四、获取列表子元素
返回值 | API |
---|---|
UiObject | getChildByDescription(UiSelector childPattern, String text) |
UiObject | getChildByDescription(UiSelector childPattern, String text, boolean allowScrollSearch) |
UiObject | getChildByInstance(UiSelector childPattern, int instance) |
UiObject | getChildByText(UiSelector childPattern, String text, boolean allowScrollSearch) |
UiObject | getChildByText(UiSelector childPattern, String text) |
实例:
@Test
public void testScroll() throws RemoteException, UiObjectNotFoundException {
UiScrollable scrollable=new UiScrollable(new UiSelector().className("android.widget.ListView"));
// getChildByText
UiObject object=scrollable.getChildByText(new UiSelector().className("android.widget.TextView"), "Views", true);
object.click();
// getChildByInstance
scrollable.getChildByInstance(new UiSelector().className("android.widget.TextView"), 3).click();
}
五. 获取与设置最大滚动次数常量值
返回值 | API |
---|---|
int | getMaxSearchSwipes() |
UiScrollable | setMaxSearchSwipes(int swipes) |
实例:
@Test
public void testSwipCount() throws UiObjectNotFoundException {
UiScrollable scroll=new UiScrollable(new UiSelector().className("android.widget.TextView"));
UiObject textView=new UiObject(new UiSelector().text("Views"));
scroll.scrollIntoView(textView);
scroll.setMaxSearchSwipes(3);
textView.click();
System.out.println("maxswipes: "+scroll.getMaxSearchSwipes());
}
六. 滑动区域校准常量设置与获取
返回值 | API |
---|---|
double | getSwipeDeadZonePercentage() |
UiScrollable | setSwipeDeadZonePercentage(double swipeDeadZonePercentage) |
实例:
@Test
public void testDeadZoon() throws UiObjectNotFoundException, RemoteException {
UiScrollable scroll=new UiScrollable(new UiSelector().className("android.widget.ListView"));
scroll.setSwipeDeadZonePercentage(0.1);
scroll.scrollForward(2);
}
七. 向前与向后滚动
返回值 | API |
---|---|
boolean | scrollBackward(int steps) |
boolean | scrollBackward() |
boolean | scrollForward(int steps) |
boolean | scrollForward() |
八. 滚动到某个对象
返回值 | API |
---|---|
boolean | scrollIntoView(UiSelector selector) |
boolean | scrollIntoView(UiObject obj) |
boolean | scrollTextIntoView(String text) |
boolean | scrollToBeginning(int maxSwipes) |
boolean | scrollToBeginning(int maxSwipes, int steps) |
boolean | scrollToEnd(int maxSwipes) |
boolean | scrollToEnd(int maxSwipes, int steps) |
实例:
@Test
public void testScrollIntoView() throws UiObjectNotFoundException {
UiScrollable scroll=new UiScrollable(new UiSelector().className("android.widget.ListView"));
UiSelector selector=new UiSelector().text("Lists");
scroll.setMaxSearchSwipes(1);
scroll.scrollIntoView(selector);
}
九. 设置滚动方向
返回值 | API |
---|---|
UiScrollable | setAsHorizontalList() |
UiScrollable | setAsVerticalList() |
实例:
@Test
public void testScrollIntoView() throws UiObjectNotFoundException {
UiScrollable scroll=new UiScrollable(new UiSelector().className("android.widget.ListView"));
scroll.setAsHorizontalList();
scroll.scrollForward();
}