Issue
I'm writing Espresso tests for a navigation drawer that has a long, dynamic list of entries. I'd like to match a navDrawer menu item by Text and not position number. Looking at Google's DataAdapterSample, I would expect I could use this to get a match:
@Test
public void myTest() {
openDrawer();
onRow("Sign In").check(matches(isCompletelyDisplayed()));
}
private static DataInteraction onRow(String str) {
return onData(hasEntry(equalTo("module_name"), is(str)));
}
I'm not getting a match. But in the log I can see what I'm looking for. I get
No data found matching: map containing ["module_name"->is "Sign In"]
contained values: <[
Data: Row 0: {_id:"0", module_name:"Applications", module_secure:"false", headerCollapsible:1, } (class: android.database.MatrixCursor) token: 0,
Data: Row 1: {_id:"1", module_name:"Sign In", module_lock:"false", module_right_text:null, } (class: android.database.MatrixCursor) token: 1,
...
Solution
I think the hasEntry() works only for Maps, and it seems to me that items in your navigation drawer are not Maps but rather MatrixCursors.
Just replace the Person class in the example with MatrixCursor class.
For example something like this:
private static DataInteraction onRow(final String str) {
return onData(new BoundedMatcher<Object, MatrixCursor>(MatrixCursor.class) {
@Override
public void describeTo(Description description) {
description.appendText("Matching to MatrixCursor");
}
@Override
protected boolean matchesSafely(MatrixCursor cursor) {
return str.equals(cursor.getString(1));
}
});
}
Here I assume that second column of the cursor contains the text we need to match to. I am assuming this based on the "No data found matching.." error message.
Answered By - ahanhine
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.