Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Espresso 2 on Android, intermediately tests fail after failing to start the activity under test while activities from previous tests are still alive

I am using Espresso 2 for testing my Android app. Intermediately I see tests randomly fail with this Espresso failure message:

failed: testLongPressXXXXX (com.company.xxx.tests.testSuite.Test) ----- begin exception ----- android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with id: com.company.xxx:id/xxx_view_id View Hierarchy: +>DecorView{id=-1, visibility=VISIBLE, width=729, height=319, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1} ...

Every beginning of a test the Espresso instrumentation prints out the number of activities that are still alive from a previous test:

MonitoringInstrumentation: Activities that are still in CREATED to STOPPED: (number)

Whenever I see a failed test I always see that the number of alive activities is not 0 which led me to believe that this is the reason for this issue even though most of the time when there are live activities before a test the test still passes successfully.

When this happens the device just shows the home screen for about 10 seconds before failing. This of course happens only when I run more than one test at once.

My question is why are there live activities between tests and is there a way to make the Instrumentation wait until the activities from the previous test are finished before proceeding to the next test.

If someone has a different idea to why the tests are failing intermediately that will also be helpful as well.

like image 605
royeet Avatar asked Jan 05 '16 16:01

royeet


People also ask

How do I record espresso test on Android?

Record UI interactionsClick Run > Record Espresso Test. In the Select Deployment Target window, choose the device on which you want to record the test. If necessary, create a new Android Virtual Device. Click OK.

What causes test failure?

The Three Common Causes of Exam Failure. There are three main ways that students of all ages can sabotage themselves in exams and bed up with an exam results fail: poor exam technique, poor revision and weak understanding of the subject itself. These can all lead to a bad day in the school exam hall.

What is espresso testing in Android?

Espresso is an open source android user interface (UI) testing framework developed by Google. The term Espresso is of Italian origin, meaning Coffee. Espresso is a simple, efficient and flexible testing framework.

What is Expresso in mobile testing?

Espresso created by Google is a native framework for Android automated testing. The tool is a part of the Android SDK and is easy to use for native mobile development. Thanks to Espresso, you can create tests that are close to the Android app's logic.


1 Answers

This question was asked a while ago but since this still seems to be relevant I thought I'd share what I found. I am facing a similar problem - AmbiguousViewMatcherException on Views that have a unique id, different tests failing randomly - probably due to Activities not being destroyed.

This seems to be a known issue and should be addressed in the next release (see here: https://github.com/google/android-testing-support-library/issues/16). Apparently onDestroy() and isFinishing() are not called reliably after each test so you might not get a fresh Activity every time or even end up with two Activities on top of each other.

Until they fix it I've decided not to waste my time on a workaround but starting using Genymotion emulators which solved my problem - possibly because they run faster than the built-in ones in Android Studio (as was suggested here).

like image 112
4ndroid4va Avatar answered Oct 05 '22 22:10

4ndroid4va