# ScrollTo
~~~
package io.appium.android.bootstrap.handler;
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 io.appium.android.bootstrap.*;
import org.json.JSONException;
import java.util.Hashtable;
/**
* This handler is used to scroll to elements in the Android UI.
*
* Based on the element Id of the scrollable, scroll to the object with the
* text.
*
*/
public class ScrollTo extends CommandHandler {
/*
* @param command The {@link AndroidCommand}
*
* @return {@link AndroidCommandResult}
*
* @throws JSONException
*
* @see io.appium.android.bootstrap.CommandHandler#execute(io.appium.android.
* bootstrap.AndroidCommand)
*/
@Override
public AndroidCommandResult execute(final AndroidCommand command)
throws JSONException {
if (!command.isElementCommand()) {
return getErrorResult("A scrollable view is required for this command.");
}
try {
Boolean result;
final Hashtable<String, Object> params = command.params();
final String text = params.get("text").toString();
final String direction = params.get("direction").toString();
final AndroidElement el = command.getElement();
if (!el.getUiObject().isScrollable()) {
return getErrorResult("The provided view is not scrollable.");
}
final UiScrollable view = new UiScrollable(el.getUiObject().getSelector());
if (direction.toLowerCase().contentEquals("horizontal")
|| view.getClassName().contentEquals(
"android.widget.HorizontalScrollView")) {
view.setAsHorizontalList();
}
view.scrollToBeginning(100);
view.setMaxSearchSwipes(100);
result = view.scrollTextIntoView(text);
view.waitForExists(5000);
// make sure we can get to the item
UiObject listViewItem = view.getChildByInstance(
new UiSelector().text(text), 0);
// We need to make sure that the item exists (visible)
if (!(result && listViewItem.exists())) {
return getErrorResult("Could not scroll element into view: " + text);
}
return getSuccessResult(result);
} catch (final UiObjectNotFoundException e) {
return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT, e.getMessage());
} catch (final NullPointerException e) { // el is null
return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT, e.getMessage());
} catch (final Exception e) {
return new AndroidCommandResult(WDStatus.UNKNOWN_ERROR, e.getMessage());
}
}
}
~~~
在uiautomator中有時候需要在一個滾動的list中找到某一個item,而這個item的位置又不定,這個時候我們可以通過UiScrollable的scrollTo來找到特定text的控件。而bootstrap的這個ScrollTo就是封裝這樣一種需求的。
首先判斷控件是否是可以滾動的,然后創建UiScrollable對象,因為默認的滾動方式是垂直方向的,如果需要的是水平方向的的話,還要設置方向為水平。
~~~
view.setAsHorizontalList();
~~~
然后將滾動控件滾到最開始的地方,然后設置最大的搜索范圍為100次,因為不可能永遠搜索下去。然后開始調用
~~~
view.scrollTextIntoView(text);
~~~
開始滾動,最后確認是否滾動到制定目標:
~~~
view.waitForExists(5000);
~~~
因為有可能有刷新到時間,所以調用方法到時候傳入了時間5秒鐘。
~~~
UiObject listViewItem = view.getChildByInstance(
new UiSelector().text(text), 0);
~~~
最后檢查結果,獲取制定text的對象,判斷其是否存在然后返回相應結果給客戶端。
- 前言
- appium框架之bootstrap
- bootstrap之Click事件
- bootstrap之WaitForIdle&&Clear
- bootstrap之Orientation
- bootstrap之Swipe
- bootstrap之Flick
- bootstrap之Drag
- bootstrap之Pinch
- bootstrap之鼠標操作
- bootstrap之文本框的操作
- bootstrap之GetName&&GetAttribute&&GetDeviceSize&&GetSize&&GetLocation&&GetDataDir
- bootstrap之ScrollTo
- bootstrap之Wake&&PressBack&&TakeScreenshot&&OpenNotification
- bootstrap之PressKeyCode&&LongPressKeyCode
- bootstrap之DumpWindowHierarchy
- bootstrap之UpdateStrings
- bootstrap之MultiPointerGesture