滑动直到元素在 Android Espresso 中可见
Swipe until element visible in Android Espresso
我有一个带有数据输入表单的长竖屏。我需要向上滑动(向下滚动)此屏幕,直到其中一个表单字段显示在屏幕上。
我尝试对位于屏幕较高位置的元素使用 swipeUp() 和 scrollTo() ViewActions,但这对我没有帮助,这些滑动后元素从屏幕上隐藏。
应该是这样的:
boolean found = false;
int i = 0;
while (!found && i < MAX_SWIPES) {
onView(withId(R.id.largeElementId)).perform(swipeUp());
SystemClock.sleep(500);
try {
uiElementToSearch.check(matches(isCompletelyDisplayed()));
found = true;
} catch (Exception e) {
// The search continues
}
i++;
}
if (!found) {
Assert.fail("The element has not been found.");
}
今天的惯用方法是使用 RepeatActionUntilViewState
。例如,以下内容将在 ID 为 largeElementId
的元素上向上滑动最多 10 次,直到用户可以看到带有文本“目标文本”的视图:
onView(withId(R.id.largeElementId)).perform(repeatedlyUntil(swipeUp(),
hasDescendant(withText("Target text")),
10));
我有一个带有数据输入表单的长竖屏。我需要向上滑动(向下滚动)此屏幕,直到其中一个表单字段显示在屏幕上。 我尝试对位于屏幕较高位置的元素使用 swipeUp() 和 scrollTo() ViewActions,但这对我没有帮助,这些滑动后元素从屏幕上隐藏。
应该是这样的:
boolean found = false;
int i = 0;
while (!found && i < MAX_SWIPES) {
onView(withId(R.id.largeElementId)).perform(swipeUp());
SystemClock.sleep(500);
try {
uiElementToSearch.check(matches(isCompletelyDisplayed()));
found = true;
} catch (Exception e) {
// The search continues
}
i++;
}
if (!found) {
Assert.fail("The element has not been found.");
}
今天的惯用方法是使用 RepeatActionUntilViewState
。例如,以下内容将在 ID 为 largeElementId
的元素上向上滑动最多 10 次,直到用户可以看到带有文本“目标文本”的视图:
onView(withId(R.id.largeElementId)).perform(repeatedlyUntil(swipeUp(),
hasDescendant(withText("Target text")),
10));