手机自动化测试:appium源码分析之bootstrap十二
手機自動化測試:appium源碼分析之bootstrap十二
?
? 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的對象,判斷其是否存在然后返回相應結果給客戶端。
轉載于:https://www.cnblogs.com/poptest/p/4950625.html
總結
以上是生活随笔為你收集整理的手机自动化测试:appium源码分析之bootstrap十二的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 海南碧桂园宝岛上城楼盘地址在哪里?
- 下一篇: 技术分享(持续更新)