X-HuMan
X-HuMan

Reputation: 1488

Espresso test: How to open my application back after opening the recent apps?

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?

Upvotes: 2

Views: 3448

Answers (4)

Mohamed Wasiq
Mohamed Wasiq

Reputation: 587

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)

Upvotes: 1

Maragues
Maragues

Reputation: 38324

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.

Upvotes: 1

Tauno
Tauno

Reputation: 66

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();

Upvotes: 1

anon
anon

Reputation:

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...
  // ...

}

Upvotes: 1

Related Questions