DucTran
DucTran

Reputation: 156

Recent Apps on UiAutomator

I am now working with Android UiAutomator on for UI Test on my Android app. My app has a function that requires the user to verify the email to continue, so I try to do it like this: after reach to that function -> getUiDevice.pressHome -> Browser -> try to log in email -> PressHome again -> Press RecentApps then I stuck here, I cannot press on my Apps to return to it again. I try another way by clicking on my App icon but it starts my app again, not at the state before. Can anyone suggest me a solution for this? Any help is appreciate.

Thanks in advance.

Upvotes: 4

Views: 2843

Answers (4)

Tiago Dávila
Tiago Dávila

Reputation: 362

I could manage to create this behavior with:

fun backgroundAndForeground() {
    val device = UiDevice.getInstance(getInstrumentation())
    device.pressHome()
    // Pressing app switch two times makes the last app put on background come to foreground.
    device.pressKeyCode(KeyEvent.KEYCODE_APP_SWITCH)
    device.pressKeyCode(KeyEvent.KEYCODE_APP_SWITCH)
}

Upvotes: 3

Mike Collins
Mike Collins

Reputation: 4549

I've spent half a day on this and concluded I needed to issue a device.click(). Since my use-case is that my app was the last one running (not switching to the browser like you), I can safely click the middle of the screen and it'll always work.

If you're the 2nd to last running app, you can probably do x: 0 and y: device.displayHeight/2.

I've not tested this on many operating systems, only 9.

Upvotes: 0

R.Katnaan
R.Katnaan

Reputation: 2526

In this case, I think that android only resume app when clicking the recent app image. It does not work on clicking display text or app icon. So, we need to click image of your app in recent app list. At that time you need to write as below. I always do that for similar case.

// Take all image view by class type and click by instance no.
new UiObject(new UiSelector().className("android.widget.ImageView").instance(3)).click();

You need to count instance no of your recent app image view. Not app icon image in recent app scroll view. Please try this. Thanks.

Upvotes: 0

Smriti
Smriti

Reputation: 1592

Try this :

UiObject appBackground = new UiObject(new UiSelector().description("ABC"));
appBackground.click();

It did not show any description through 'uiautomatorviewer' command but this worked for me.

Upvotes: 6

Related Questions