8

Dunno why espresso intermittently can't find a clearly visible elements. It fails with:

failed: android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with id: com.justyoyo.staging.debug:id/content_loading_progress_bar

It's freaking frustrating as it doesn't matter whether I'm running the tests on local physical devices, AWS device farm or on Genymotion's emulators. It also doesn't matter that I add an explicit wait SystemClock.sleep(5000); before checking if the element is visible. I works fine 2,3 times in a row, then it fails.

To give you more insights, we're using Dagger, Mortar and we're running the tests on the UI thread, as running them on separate one was even worse.

kowalcj0
  • 1,524
  • 1
  • 9
  • 8
  • 6
    I'm having a similar issue with flaky Espresso tests. They pass sometimes and fail other times, and it's incredibly frustrating. As a stopgap, I wrote a test rule to re-run failed tests a second time before allowing them to fail. – AutonomousApps Dec 30 '15 at 19:30
  • Can you share that code pls? @Autonomous – HRVHackers Jun 02 '16 at 00:35
  • 5
    @gorbysbm, here you go: https://gist.github.com/trobalik/b812e2a4d36edcf4157c279b143c8de1 – AutonomousApps Jun 02 '16 at 21:49
  • 1
    I had some similar problems with flaky tests (http://stackoverflow.com/questions/42032010/flaky-android-espresso-test-snackbar/42092060#42092060), in my case in particular with the Snackbar. Espresso IdlingResources is one way to wait for stuff to happen, but they introduce a min 5sec delay, which doesn't help if the view is only on the screen for 3.5 sec!! ConditionWatcher to the rescue - https://medium.com/azimolabs/wait-for-it-idlingresource-and-conditionwatcher-602055f32356#.9rms52osh – Mark Feb 07 '17 at 14:35
  • Same here. I use EspressoIdlingRessource inc/dec to make it wait for data loading but it fails right after data loading. I tried to postpone the idling ressource decrement() call by 1sec but still fails right after data loading. Frustrating, I can relate... – Achraf Amil Jun 04 '18 at 12:58

2 Answers2

0

I've been having similar issues a lot. I've followed these steps and it has helped a little.

  1. When your test fails with a NoMatchingViewException check the output of the view hierarchy and see if you see the id/text you were searching for. If you find it the view might not be fully visible and you need to scroll to the right position / generally make sure it isn't covered. Even notifications popping down from your status bar can block your view and ruin you test.
  2. If you do not find the view in the view hierarchy try to figure out what layout espresso is actually looking at. I sometimes have dialogs popping up and espresso evaluating that layout instead of the activity that lies below.
  3. Place a breakpoint where your view gets inflated and make sure you actually get there.

But generally difficult to troubleshoot...

Oliver Metz
  • 2,712
  • 2
  • 20
  • 32
-4

Espresso is starting your Activity but is not able to find any view with the id content_loading_progress_bar. This problem does not come from Espresso but from your test.

Make sure your are correctly starting the correct activity. Also regarding to the AWS Device Farm and your tests with Espresso, I suggest you to follow this template. But be careful, to make it work on AWS Device Farm, all your test methods have to start with "testXYZ()" or the farm will not detect them.

Renaud Mathieu
  • 366
  • 2
  • 9