简体   繁体   English

滑动直到元素在 Android Espresso 中可见

[英]Swipe until element visible in Android Espresso

I have a long vertical screen with data entry form.我有一个带有数据输入表单的长垂直屏幕。 I need swipeUp (scroll down) this screen until one of form's field is displayed on screen.我需要 swipeUp (向下滚动)此屏幕,直到屏幕上显示表单的字段之一。 I try use swipeUp() and scrollTo() ViewActions on the elements located higher on the screen, but it didn't help me, that these elements hiding from screen after swiping.我尝试在位于屏幕较高位置的元素上使用swipeUp()scrollTo() ViewActions,但它没有帮助我,这些元素在滑动后隐藏在屏幕上。

The idiomatic approach today would be to use RepeatActionUntilViewState .今天惯用的方法是使用RepeatActionUntilViewState For example, the following would swipe up on an element with ID largeElementId up to 10 times until a view with text "Target text" is visible to the user:例如,以下内容将在 ID 为largeElementId的元素上向上滑动最多 10 次,直到用户可以看到带有文本“目标文本”的视图:

onView(withId(R.id.largeElementId)).perform(repeatedlyUntil(swipeUp(), 
        hasDescendant(withText("Target text")), 
        10));

It should be something like this:它应该是这样的:

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.");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM