標籤:
poptest是國內唯一一家培養測試開發工程師的培訓機構,以學員能勝任自動化測試,效能測試,測試載入器開發等工作為目標。如果對課程感興趣,請大家諮詢qq:908821478。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十二