Android - espresso - 根据自定义对象单击listview条目

tm1*_*701 11 android hamcrest android-espresso

Espresso用于自动测试我的应用程序.

编辑:下面你找到了许多答案!

如何在一长串自定义对象中的条目上单击(在自动Espresso测试脚本中)?

在Espresso文档中,有一个LongList示例.我通常使用对象列表.到目前为止,尝试从Map到Object的许多选项都没有产生好的结果.

Espresso文档说应该使用'onData'.所以,像:

onData( myObjectHasContent("my_item: 50")).perform(click());
onView(withId( R.id.selection_pos2)).check(matches(withText("50")));
Run Code Online (Sandbox Code Playgroud)

我的问题(我认为它们对学习社区有帮助): - 你能为此写一个好的匹配器吗? - 我们如何在'onData'中使用它?

现在是什么状况?在屏幕上,我有一个对象列表视图,如:

public class MyOjbect { 
    public String content; 
    public int    size; 
}
Run Code Online (Sandbox Code Playgroud)

我用来填充填充列表的适配器是:

public class MyObjectWithItemAndSizeAdapter extends ArrayAdapter<MyObjectWithItemAndSize> {
    private final Context context;
    private final List<MyObjectWithItemAndSize> values;
    ...
    @Override
    public View getView(int position, View concertView, ViewGroup parent) {
        View view = null;
        if (concertView != null) {
            view = (LinearLayout) concertView;
        } else {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate( R.layout.list_item, parent, false);
        } 
        TextView itemT = (TextView) view.findViewById( R.id.item_content);
        itemT.setText( values.get(position).item);
        TextView sizeT = (TextView) view.findViewById( R.id.item_size);
        sizeT.setText( "" + values.get(position).size);
        return view;
    }
 }
Run Code Online (Sandbox Code Playgroud)

haf*_*fax 21

给予匹配onData()必须返回所需的值相匹配Adapter.getItem(int)的期望ListView.

所以在你的例子中,匹配器应该是这样的:

public static Matcher<Object> withContent(final String content) {
    return new BoundedMatcher<Object, MyObjectWithItemAndSize>(MyObjectWithItemAndSize.class) {
        @Override
        public boolean matchesSafely(MyObjectWithItemAndSize myObj) {
            return myObj.content.equals(content);
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("with content '" + content + "'");
        }
    };
}
Run Code Online (Sandbox Code Playgroud)


tm1*_*701 8

作为前一个答案的补充,我创建了一个包含2种验证的完整版本.这可以帮助您了解Espresso和Custom Matchers.

与标准Espresso LongList示例的不同之处在于我使用Custom对象列表在listview中显示.滚动到右侧列表条目并检查结果如下所示.

方法1 - 对字符串的验证

在测试脚本中是:

onData( allOf( instanceOf( MyObjectWithItemAndSize.class), myCustomObjectShouldHaveString( "my_item: 60")))
         .perform(click());
// testing the result ... as in the longlist example
onView(withId(R.id.selection_pos2)).check(matches(withText("my_item: 60"))); 
Run Code Online (Sandbox Code Playgroud)

匹配器是:

public static Matcher<Object> myCustomObjectShouldHaveString( String expectedTest) {
    return myCustomObjectShouldHaveString( equalTo( expectedTest));
}
private static Matcher<Object> myCustomObjectShouldHaveString(final Matcher<String> expectedObject) {
return new BoundedMatcher<Object, MyObjectWithItemAndSize>( MyObjectWithItemAndSize.class) {
    @Override
    public boolean matchesSafely(final MyObjectWithItemAndSize actualObject) {
        // next line is important ... requiring a String having an "equals" method
        if( expectedObject.matches( actualObject.item) ) {
             return true;
           } else { 
             return false;
           }
      }
      @Override
      public void describeTo(final Description description) {
         // could be improved, of course
         description.appendText("getnumber should return ");
      }
   };
}
Run Code Online (Sandbox Code Playgroud)

方法2:验证(完整对象).

在测试脚本中,您会发现:

MyObjectWithItemAndSize myObject = new MyObjectWithItemAndSize( "my_item: 60", 11);
onData( allOf( instanceOf( MyObjectWithItemAndSize.class), myObjectHasContent( myObject))).perform( click());
onView(withId( R.id.selection_pos2)).check(matches(withText("my_item: 60"))); 
Run Code Online (Sandbox Code Playgroud)

匹配器是.

最重要的一句话(我一直在努力)低于//****

public static Matcher<Object> myObjectHasContent( MyObjectWithItemAndSize expectedObject) {
   return myObjectHasContent( equalTo( expectedObject));
}
//private method that does the work of matching
private static Matcher<Object> myObjectHasContent(final Matcher<MyObjectWithItemAndSize> expectedObject) {
     return new BoundedMatcher<Object, MyObjectWithItemAndSize>(MyObjectWithItemAndSize.class) {
        @Override
        public boolean matchesSafely( final MyObjectWithItemAndSize actualObject) {
            // ****** ... the 'matches'. See below. 
            // this requires the MyObjectWithItemAndSize to have an 'equals' method
            if( expectedObject.matches( actualObject) ) {
                return true;
            } else { 
                return false;
            }
        }
        @Override
        public void describeTo(final Description description) {
           description.appendText("getnumber should return ");
        }
     };
  }
Run Code Online (Sandbox Code Playgroud)

非常重要的是Custom对象有这个方法(我猜想覆盖):

@Override
public boolean equals( Object mob2) {
    return( (this.item.equals( ((MyObjectWithItemAndSize) mob2).item)));
    // of course, could have also a check on this.size.
} 
Run Code Online (Sandbox Code Playgroud)

它的工作原理!!!! Pfff,花了一段时间,但克服了.还要感谢Yash F.