首页 > 解决方案 > 如何检查一个元素是否在android 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 应该出现在左边的另一个文本的右边

标签: androidandroid-espresso

解决方案


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

这是取自谷歌测试示例应用程序的示例文档

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

推荐阅读