Hi. How to create Espresso DataMatcher Selector for Appium?
I have an app with ListView which is populated by ArrayAdapter. The item is a Cat.class. I want to click on element with text “Barsik” (in case if it is invisible).
public class MainActivity extends ListActivity {
private static final List<Cat> cats = new ArrayList<Cat>();
static {
cats.add(new Cat("Vaska", "kote"));
cats.add(new Cat("Murzik", "kotyara"));
cats.add(new Cat("Murka", "коshka"));
cats.add(new Cat("Barsik", "kotik"));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayAdapter<Cat> adapter = new CatAdapter(this);
setListAdapter(adapter);
}
private static class Cat {
public final String name;
public final String gender;
public Cat(String name, String gender) {
this.name = name;
this.gender = gender;
}
}
private class CatAdapter extends ArrayAdapter<Cat> {
public CatAdapter(Context context) {
super(context, android.R.layout.simple_list_item_2, cats);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Cat cat = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext())
.inflate(android.R.layout.simple_list_item_2, null);
}
((TextView) convertView.findViewById(android.R.id.text1))
.setText(cat.name);
((TextView) convertView.findViewById(android.R.id.text2))
.setText(cat.gender);
return convertView;
}
}
}
I tried with next method to click and it throws “org.openqa.selenium.InvalidSelectorException: Not a valid selector”:
public static void findElementInSimpleList(WebDriver driver) {
WebElement list = driver.findElement(MobileBy.id("android:id/list"));
String selector = new Json().toJson(
ImmutableMap.of(
"name", "contains",
"args", ImmutableList.of(
ImmutableMap.of(
"name", "isA",
"args", "String.class"
),
ImmutableMap.of(
"name", "containsString",
"args", "Barsik"
)
)));
list.findElement(MobileBy.androidDataMatcher(selector)).click();
}