我想使用espresso检查某个元素是否在另一个元素的右边。如何检查。一个示例示例会有所帮助

ViewInteraction textView = onView(allOf(withId(R.id.name), withText("dín"), childAtPosition(childAtPosition(IsInstanceOf.<View>instanceOf(androidx.recyclerview.widget.RecyclerView.class), 0), 0), isDisplayed()));


文本din应该出现在左边的另一个文本的右边

最佳答案

PositionAssertions帮助您测试元素在屏幕上的相对位置(在xy平面上,z平面被忽略)。

这是来自google testing sample app的示例文档

 public void testRelativePositions() {
    // left text should be left of right button although they share 1 pixel at the boundary.
    onView(withId(R.id.left_text)).check(isLeftOf(withId(R.id.right_button)));
    // Switch length button should be above Wrap button
    onView(withId(R.id.length)).check(isAbove(withId(R.id.wrap)));
    // Switch length button and Wrap button should be aligned to the left.
     onView(withId(R.id.length)).check(isLeftAlignedWith(withId(R.id.wrap)));
  }

07-24 20:49