使用Espresso是否可以简化这两个语句?

    onView(withText(expectedErrorTitle))
            .check(matches(isDisplayed()));
    onView(withText(expectedErrorMessage))
            .check(matches(isDisplayed()));


我尝试了这个,但是不起作用:

    onView(allOf(
            withText(expectedErrorTitle),
            withText(expectedErrorMessage)
    )).check(matches(isDisplayed()));

最佳答案

为什么还要简化?但是您可以检查父视图,以使子级具有预期的文本。

onView(R.id.parentLayout)
  .check(matches(allOf(
    isDisplayed(),
    withChild(withText("A")),
    withChild(withText("B"))
)));


检查是否显示了父级就足够了,或者您做了更多疯狂的事情,例如

onView(R.id.parentLayout)
  .check(matches(allOf(
    withChild(allOf(withText("A"), isDisplayed())),
    withChild(allOf(withText("B"), isDisplayed())),
)));

09-10 14:49