如何让 UI Automator 等到 TextView 的文本值改变

How to make UI Automator wait until TextView's text value changes

我目前正在进行一些跨应用程序功能 UI 测试。

存在一个 TextView,我必须测试它的文本值。因此,我必须找到一种方法来等待 TextView 的文本值发生变化。

这是我的用例:

final UiSelector contextualInformation = new UiSelector().resourceId(resourceId);
final UiObject contextualInformationUiObject = mDevice.findObject(contextualInformation);

// I would like to find something to fulfill this statement
Boolean conditionWasMet = contextualInformationUiObject.wait(Until./*a search condition for text to contain given substring*/, timeout);

assertThat(conditionWasMet, is(notNullValue()));
assertThat(conditionWasMet, is(true));
assertThat(contextualInformationUiObject.exists(), is(true));
assertThat(contextualInformationUiObject.getText(), containsString(/*given substring*/));

我很清楚 Until.textContains(/*substring*/) 的存在,但我不知道如何在这里应用它 to/with contextualInformationcontextualInformationUiObject

据我所知,如果你的进程没有任何onFinished回调,那没办法只能使用Thread.sleep()。这是我在最新项目中所做的:

final long waitingTime = 5000L;
Thread.sleep(averageWaitingTime);

您只需调整waitingTime即可。这个想法是为您的流程争取足够的时间来完成。

无法使用 UiObject 等待文本,只能使用 UiObject2。要等待短信,您可以使用:

fun waitElement(expectedText: String): UiObject2 {
    val view = device.wait(Until.hasObject(By.text(expectedText)), TIMEOUT)

    if (view == null) {
        throw Exception(
                "After waiting for ${TimeUnit.MILLISECONDS.toSeconds(TIMEOUT)} seconds, " +
                "the text $expectedText was not found"
        )
    }
    else {
        return view
    }
}