Android-浓缩咖啡-基于自定义对象单击listview条目
浓缩咖啡用于自动测试我的应用程序 编辑:下面你可以找到一些答案 如何(在自动浓缩咖啡测试脚本中)单击自定义对象长列表中的条目 在浓缩咖啡文档中有一个长列表示例。使用对象列表是我通常做的事情。到目前为止,尝试许多从地图到对象的步骤并没有产生好的结果 浓缩咖啡文件上说应该使用“onData”。比如说:Android-浓缩咖啡-基于自定义对象单击listview条目,android,hamcrest,android-espresso,Android,Hamcrest,Android Espresso,浓缩咖啡用于自动测试我的应用程序 编辑:下面你可以找到一些答案 如何(在自动浓缩咖啡测试脚本中)单击自定义对象长列表中的条目 在浓缩咖啡文档中有一个长列表示例。使用对象列表是我通常做的事情。到目前为止,尝试许多从地图到对象的步骤并没有产生好的结果 浓缩咖啡文件上说应该使用“onData”。比如说: onData( myObjectHasContent("my_item: 50")).perform(click()); onView(withId( R.id.selection_pos2)).ch
onData( myObjectHasContent("my_item: 50")).perform(click());
onView(withId( R.id.selection_pos2)).check(matches(withText("50")));
我的问题(我认为它们对学习社区很有帮助):
-你能为这个写一个好的匹配器吗?
-我们如何在“onData”中使用它
情况如何?在屏幕上,我有一个对象列表视图,如:
public class MyOjbect {
public String content;
public int size;
}
我用来填充填充列表的适配器是:
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;
}
}
公共类MyObject WithItemAndSizeDapter扩展了ArrayAdapter{
私人最终语境;
私有最终列表值;
...
@凌驾
公共视图getView(int位置、视图concertView、视图组父视图){
视图=空;
if(concertView!=null){
视图=(线性布局)视图;
}否则{
LayoutFlater充气器=(LayoutFlater)context.getSystemService(context.LAYOUT\u充气器\u服务);
视图=充气机。充气(R.layout.list_项,父项,false);
}
TextView itemT=(TextView)view.findViewById(R.id.item\u content);
itemT.setText(values.get(position.item));
TextView size=(TextView)view.findViewById(R.id.item\u size);
setText(“+values.get(position.size)”;
返回视图;
}
}
指定给onData()
的匹配器必须匹配所需列表视图的适配器.getItem(int)
返回的所需值
因此,在您的示例中,匹配器应该是这样的:
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 + "'");
}
};
}
带有内容的公共静态匹配器(最终字符串内容){
返回新的BoundedMatcher(MyObjectWithItemAndSize.class){
@凌驾
公共布尔匹配安全(MyObjectWithItemAndSize myObj){
返回myObj.content.equals(content);
}
@凌驾
公共无效说明(说明){
description.appendText(“带内容“+”内容“”);
}
};
}
除了前面的答案之外,我还创建了一个完整的版本,其中包含两种验证。这可能有助于您了解浓缩咖啡和定制火柴
与标准Espresso LongList示例的不同之处在于,我使用自定义对象的列表显示在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")));
匹配者是:
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 ");
}
};
}
匹配者是
最重要的一行(我一直在努力)如下//***
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 ");
}
};
}
而且很有效!!!!Pfff,花了一段时间,但还是克服了。多亏了Yash F.我今天需要使用Espresso 2测试一个带有自定义适配器的适配器View
。我最终使用了FeatureMatcher
:
private static FeatureMatcher<Product, String> withProductName(final String productName) {
return new FeatureMatcher<Product, String>(equalTo(productName), "with productName", "productName") {
@Override
protected String featureValueOf(Product actual) {
return actual.name;
}
};
}
我认为当您想要断言数据对象的特定属性时,FeatureMatcher
非常好。您可以对Listview.Tx使用setOnItemClick,这是关于使用Android Espresso进行自动测试的。我更新了我的问题,所以现在这个问题更加清楚了。另一个选项是在MyObjectWithItemAndSize
类中覆盖toString
,并与HastString
匹配,比如onData(allOf(is(instanceOf(MyObjectWithItemAndSize.class)),HastString(containssString(“40”)
@Override
public boolean equals( Object mob2) {
return( (this.item.equals( ((MyObjectWithItemAndSize) mob2).item)));
// of course, could have also a check on this.size.
}
private static FeatureMatcher<Product, String> withProductName(final String productName) {
return new FeatureMatcher<Product, String>(equalTo(productName), "with productName", "productName") {
@Override
protected String featureValueOf(Product actual) {
return actual.name;
}
};
}
onData(withProductName("My Awesome Product"))
.inAdapterView(withId(R.id.product_list))
.onChildView(withId(R.id.product_title))
.check(matches(withText("My Awesome Product")));