Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Espresso test fails with NoActivityResumedException often

EDIT: Updated description and error message and added some images. Still have this problem.

I have a strange error that occur many times when running espresso tests. After a couple of successful test runs the tests starts failing with following exception:

   06-23 13:04:48.438   info    TestRunner  failed: WhenNavigatingToReportsThenCorrectViewShouldBeShown(com.myapp.ui.views.MainActivityTest)
06-23 13:04:48.439  info    TestRunner  ----- begin exception -----
06-23 13:04:48.441  info    TestRunner  android.support.test.espresso.NoActivityResumedException: No activities in stage RESUMED. Did you forget to launch the activity. (test.getActivity() or similar)?
06-23 13:04:48.441  info    TestRunner  at dalvik.system.VMStack.getThreadStackTrace(Native Method)
06-23 13:04:48.441  info    TestRunner  at java.lang.Thread.getStackTrace(Thread.java:580)
06-23 13:04:48.441  info    TestRunner  at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:82)
06-23 13:04:48.441  info    TestRunner  at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:53)
06-23 13:04:48.441  info    TestRunner  at android.support.test.espresso.ViewInteraction.runSynchronouslyOnUiThread(ViewInteraction.java:184)
06-23 13:04:48.441  info    TestRunner  at android.support.test.espresso.ViewInteraction.doPerform(ViewInteraction.java:115)
06-23 13:04:48.441  info    TestRunner  at android.support.test.espresso.ViewInteraction.perform(ViewInteraction.java:87)
06-23 13:04:48.441  info    TestRunner  at com.myapp.ui.views.MainActivityTest.WhenNavigatingToReportsThenCorrectViewShouldBeShown(MainActivityTest.java:96)
06-23 13:04:48.441  info    TestRunner  at java.lang.reflect.Method.invoke(Native Method)
06-23 13:04:48.441  info    TestRunner  at java.lang.reflect.Method.invoke(Method.java:372)
06-23 13:04:48.441  info    TestRunner  at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
06-23 13:04:48.441  info    TestRunner  at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
06-23 13:04:48.441  info    TestRunner  at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
06-23 13:04:48.441  info    TestRunner  at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
06-23 13:04:48.441  info    TestRunner  at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
06-23 13:04:48.441  info    TestRunner  at android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55)
06-23 13:04:48.441  info    TestRunner  at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:257)
06-23 13:04:48.441  info    TestRunner  at org.junit.rules.RunRules.evaluate(RunRules.java:18)
06-23 13:04:48.441  info    TestRunner  at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
06-23 13:04:48.441  info    TestRunner  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
06-23 13:04:48.441  info    TestRunner  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
06-23 13:04:48.441  info    TestRunner  at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
06-23 13:04:48.441  info    TestRunner  at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
06-23 13:04:48.441  info    TestRunner  at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
06-23 13:04:48.441  info    TestRunner  at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
06-23 13:04:48.441  info    TestRunner  at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
06-23 13:04:48.441  info    TestRunner  at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
06-23 13:04:48.441  info    TestRunner  at org.junit.runners.Suite.runChild(Suite.java:128)
06-23 13:04:48.441  info    TestRunner  at org.junit.runners.Suite.runChild(Suite.java:24)
06-23 13:04:48.441  info    TestRunner  at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
06-23 13:04:48.441  info    TestRunner  at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
06-23 13:04:48.441  info    TestRunner  at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
06-23 13:04:48.441  info    TestRunner  at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
06-23 13:04:48.441  info    TestRunner  at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
06-23 13:04:48.441  info    TestRunner  at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
06-23 13:04:48.441  info    TestRunner  at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
06-23 13:04:48.441  info    TestRunner  at org.junit.runner.JUnitCore.run(JUnitCore.java:136)
06-23 13:04:48.441  info    TestRunner  at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:54)
06-23 13:04:48.441  info    TestRunner  at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:228)
06-23 13:04:48.441  info    TestRunner  at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1837)
06-23 13:04:48.441  info    TestRunner  ----- end exception -----
06-23 13:04:48.443  info    TestRunner  finished: WhenNavigatingToReportsThenCorrectViewShouldBeShown(com.myapp.ui.views.MainActivityTest)

I have a simple app with navigation to pages just containing text at this point, and the test should navigate to each page and identify this text.

@RunWith(AndroidJUnit4.class)
@LargeTest
public class MainActivityTest {

@Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule(MainActivity.class);
private MainActivity mainActivity;

@Before
public void setActivity() {
    mainActivity = mActivityRule.getActivity();
}

 @Test
public void WhenNavigatingToReportsThenCorrectViewShouldBeShown() {
    onView(allOf(withId(R.id.icon), hasSibling(withText(R.string.reports)))).perform(click());

    onView(withText("This is the Reports Activity.")).check(matches(isDisplayed()));
    Spoon.screenshot(mainActivity, "main_view");
}
}

This failure only occurs on the real device. On emulated device the tests works fine. See images below.

All tests are executed by gradle script, starting with removing the previous app and test api, before installing it again. To ensure clean environment. The log indicates that uninstallation has been successful. Then deploying the new app and start running the tests. Now they fail.

If the tests have failed, it's not until I manually remove the app and the test api from device that I can get successful tests again. But only for a while until the same error occur.

Why do I get NoActivityResumedException, I can't find a good example of what it is and when it occurs.

Device overview with only physical device failingFailing tests

like image 786
peuhse Avatar asked Jun 02 '15 12:06

peuhse


4 Answers

I had the same problem. This happen when the screen on device is off. Turn screen on should fix the problem.

like image 122
Artur Poznański Avatar answered Oct 28 '22 15:10

Artur Poznański


You can wake up your device before every test using Uiautomator.

@Before
public void init(){
     UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
     Point[] coordinates = new Point[4];
     coordinates[0] = new Point(248, 1520);
     coordinates[1] = new Point(248, 929);
     coordinates[2] = new Point(796, 1520);
     coordinates[3] = new Point(796, 929);
     try {
         if (!uiDevice.isScreenOn()) {
             uiDevice.wakeUp();
             uiDevice.swipe(coordinates, 10);
         }
     } catch (RemoteException e) {
         e.printStackTrace();
     }
}
like image 35
Gordak Avatar answered Oct 28 '22 16:10

Gordak


Mainly the error is because if your connection is not in an active mode, it shouldn't be in sleep mode either it has to be in the app where the test cases have been written. Assuming you already used the @Rule Activity launch rule.

like image 33
user11479360 Avatar answered Oct 28 '22 16:10

user11479360


In my case, the solution was to use the UI Automater

val uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
uiDevice.findObject(UiSelector().textContains("text")).click()

I wanted to do clicks on an activity hosted by google login SDK. I noticed the activity triggerred by SDK is still visible even when the process of the app under test is killed. So I used UI Automator which is inteded for cross-app functional UI testing

enter image description here

like image 40
Islam Salah Avatar answered Oct 28 '22 16:10

Islam Salah