Testing TableLayout/TableRow with Espresso

1,013 views
Skip to first unread message

Ewan

unread,
Oct 21, 2015, 11:11:04 AM10/21/15
to Android Testing Support Library
Hi,

I wondered if anyone has ideas about using Espresso on tables. The views in each TableRow may well have shared ids; so if, for instance, you wanted to test the string in a TextView in the third column of the first row of the table, I don't think there's an easy way to do this. The TableRows aren't views themselves, is there another approach than to tag each view in the table with a composite object which encapsulated row and column info and then write appropriate Matcher<View> instances to match given 'cells' in the table (yuk..)?

Any thoughts?
Cheers
Ewan

Jose Alcérreca

unread,
Oct 21, 2015, 12:12:08 PM10/21/15
to Ewan, Android Testing Support Library


Jose Alcérreca | Developer Programs Engineer | ja...@google.com | Google+


--
You received this message because you are subscribed to the Google Groups "Android Testing Support Library" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-testing-suppo...@googlegroups.com.
To post to this group, send email to android-testing...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-testing-support-library/7eeb2422-b2f6-41d3-9ff6-2ad1cf5df056%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ewan

unread,
Oct 21, 2015, 2:21:40 PM10/21/15
to Android Testing Support Library
Hi Jose,

That's great, many thanks for help ! The new atPositionInTable matcher seems to be working fine, as below :)

Cheers, Ewan

@Test
public void testAtPositionInTableMatcher() {
    onView(atPositionInTable(0,0)).check(matches(withText("0,0")));
    onView(atPositionInTable(0,1)).check(matches(withText("0,1")));
    onView(atPositionInTable(1,0)).check(matches(withText("1,0")));
    onView(atPositionInTable(1,1)).check(matches(withText("1,1")));
    onView(atPositionInTable(1,2)).check(doesNotExist());
}

static Matcher<View> atPositionInTable(final int x, final int y) {
    return new TypeSafeMatcher<View>(){

        @Override
        public void describeTo(Description description) {
            description.appendText("is at position # " + x + " , " + y);
        }

        @Override
        public boolean matchesSafely(View view) {
            ViewParent viewParent = view.getParent();
            if (!(viewParent instanceof TableRow)) {
                return false;
            }
            TableRow row = (TableRow) viewParent;
            TableLayout table = (TableLayout) row.getParent();
            if (table.indexOfChild(row) != y)
                return false;
            if (row.indexOfChild(view) == x)
                return true;
            else
                return false;
        }};
}
On Wednesday, October 21, 2015 at 5:12:08 PM UTC+1, Jose Alcerreca wrote:


Jose Alcérreca | Developer Programs Engineer | ja...@google.com | Google+


On 21 October 2015 at 16:11, Ewan <ew...@telesense.co.uk> wrote:
Hi,

I wondered if anyone has ideas about using Espresso on tables. The views in each TableRow may well have shared ids; so if, for instance, you wanted to test the string in a TextView in the third column of the first row of the table, I don't think there's an easy way to do this. The TableRows aren't views themselves, is there another approach than to tag each view in the table with a composite object which encapsulated row and column info and then write appropriate Matcher<View> instances to match given 'cells' in the table (yuk..)?

Any thoughts?
Cheers
Ewan

--
You received this message because you are subscribed to the Google Groups "Android Testing Support Library" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-testing-support-library+unsubscribe@googlegroups.com.
To post to this group, send email to android-testing-support-lib...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages