2

I want to open my application back while writing an Espresso test case after opening recent apps by calling pressRecentApps() method. Is there a way to do this except of simulating a click by coordinates?

X-HuMan
  • 1,488
  • 1
  • 17
  • 37

4 Answers4

1

I'd say that you can't. The moment your app loses focus, you are out of luck.

You can use UI Automator for that.

Nohus
  • 739
  • 8
  • 16
Maragues
  • 37,861
  • 14
  • 95
  • 96
  • I achieved to this with Ui Automator, by finding the title and clicking on it. The bad thing that it just has API min level 18. – X-HuMan Oct 18 '16 at 16:41
1

You can do this with Espresso by calling the following:

val targetContext = InstrumentationRegistry.getTargetContext()
val launchIntent = Intent(targetContext, NameOfTheActivityYouAreTesting::class.java)
activityTestRule.finishActivity()
activityTestRule.launchActivity(launchIntent)

I actually wrote a helper function for this:

inline fun <reified T : Activity> ActivityTestRule<T>.restartActivity() {
  finishActivity()
  launchActivity(Intent(InstrumentationRegistry.getTargetContext(), T::class.java))
}

And I call it like this:

val activityTestRule = ActivityTestRule(ActivityIAmTesting::class.java)

@Test
fun someEspressoTest() {

  // Some testing ...
  // ...

  activityTestRule.restartActivity()

  // Some more testing...
  // ...

}
1

You could use UIAutomator for this if you run your tests on an API level where the recent screen has application labels:

UiDevice device = UiDevice.getInstance(getInstrumentation());
Context appContext = getInstrumentation().getTargetContext();
String appName = appContext.getString(appContext.getApplicationInfo().labelRes);
device.findObject(new UiSelector().text(appName)).click();
Tauno
  • 66
  • 3
0

Try UIAutomator. Tap recent apps twice to get back to your main application your Espresso is handling.

    uiDevice.pressRecentApps()
    Thread.sleep(1000)
    uiDevice.pressRecentApps()
    Thread.sleep(2000)
Mohamed Wasiq
  • 490
  • 4
  • 17