誰もアンドロイドUITesting
フレームワークを試していますUIAutomator
か? クラスUiScrollabl
" を使用してスクロール可能なオブジェクト内のいくつかのオブジェクトを検索すると、スクロール可能なオブジェクトの長さが長すぎる場合 (見つけるには 2 回スワイプする必要がある場合)、オブジェクトが見つかりません。 「設定」アプリ.同じ問題を抱えている人はいますか?
6 に答える
スクロールが必要なビュー内の要素を見つけるには、以下の方法を使用しましたが、うまくいくようです。
public void searchForText(String searchText)
throws UiObjectNotFoundException {
UiScrollable textScroll = null;
UiObject text = null;
if (searchText != null) {
textScroll = new UiScrollable(new UiSelector().scrollable(true));
textScroll.scrollIntoView(new UiSelector().text(searchText));
text = new UiObject(new UiSelector().text(searchText));
text.click();
}
}
私は一般に、のメソッドよりもUiDevice
のほうが運がいいです。drag()
Scrollable
私も一度だけスクロールに出会いgetChildByText
、それをなぞってみました。根本的な原因はシステムにある可能性があります。
を呼び出すとgetChildByText
、そのフローは次のようになります。
getChildByText -> scrollIntoView -> scrollForward -> scrollSwipe
では、システムは現在の画面ですべてを検索してフィルタリングするscrollSwipe
よりも、スワイプ コマンドを実行し、待機します。この手順により、UiScrollable が再度スクロールされるか、最後に確認されます。AccessibilityEvent
AccessibilityEvent.TYPE_VIEW_SCROLLED
しかしバグはここにあり、システムは正しく戻ることができませんAccessibilityEvent
。
public boolean scrollSwipe(final int downX, final int downY, final int upX, final int upY,
final int steps) {
Runnable command = new Runnable() {
@Override
public void run() {
swipe(downX, downY, upX, upY, steps);
}
};
// Collect all accessibility events generated during the swipe command and get the
// last event
ArrayList<AccessibilityEvent> events = new ArrayList<AccessibilityEvent>();
runAndWaitForEvents(command,
new EventCollectingPredicate(AccessibilityEvent.TYPE_VIEW_SCROLLED, events),
Configurator.getInstance().getScrollAcknowledgmentTimeout());
//**Root cause**
//Some times the events list size will be 0 ,
//but in fact it can scroll again.
Log.e(LOG_TAG,"events size = " + events.size());
AccessibilityEvent event = getLastMatchingEvent(events,
AccessibilityEvent.TYPE_VIEW_SCROLLED);
if (event == null) {
// end of scroll since no new scroll events received
recycleAccessibilityEvents(events);
return false;
}
...
..
.
}
MaxSearchSwipes
コードをオーバーライドして適切な を定義し、アイテムがこのサイズを超える場合はテストを失敗させるのが最善の方法だと思います。
public class UiScrollableFix extends UiScrollable {
public UiScrollableFix(UiSelector container) {
super(container);
}
@Override
public boolean scrollIntoView(UiSelector selector) throws UiObjectNotFoundException {
Tracer.trace(selector);
// if we happen to be on top of the text we want then return here
UiSelector childSelector = getSelector().childSelector(selector);
if (exists(childSelector)) {
return (true);
} else {
// we will need to reset the search from the beginning to start search
scrollToBeginning(getMaxSearchSwipes());
if (exists(childSelector)) {
return (true);
}
for (int x = 0; x < getMaxSearchSwipes(); x++) {
Log.e("test", "x=" + x);
boolean scrolled = scrollForward();
if (exists(childSelector)) {
return true;
}
//For avoiding the scroll fail.
// if (!scrolled && x!=0) {
// return false;
// }
}
}
return false;
}
}
scrollableObject.scrollIntoView(btn_1);
try {if(scrollableObject.scrollIntoView(btn_1))
btn_1.click();
}
catch (UiObjectNotFoundException e){
System.out.print("error1: "+e);
}
これが役立つかどうかはわかりませんが、これが私のアプリで行うことです。最初の行では、btn_1 が見つかるまでスクロールし、オブジェクトが見つかったらクリックします。