gpt4 book ai didi

android - 如何使用 Espresso 匹配此表格单元格?

转载 作者:太空狗 更新时间:2023-10-29 16:16:21 25 4
gpt4 key购买 nike

我需要匹配由红色矩形突出显示的 View 。我应该为它写哪个 Espresso 表达式?

enter image description here

这是一个表格布局,所有的单元格都是TextView的实例。单元格 View 没有唯一 ID。感兴趣的 View 内部可能有也可能没有文本。我所知道的是,此 View 始终位于“食物组”单元格下方。

欢迎提供任何线索。

最佳答案

这是我会为您编写的测试。

public void testCellBelowFoodGroup() {
getActivity();
onView(
allOf(
isDescendantOfA(isAssignableFrom(TableLayout.class)),
isInRowBelow(withText("Food Group")),
hasChildPosition(0)
)
).check(matches(withText("TEXT TO BE FOUND")));
}

因此,我们正在给定 TableLayout 中寻找一个 View ,它位于“食物组”文本下方的一行中,并且是该行最左边的元素。然后我们可以用那个 View 做任何我们想做的事,例如检查其文本。

isInRowBelowhasChildPosition 不是由 Espresso 提供的,它们是自定义方法,在使用 Espresso 进行测试时通常如此:我们鼓励您创建自己的 View 断言和 View 匹配器。

这里是实现。

static Matcher<View> isInRowBelow(final Matcher<View> viewInRowAbove) {
checkNotNull(viewInRowAbove);
return new TypeSafeMatcher<View>(){

@Override
public void describeTo(Description description) {
description.appendText("is below a: ");
viewInRowAbove.describeTo(description);
}

@Override
public boolean matchesSafely(View view) {
// Find the current row
ViewParent viewParent = view.getParent();
if (!(viewParent instanceof TableRow)) {
return false;
}
TableRow currentRow = (TableRow) viewParent;
// Find the row above
TableLayout table = (TableLayout) currentRow.getParent();
int currentRowIndex = table.indexOfChild(currentRow);
if (currentRowIndex < 1) {
return false;
}
TableRow rowAbove = (TableRow) table.getChildAt(currentRowIndex - 1);
// Does the row above contains at least one view that matches viewInRowAbove?
for(int i = 0 ; i < rowAbove.getChildCount() ; i++) {
if (viewInRowAbove.matches(rowAbove.getChildAt(i))) {
return true;
}
}
return false;
}};
}

static Matcher<View> hasChildPosition(final int i) {
return new TypeSafeMatcher<View>(){

@Override
public void describeTo(Description description) {
description.appendText("is child #" + i);
}

@Override
public boolean matchesSafely(View view) {
ViewParent viewParent = view.getParent();
if (!(viewParent instanceof ViewGroup)) {
return false;
}
ViewGroup viewGroup = (ViewGroup) viewParent;
return (viewGroup.indexOfChild(view) == i);
}};
}

完整的源代码可以从https://github.com/acontal/android-stackoverflow-espresso-match_table_cell下载。

关于android - 如何使用 Espresso 匹配此表格单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25975564/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com