8

I have a Viewpager that's composed of copies of the same fragment view. You can swipe between them. I'm writing an Espresso test and trying to assert on ids of each page, but they are obviously ambiguous because there are multiple pages loaded and they all share the same ids. I don't want to change the view pager to load only 1 page at a time. In my application, it's inefficient to do so.

I've also thought of using tags and tag each view with a unique string, but then I'm modifying the source code only so that my test works. I don't like that. What is the correct way of checking content inside the page of a viewpager with Espresso?

Andre Perkins
  • 7,640
  • 6
  • 25
  • 39
Eduard
  • 3,482
  • 2
  • 27
  • 45

1 Answers1

3

Assuming that only one of the pages is displayed at a time you can use swipeLeft/swipeRight to move to a page and then check that the right thing is displayed in that page.

There are some examples here.

e.g.

 onView(withId(R.id.pager))
   .perform(swipeLeft())
   .check(matches(hasDescendant(withText("Position #2"))));
yogurtearl
  • 3,035
  • 18
  • 24
  • The problem is not with swiping, but with checking content. I need to get access at the specific elements on each page. Unfortunately I can't use hasDescendant(withText()) because of the nature of the content inside those elements (it's often repeated). – Eduard Dec 30 '14 at 21:03
  • 1
    For clarity, let's say I have R.id.mytext inside each page of a viewpager. Its content is unique on each page, but has a repeated part. For example, on page1 it says "Sr. Software Engineer", on page2 it says "Software Engineer in Test". I want to assert that the content changes from swipe to swipe, but still contains the repeated part: "Software Engineer". So to answer your question, the full string of R.id.mytext is unique (title of a job) – Eduard Dec 30 '14 at 21:13